Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Serial Communication -> Problem with EventListener

i'm trying to use Java Serial Communication to read measured values from a serial device. The task is to send the ASCII Code for 9 (57 in decimal) to the device and it will return the current value that is measured. In first place, I want to make sure that some value is correctly send back. So far, the connection works and when i changed the getListeningEvents() method to check for SerialPort.LISTENING_EVENT_DATA_WRITTEN it worked and showed me my phrase "Reading possible", but in this case this only shows that data got transferred.

So the number is send correctly, but I can't get an answer and the program gets stuck after printing "Written". In many examples i saw the method notifyOnDataAvailable() for the SerialPort class, but i can't find it in the documentation anymore and i'm not sure if i have to use any other methods to initialize the listener. So my question is, what is wrong about my program, especially my EventListener, that it can't receive or identify the returned value?

Here is my code:

public class ConnectionNew implements SerialPortDataListener {

    private  SerialPort serialPort = null;
    private java.io.OutputStream output = null;
    private InputStream input = null;
    private SerialPort [] ports = null;
    
    public static void main(String[] args) {
        ConnectionNew connect = new ConnectionNew();
        connect.connectPort();
        connect.initIOStream();
        connect.initListener();
        connect.writeData();

    }
    
    public void connectPort() {
        ports = SerialPort.getCommPorts();
        System.out.println("Select a port: ");
        int i = 1;
        for (SerialPort port : ports) {
            System.out.println(i++ + ": " + port.getSystemPortName());
        }
        Scanner s = new Scanner(System.in);
        int chosenPort = s.nextInt();
        serialPort = ports[chosenPort - 1];
        if (serialPort.openPort()) {
            System.out.println("Port opened successfully");
        } else {
            System.out.println("Unable to open the port");
            return;
        }
    }
    
    public boolean initIOStream(){
        input = serialPort.getInputStream();
        output = serialPort.getOutputStream();
        System.out.println("Streams Connected");
        return true;
    }
    
    public void initListener() {
        serialPort.addDataListener(this);
        
    }
    
    public void writeData(){
        try {
            
            output.write(57);
            output.flush();
            
            System.out.println("Written");
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
    }
    @Override
    public int getListeningEvents() {
        return  SerialPort.LISTENING_EVENT_DATA_RECEIVED;
    }

    @Override
    public void serialEvent(SerialPortEvent arg0) {
        System.out.println("Reading possible");
        
    }

}

I'm very happy about any hints, thanks in advance!!

like image 284
Sophie_R Avatar asked Feb 05 '26 06:02

Sophie_R


1 Answers

You need to get the data from the event:

public void serialEvent(SerialPortEvent e) {
    byte[] data = e.getReceivedData​();
    // ...
}
like image 50
Guillaume Avatar answered Feb 06 '26 19:02

Guillaume