NeuronRobotics / nrjavaserial

A Java Serial Port system. This is a fork of the RXTX project that uses in jar loading of the native code.
Other
345 stars 143 forks source link

SerialPort.inputstream only returns HEX 00 when reading bytes #96

Closed kgoderis closed 4 years ago

kgoderis commented 7 years ago

@madhephaestus @cvanorman [as complement to https://github.com/NeuronRobotics/nrjavaserial/issues/95] I discovered the following behaviour on a Linux Ubuntu 17.04 instance:

I monitor a USB serial port (connected to a remote device that takes ASCII commands, returning an ASCII answer in return) by running the following command that creates a pseudo-tty that is relaying between nrjavaserial and the actual USB port:

sudo socat -v /dev/ttyUSB0,raw,echo=0 pty,link=/dev/ttyS1,raw,echo=0

In a first test, when I declare the following in java (omitting boiler plate try/catch clauses):

            RXTXCommDriver rxtxCommDriver = new RXTXCommDriver();
            rxtxCommDriver.initialize();
            CommPortIdentifier.addPortName("/dev/ttyS1", CommPortIdentifier.PORT_RAW, rxtxCommDriver);
            portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS1");
            serialPort = portId.open(this.getThing().getUID().getBindingId(), 2000);
            inputStream = serialPort.getInputStream();

and then read out the stream as follows:

                    if ((len = inputStream.read(tmpData)) > -1) {
                        foundStart = false;
                        if (logger.isTraceEnabled()) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < len; i++) {
                                sb.append(String.format("%02X", tmpData[i]));
                                sb.append(" ");
                            }
                            logger.trace("Read {} bytes : {}", len, sb.toString());
                        }
                      ..........

then socat is returning:

getCYN\r> 2017/05/08 20:06:01.179107  length=1 from=80 to=80
.> 2017/05/08 20:06:01.195054  length=4 from=81 to=84
C5\r.< 2017/05/08 20:06:04.224502  length=7 from=35 to=41

with "getCYN" the input for the remote device, and "C5" being the response from the device

on the java console I get the same ASCII sequence, as expected:

2017-05-08 20:06:01.179 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 1 bytes : 00 
2017-05-08 20:06:01.179 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals '' (hex '00')
2017-05-08 20:06:01.189 [TRACE] [.oceanic.handler.OceanicThingHandler] - Reading the inputStream
2017-05-08 20:06:01.195 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 1 bytes : 43 
2017-05-08 20:06:01.195 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals 'C' (hex '43')
2017-05-08 20:06:01.195 [TRACE] [.oceanic.handler.OceanicThingHandler] - Found the start
2017-05-08 20:06:01.195 [TRACE] [.oceanic.handler.OceanicThingHandler] - dataBuffer[0] set to 'C'(hex '43')
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - Reading the inputStream
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 3 bytes : 35 0D 00 
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals '5' (hex '35')
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - Found the start
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - dataBuffer[1] set to '5'(hex '35')
' (hex '0D')0:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 1 equals '
2017-05-08 20:06:01.205 [TRACE] [.oceanic.handler.OceanicThingHandler] - The resulting line is 'C5'

However, if I declare directly the USB port, in either PORT_RAW or PORT_SERIAL mode:

           .....
            CommPortIdentifier.addPortName("/dev/ttyUSB0", CommPortIdentifier.PORT_RAW, rxtxCommDriver);
            portId = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
           .....

then the output is as follows for socat (socat is only reporting the bytes returned on the /dev/ttyUSB0 serial port):

> 2017/05/08 19:59:56.369714  length=6 from=0 to=5
2719\r.> 2017/05/08 20:00:09.399050  length=4 from=6 to=9
C5\r.> 2017/05/08 20:00:22.412339  length=5 from=10 to=14
00:00> 2017/05/08 20:00:22.413233  length=2 from=15 to=16
\r.> 2017/05/08 20:00:35.441477  length=8 from=17 to=24
.00:01\r.> 2017/05/08 20:00:48.473635  length=3 from=25 to=27
1\r.> 2017/05/08 20:01:01.470788  length=4 from=28 to=31
37\r.

e.g. the device is returning valid responses, and the serial driver on the linux instance is correctly working, BUT the inputstream.read() calls is returning a constant stream of HEX 00 bytes:

2017-05-08 20:00:19.402 [WARN ] [.oceanic.handler.OceanicThingHandler] - Received an empty answer for 'getCYN'
 ('[103, 101, 116, 67, 89, 84, 13]')eanic.handler.OceanicThingHandler] - writeString : getCYT
2017-05-08 20:00:22.412 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 1 bytes : 00 
2017-05-08 20:00:22.412 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals '' (hex '00')
2017-05-08 20:00:22.422 [TRACE] [.oceanic.handler.OceanicThingHandler] - Reading the inputStream
2017-05-08 20:00:32.428 [WARN ] [.oceanic.handler.OceanicThingHandler] - A timeout occurred while requesting data from the water softener
2017-05-08 20:00:32.428 [TRACE] [.oceanic.handler.OceanicThingHandler] - Resetting the SerialPortReader
2017-05-08 20:00:32.428 [WARN ] [.oceanic.handler.OceanicThingHandler] - Received an empty answer for 'getCYT'
 ('[103, 101, 116, 82, 84, 73, 13]')eanic.handler.OceanicThingHandler] - writeString : getRTI
2017-05-08 20:00:45.452 [WARN ] [.oceanic.handler.OceanicThingHandler] - A timeout occurred while requesting data from the water softener
2017-05-08 20:00:45.452 [TRACE] [.oceanic.handler.OceanicThingHandler] - Resetting the SerialPortReader
2017-05-08 20:00:45.452 [WARN ] [.oceanic.handler.OceanicThingHandler] - Received an empty answer for 'getRTI'
 ('[103, 101, 116, 87, 72, 85, 13]')eanic.handler.OceanicThingHandler] - writeString : getWHU
2017-05-08 20:00:48.457 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 1 bytes : 00 
2017-05-08 20:00:48.457 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals '' (hex '00')
2017-05-08 20:00:48.468 [TRACE] [.oceanic.handler.OceanicThingHandler] - Reading the inputStream
2017-05-08 20:00:58.459 [WARN ] [.oceanic.handler.OceanicThingHandler] - A timeout occurred while requesting data from the water softener
2017-05-08 20:00:58.459 [TRACE] [.oceanic.handler.OceanicThingHandler] - Resetting the SerialPortReader
2017-05-08 20:00:58.459 [WARN ] [.oceanic.handler.OceanicThingHandler] - Received an empty answer for 'getWHU'
 ('[103, 101, 116, 73, 87, 72, 13]')eanic.handler.OceanicThingHandler] - writeString : getIWH
2017-05-08 20:01:01.470 [TRACE] [.oceanic.handler.OceanicThingHandler] - Read 1 bytes : 00 
2017-05-08 20:01:01.470 [TRACE] [.oceanic.handler.OceanicThingHandler] - Byte 0 equals '' (hex '00')
2017-05-08 20:01:01.481 [TRACE] [.oceanic.handler.OceanicThingHandler] - Reading the inputStream
madhephaestus commented 4 years ago

should be fixed as of 5.0.2