I have a serial device which can read and write bytes to the a java application based on inbuilt protocol. The java application in my windows PC gives me the correct output. My java application is

public class SimpleWriteApp {

    public static void main(String[] args) throws Exception
        Enumeration  portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("SimpleRead Started.");
        while (portList.hasMoreElements())
            CommPortIdentifier  portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
                        //System.out.println ("Found " + portId.getName());
                        if (portId.getName().equals("COM5"))

                                SerialPort serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);

    InputStream in = serialPort.getInputStream();

    OutputStream outputStream = serialPort.getOutputStream();
    CommPort port = serialPort;

    outputStream.write( 53 ); 
    outputStream.write( 71 ); 
    outputStream.write( 241 ); 
    outputStream.write( 8 ); 
    outputStream.write( 83 ); 

    System.out.println( "Write done" );

      byte[] buffer = new byte[ 1024 ];
      int len = -1;

        while( ( len = in.read( buffer ) ) > 0 ) {
                            String stringis= new String( buffer, 0, len,"ASCII" );
        byte[] by_new = stringis.getBytes();

             for(byte s :by_new){
             System.out.printf("%5s", s);




In my Windows PC, the above java application using the Rxtx library gives

   Stable Library
   Native lib Version = RXTX-2.1-7
   Java lib Version   = RXTX-2.1-7
   SimpleRead Started.
   Write done
   53   71   63    8   83   53   27    0   63   83

In Raspberry pi, using /dev/ttyUSB0, it gives

   53   71   63    8   83   53   27    0   -75 -54 -59   83

The output should be 10 bytes with unsigned bytes. Why the Raspberry Pi gives incorrect read.

  • I can't reconcile either of the outputs with the write of 53 71 241 8 83.
    – joan
    Sep 10, 2015 at 10:22
  • @joan.. i have a serial machine which has inbuilt protocol which make it respond like that Sep 10, 2015 at 11:20

1 Answer 1


I would start with creating a dummy serial device which will capture the output of your Java program sent via serial. You could program this in Java. Since you are running the same program on Windows and on the rPi, I assume the issue is with the values the serial device receives, not a problem in the Java code itself. Perhaps the 241 value gets sent over as a signed byte on the rPi (vs unsigned on Windows), and becomes a negative number, which leads your device to display the incorrect terminator -75 -54 -59 instead of 63.

  • How would i receive it as an unsigned byte.. since the read buffer receives byte values in both OS. Sep 11, 2015 at 11:30
  • Not sure - not knowing which serial device you have this is getting tricky. You need to isolate the problem - is it the Java code (unlikely, but then again I do not know which JVM you use on Windows vs rPi which might cause the difference), the transmission via Serial (for which you could build a dummy serial device/listener as I suggested) or an issue in your serial device itself (not knowing what it is it makes it difficult to say anything about it) - for which you would need to look at its documentation to see what those negative return values mean. Of course, perhaps it is an RxTx-lib bug.
    – Phil B.
    Sep 11, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.