Fazecast / jSerialComm

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

read(byte[], long) fill array with 0. #330

Closed Aloomenon closed 3 years ago

Aloomenon commented 3 years ago

Hello everyone. I'm trying to get bytes from a serial port, but it returns real data only for the first 3 batches then only zeros.

my code snippet:

    SerialPort port = SerialPort.getCommPort("COM1");
    port.openPort();

    if (!port.isOpen()) {
        throw new RuntimeException("Port didn't open");
    }

    port.setComPortParameters(115200, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
    port.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 500, 0);

    while (true) {
        int bytes = port.bytesAvailable();
        if (bytes > 0) {
            array = new byte[1024];
            port.readBytes(array, bytes);
            System.out.println(DatatypeConverter.printHexBinary(array));
        }
    }

output is: 000D022B30303030323330314103022B30303030323330314103022B303030303233303141030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

and then only zeros for every read.

But If I connect to this port with putty, I see a lot of non-zero values, like this: 022B30303030323330314103.

Any ideas?

Aloomenon commented 3 years ago

I think the problem is the wrong baud rate. I changed it to 9600 and it works correctly now. But I still can't figure out why I saw batches of correct data and then only zeros.

golimarrrr commented 3 years ago

Looks like the zeros were not received by the port but were already in your array of bytes

Aloomenon commented 3 years ago

But why port.bytesAvailable() returns something > 0 and output is zeros? Zeros prints infinitely.

And one more thing: I changed baudrate and code is working perfectly.

hedgecrw commented 3 years ago

That's not a totally unexpected result when using the incorrect baud rate. Many drivers will return either 0s or garbage values when trying to read serial data with a mismatched speed. Glad changing the baud rate worked for you!