[Rxtx] Multiple serial port connections in RXTX

Muzammil Hussain muzammil.hussain.syed at gmail.com
Thu Mar 31 04:36:57 MDT 2011


Michael,

Could you let me know how to do it correctly ? I believe that I am
instantiating seperate objects of SerialReader and SerialWriter for
the 2 serial ports.

regards,
Muzammil




On Thu, Mar 31, 2011 at 12:20 AM, Muzammil Hussain
<muzammil.hussain.syed at gmail.com> wrote:
> Hi,
>
> I have 2 serial ports on my computer and I am using RXTX to read and
> write to them *seperately*. My code snippet is as follows
>
>
>                if ( (commPort1 instanceof SerialPort) && (commPort2
> instanceof SerialPort) )
>                {
>                    SerialPort serialPort1 = (SerialPort) commPort2;
>
> serialPort1.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
>
>                    SerialPort serialPort2 = (SerialPort) commPort2;
>
> serialPort2.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
>
>                    InputStream in_p1 = serialPort1.getInputStream();
>                    OutputStream out_p1 = serialPort1.getOutputStream();
>
>                    InputStream in_p2 = serialPort2.getInputStream();
>                    OutputStream out_p2 = serialPort2.getOutputStream();
>
>                    (new Thread(new SerialReader("MOTE-1", in_p1))).start();
>                    System.out.println("Started serial reader 1");
>                    (new Thread(new SerialWriter("MOTE-1", out_p1,
> write_to_mote))).start();
>                    System.out.println("Started serial writer 1");
>
>                    (new Thread(new SerialReader("MOTE-2",in_p2))).start();
>                    System.out.println("Started serial reader 2");
>                    (new Thread(new SerialWriter("MOTE-2", out_p2,
> write_to_mote2))).start();
>                    System.out.println("Started serial writer 2");
>
>                    System.out.println("Successfully connected to
> Cricket mote on ports "+ port1Name +" and "+ port2Name);
>                }
>
> The SerialReader and SerialWriter are Runnable classes as defined in
> the example on the RXTX wiki.
>
> The problem is that there seems to be an intermingling of the data
> read and written to the two ports. For example, a test byte-stream
> coming on port 1, will have some of the characters on the SerialReader
> of port 2 as well. Similarly, when I use the SerialWriter tp write
> some bytes to Port 1, I find that some (if not all of them) are being
> written on the SerialWriter of Port 2.
>
> I guess it has to do with the thread-safe aspect of RXTX and I
> understand that it is not thread-safe.
>
> Is there a possible solution for this ?
>
> M
>



More information about the Rxtx mailing list