-1

i have a problem that i can call the method from other class into my JFrame

this my method class which i got from other people in this forum

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package transaksi_satu;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;


public class UnoConnect implements SerialPortEventListener{

    SerialPort serialPort;

    private static final String PORT_NAMES[] = {"COM3"};
    private BufferedReader input;
    private OutputStream output1;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;

    public UnoConnect(){
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output1 = serialPort.getOutputStream();

        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }



    }
    public void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

public void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=null;
            if (input.ready()) {
                inputLine = input.readLine();
                            System.out.println(inputLine);
                            //Lbl_ID.setText(inputLine);                
            }

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

}
}

i want get the result from function "public void serialEvent(SerialPortEvent oEvent)" at the bottom of source code, when i try to call it into another from it doesn't show up.

what mistake i make? can someone help me?

Deltharis
  • 2,320
  • 1
  • 18
  • 29
  • Have you debugged it? Maybe there was an exception so `serial event` haven't ever called – Beloo Oct 13 '16 at 07:21
  • @Beloo yeah, i have but the result is "Build Successfull" – klik fourth Oct 13 '16 at 07:24
  • It's not a debugging, it's building. Debugging is a process when you attach debugger to your running application and perform program operations step by step to check input and output results of each line. – Beloo Oct 13 '16 at 07:27
  • Please, read the http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve, then improve your question in accordance to those standards – Krzysztof Cichocki Oct 13 '16 at 07:28

1 Answers1

0

The method public void serialEvent(SerialPortEvent oEvent) is executed when something is received from serial port (it is not intended to be called by yourself), so inside it you need to process what is received, to picture it (based on your example, pay attention to the comments inside, there you should do something with the received data):

public void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=null;
            if (input.ready()) {
                inputLine = input.readLine();
                            System.out.println(inputLine);
                            // DO SOMETHING WITH THE inputLine RECEIVED HERE 
                            // EG. PASS IT TO SOME OTHER SERVICE METHOD

            }

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • Can you help me, how to pass the result to other service method if it in the method??? – klik fourth Oct 13 '16 at 07:37
  • i have a problem in here for a few days, and it give me a headache – klik fourth Oct 13 '16 at 07:38
  • Add a reference to that service into the listener (best during construction), then just call proper method of that service. Remeber that you need to register this listener into proper serial port (constructed earlier) in order to receive events from it. Look at the documentation of the library you use for that. – Krzysztof Cichocki Oct 13 '16 at 07:39