I have weight indicator device that sends data using rs-485 protocol,
I am connecting it to computer using USB (serial to usb converter). using program (serial comm tester) i can read data correctly from it and it works fine. now I am trying to read this data using java using JSSC library and this is my code
public class Test2 {
/**
* @param args the command line arguments
*/
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM9");
try {
serialPort.openPort();//Open port
serialPort.setParams(SerialPort.BAUDRATE_115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
public static class SerialPortReader implements SerialPortEventListener {
String buffer = "";
private void onMessage() {
// constructing message
System.out.println("RECEIVED MESSAGE: " + buffer);
buffer = "";
}
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
String b = serialPort.readString(event.getEventType());
System.out.println("event:"+b);
if (b.equals("\n") ) {
onMessage();
} else {
buffer += b;
}
} catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
}
}
this is what data should look like and the option used in "serial comm tester" i should receive " 50.46 KG NT " serial comm tester output
but in java I can not read it correctly, any help please? java output
note that after multiple values sends java program shows some character from perivous data !!