Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.34k stars 287 forks source link

Reading ASCII string #383

Closed indiika closed 2 years ago

indiika commented 2 years ago

Hi all, I'm trying to read a device output using JSC, According to device specification it sends ASCHII stream via serial port, Also I tried device monitoring studio to read the com output and row data set receive like bellow,

....... WBC 4.3910^9/l[4.00-10.00] LYM-0.6810^9/l[1.30-4.00] MON 0.4810^9/l[0.15-0.70] GRA 3.2310^9/l[2.50-7.50] LY%-15.5%[25.0-40.0] MO%+10.9%[ 3.0- 7.0] GR% 73.6%[50.0-75.0] RBC-3.6910^12/l[4.00-5.50]

................

But when I try to capture same using jSerialComm output is not readable,

e.g. �ooSW��w��

I used bellow code as well as various other available samples.

public static void main(String[] args) {

    System.out.println("Hello world");

    SerialPort[] ports = SerialPort.getCommPorts();

    for (SerialPort port : ports) {
        System.out.println(port.getSystemPortName());
        if (port.getSystemPortName().equals("COM6")) {

            //----------------------------------------------------------------
            SerialPort comPort = port;
            comPort.openPort();
            comPort.addDataListener(new SerialPortDataListener() {
                @Override
                public int getListeningEvents() {
                    return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
                }

                @Override
                public void serialEvent(SerialPortEvent event) {
                    if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_RECEIVED) {
                        return;
                    }

                    try {

                        comPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);//Selectport is my port name//

                        byte[] byteArray = event.getReceivedData();
                        String s = new String(byteArray, StandardCharsets.UTF_8);
                        System.out.println(s);
                    } catch (Exception e) {
                        System.out.println(" " + e.toString());
                    }

                }
            });

            //----------------------------------------------------------------
        }
    }

}

Can someone kindly help me to read expected data using the library

hedgecrw commented 2 years ago

Try not calling setComPortTimeouts in your event handler...this is causing the port to get reconfigured every time data is received which could definitely be causing problems. If the problem persists, it may be helpful to print out the actual received bytes in hex and compare them to what you are seeing in Device Monitoring Studio so that we can see what is truly being received instead of the UTF-8 converted version of it.

indiika commented 2 years ago

Many thanks for the quick response. I can see a hex string now, F9,56,FC,F9,EE,6F,03,53,57,EF,C7,77,FF,C6,C2,C6,4A,42,EE,EE,92,4E,2E,C6,A2,A2,EE,E2, .... But how can I collect it to a single String or buffer, as it sends data periodically .... like when I hit a button in the device. Thanks again.

public static void main(String[] args) {

    System.out.println("Hello world");

    SerialPort[] ports = SerialPort.getCommPorts();

    for (SerialPort port : ports) {
        System.out.println(port.getSystemPortName());
        if (port.getSystemPortName().equals("COM6")) {

            //----------------------------------------------------------------
            SerialPort comPort = port;
            comPort.openPort();
            comPort.addDataListener(new SerialPortDataListener() {
                @Override
                public int getListeningEvents() {
                    return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
                }

                @Override
                public void serialEvent(SerialPortEvent event) {
                    if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_RECEIVED) {
                        return;
                    }

                    try {
                        byte[] byteArray = event.getReceivedData();
                        for (byte b : byteArray) {
        String st = String.format("%02X", b);
        System.out.print(st + ",");
    }
                    } catch (Exception e) {
                        System.out.println(" " + e.toString());
                    }

                }
            });

            //----------------------------------------------------------------
        }
    }

}

hedgecrw commented 2 years ago

Please re-test your application using the newly released library version 2.8.0, although I don't expect it will solve your problem. It looks more like there is some sort of baud rate or flow control mismatch between your device and your port configuration. Please report back once you've re-tested. Thanks!

olegazyx commented 2 years ago

Chech port settings, mainly port speed, I think speed is different on host pc and your device

indiika commented 2 years ago

Hi hedgecrw many thanks for the response. Instead byte array I used line reader and got it working. Cheers !

hedgecrw commented 2 years ago

Great, thanks!