Data corruption issue in Java program using jSerialComm library, but in C# it is OK

154 views Asked by At

I have a Java program that communicates with various devices to retrieve data via serial communication. I'm using the jSerialComm library for this purpose.

However, when I retrieve data from a particular device using this Java program, the data appears to be corrupted.

Surprisingly, when I use a C# program without any external libraries to retrieve data from the same device, the data is received correctly, without any corruption.

The device configuration settings are as follows:

Port: COM3
BaudRate: 2400
Parity: EVEN
Databits: 7
Stopbits: 1
  • In my Java program, I'm using the jSerialComm library, while in the C# program, I'm not utilizing any external libraries.

  • My Java program can successfully detect and retrieve normal data from other devices using the same communication settings. The data corruption issue seems to be specific to this particular device.

I have compared the logs from both programs and observed that the C# program consistently receives the expected data, whereas the Java program consistently receives corrupted data from this specific device. I have also attempted to modify the baud rate and parity settings in the Java program, but the issue persists.

Java Program Connect Device:

SerialPort[] serialPorts = SerialPort.getCommPorts();
for (SerialPort port : serialPorts) {
                if (DEVICE_PORT.equals(port.getSystemPortName())) {
                    bolFound = true;
                    serialPort = port;
                    try {
                        log.info("Opening port '" + DEVICE_PORT + "'...");
                        log.info("Connecting to device '" + DEVICE_NAME + "'...");

                        serialPort.setComPortParameters(DEVICE_RATE, DEVICE_DATABITS, DEVICE_STOPBITS, DEVICE_PARITY);
                        serialPort.addDataListener(this);
                        serialPort.openPort();

...........

Java Data listening:

@Override
public int getListeningEvents() {
    return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}

@Override
public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
        case SerialPort.LISTENING_EVENT_DATA_AVAILABLE:
            readComm();
            break;
        default:
            log.info("XXX default");
            break;
    }
}

public void readComm() {
    log.info(AppConfig.getInstance().getProperty(AppConfig.KEY_TEST_PORT, String.class) + " Reading Comm...");
    byte[] readBuffer = new byte[1024];
    try {
        inputStream = serialPort.getInputStream();
        int len = 0;
            
        while ((len = inputStream.read(readBuffer)) != -1) {
            log.info("Real Time Response:" + new String(readBuffer, 0, len));
            value += new String(readBuffer, 0, len);
            break;
        }
        log.info("value:" + value);

        if (isDataReady) {
            // process data
            value = ""; //reset data
        }

    } catch (IOException e) {
        intNumberOfFailure++;
    }
}

Java Program Log File:

2023-11-09 10:38:58.165  INFO 21320 [Thread-117 thread] h.h.c.v.l.BaseListener                   : Trying to init Test Listener (COM5)...
2023-11-09 10:38:58.350  INFO 21320 [Thread-117 thread] h.h.c.v.l.BaseListener                   : Opening port 'COM5'...
2023-11-09 10:38:58.350  INFO 21320 [Thread-117 thread] h.h.c.v.l.BaseListener                   : Connecting to device 'Testing'...
2023-11-09 10:38:58.350  INFO 21320 [Thread-117 thread] h.h.c.v.l.BaseListener                   : Opening port 'COM5' Success
2023-11-09 10:38:58.350  INFO 21320 [Thread-117 thread] h.h.c.v.l.BaseListener                   : Connecting to device 'Testing' Success
2023-11-09 10:38:59.211  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:38:59.215  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:[ 
2023-11-09 10:38:59.217  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ 
2023-11-09 10:38:59.219  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:38:59.221  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:l
2023-11-09 10:38:59.222  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l
2023-11-09 10:38:59.225  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:38:59.227  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:@ 1
2023-11-09 10:38:59.229  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1
2023-11-09 10:39:00.215  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:39:00.215  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:{=Um%
2023-11-09 10:39:00.226  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1{=Um%
2023-11-09 10:39:00.230  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:39:00.232  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:l
2023-11-09 10:39:00.232  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1{=Um%l
2023-11-09 10:39:00.236  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:39:00.237  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response: ,a
2023-11-09 10:39:00.238  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1{=Um%l ,a
2023-11-09 10:39:00.241  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:39:00.242  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response: 
2023-11-09 10:39:00.243  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1{=Um%l ,a 
2023-11-09 10:39:01.202  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : COM5 Reading Comm...
2023-11-09 10:39:01.217  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : Real Time Response:  
2023-11-09 10:39:01.217  INFO 21320 [Thread-120 thread] h.h.c.v.l.TestListener                   : value:[ l@   1{=Um%l ,a   

C# Program Connect Device:

string portName = "COM3";
int baudRate = 2400;
Parity parity = Parity.Even;
int dataBits = 7;
StopBits stopBits = StopBits.One;

_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);

_serialPort.DataReceived += SerialPort_DataReceived;

_serialPort.Open();

C# Program Data listening:

Console.WriteLine("Serial port opened. Press Enter to stop reading.");
        _logWriter = new StreamWriter("log.txt", true);

        while (true)
        {
            if (_serialPort.BytesToRead > 0)
            {
                string data = _serialPort.ReadExisting();
                DateTime now = DateTime.Now;

                string logMessage = $"[{now}] Received data: {data}";

                Console.WriteLine(logMessage);
                _logWriter.WriteLine($"[{now}] Received data: {data}");
                _logWriter.Flush(); // Flush the buffer to ensure data is written immediately

            }
        }

C# Program Log File:

[09/11/2023 10:53:09] Received data: US,+000091.50 kg

[09/11/2023 10:53:09] Received data: US,+000091.50 kg

[09/11/2023 10:53:10] Received data: US,+000091.50 kg

[09/11/2023 10:53:10] Received data: US,+000091.50 kg

[09/11/2023 10:53:10] Received data: S
[09/11/2023 10:53:10] Received data: T,+171.4 cm

[09/11/2023 10:53:11] Received data: ST,+000091.50 kg

[09/11/2023 10:53:11] Received data: US,+0000
[09/11/2023 10:53:11] Received data: 91.50 kg

[09/11/2023 10:53:12] Received data: US,+000091.50 kg

[09/11/2023 10:53:12] Received data: US,+000091.50 kg

[09/11/2023 10:53:13] Received data: US,+00
[09/11/2023 10:53:13] Received data: 0091.50 kg

[09/11/2023 10:53:13] Received data: US,+000091.50 kg

My goal is to understand why the Java program using the jSerialComm library encounters data corruption only with this specific device, while it can successfully retrieve normal data from other devices. How to modify my Java program to receive the data correctly from this particular device?

1

There are 1 answers

7
Ihdina On

Try like this:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

private final static char CR = (char)0x0D;
private final static char LF = (char)0x0A;
private final static ByteArrayOutputStream stream = new ByteArrayOutputStream();

public static String bytesToHex(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
      sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

public void readComm() {
    try {
        
        // if data available
        int length = serialPort.bytesAvailable();

        if(length > 0) {        

            // read buffer
            byte[] readBuffer = new byte[length];
            
            // read bytes and store in read readBuffer
            int numRead = serialPort.readBytes(readBuffer, readBuffer.length);

            // print length of data
            System.out.println("Read " + numRead + " bytes.");

            // append data to stream 
            stream.write(readBuffer);

            // stream to byte array
            byte[] packets = stream.toByteArray();

            // parsing data
            for (int i = 0; i < packets.length; ++i) {              

                // detect CR LF on packets
                if(((char)packets[i] == CR) && ((char)packets[i+1] == LF)) {

                    // read completed data
                    byte[] completed = Arrays.copyOfRange(packets, 0, i);
                    System.out.println(bytesToHex(completed));

                    // stream discarded
                    stream.reset(); 

                    // out looping
                    break;
                }
            }
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}