I am trying to send data through COM-port via SerialPort class and receive data through COM-port, but having this problem.........................................................................................................
Thank you.
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.*;
public class writeValues {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM1");
try {
//Open port
String T;
serialPort.openPort();
serialPort.setParams(2400,7,2,1, false, true);
String bytes = serialPort.readString(); //Reads 10 bytes from serial port
serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
serialPort.writeString("connected.");
//serialPort.closePort();
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
public static class PortReader implements SerialPortEventListener {
@Override
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR() && event.getEventValue() > 0) {
try {
String receivedData = SerialPort.readString(event.getEventValue());
System.out.println("Received response: " + receivedData);
}
catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
}
}