Search code examples
javaserial-portrxtx

RXTX receive error


I'm using RXTX-2.1-7 and am trying to read a COM port in Windows. I see some inconsistencies in the received data compared to the data received by using PuTTY.

The data received in PuTTY:

a5 5a 0b 05 10 00 00 00 00 27 d4 73 30 ae

But the exact same data received using RXTX:

3f 5a 0b 0510 00 00 00 00 27 3f 73 30 3f

It seems like all received bytes greater than at least a0 are read as 3f. Here's the relevant part of the code that I'm using

                char[] buffer = new char[14];
                int i=0;
                Arrays.fill(buffer,(char)0);
                while (i<14)
                {                    
                    buffer[i++] = (char)charReader.read();   /*DOUBT*/
                }
                /*System.out.println(Arrays.toString(buffer));*/
                String bufferString = new String(buffer);
                System.out.println(String.format("%x ", new BigInteger(bufferString.getBytes("ISO-8859-1"))));

And charReader is an InputStreamReader of the opened serial port. I also checked if the typecast to (char) in the line marked /*DOUBT*/ is the culprit, but the code without the cast I still get the inconsistency.

65533, 90, 11, 5, 16, 0, 0, 0, 0, 39, 65533, 115, 48, 65533

Any ideas why I'm getting this inconsistency?


Solution

  • It's a character encoding issue: I think your java program decodes port input as UTF-8. So you get either ? (ascii \x1a) or unicode REPLACEMENT CHARACTER (65533) as a placeholder for invalid characters.

    It's better to work with bytes explicitly, without conversion to characters, when you really work with bytes. If you absolutely have to represent bytes as characters, use unibyte encoding ISO-8859-1 (which has 1:1 mapping to lower 256 unicode characters).