porting RXTX linux code to windows

A

Arash Nikkar

I am trying to port some Java code which was written specifically for
linux over to windows, and I have run into a bit of a problem.

The linux code uses a USB->Ir dongle, but uses the RXTX comm package. I
am a bit confused by this, because I though the comm package does not
support USB. Is the Comm package being "fooling" (sorry couldn't think
of a better term), into thinking that the USB is a serial port. The
code is as follows:

String port = "/dev/ttyUSB0"; //also seems to use: /dev/ttyS0 for
testing
RXTXCommDriver gnuDriver = new RXTXCommDriver();
CommPortIdentifier.addPortName(port,
CommPortIdentifier.PORT_SERIAL, gnuDriver);
portId = CommPortIdentifier.getPortIdentifier(port);
if (portId.getPortType() != CommPortIdentifier.PORT_SERIAL) {
System.out.println("Port " + port + " is not a serial port");
return;
};
serialPort = (SerialPort)portId.open("Test", 2000);
outputStream = serialPort.getOutputStream();
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);


Seems straighforward to me, except for the PORT part. Is it possible to
mimick this code in windows?

The traditional way I have used serial ports with java is by asking the
system for all the available comm ports, and iterating through them:

List<SerialPort> serialPortList = new ArrayList<SerialPort>();
while (portList.hasMoreElements()) {
CommPortIdentifier portId =
(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL &&
!portId.isCurrentlyOwned()) {
try {
SerialPort tempPort = (SerialPort)
portId.open("Test", 2000);
tempPort.setSerialPortParams(9600,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
....
}

thanks in advance...
 
G

Gordon Beaton

The linux code uses a USB->Ir dongle, but uses the RXTX comm
package. I am a bit confused by this, because I though the comm
package does not support USB. Is the Comm package being "fooling"
(sorry couldn't think of a better term), into thinking that the USB
is a serial port. The code is as follows:

String port = "/dev/ttyUSB0";

All of the tty devices can be treated as serial ports by software,
regardless of the physical device (if any) they represent.

So even though your software may not support USB directly, the device
has been identified as a USB serial device and it will be supported by
the usbserial driver, which has created /dev/ttyUSB0 for applications
to access it through.

/gordon
 
M

Martin Gregorie

Gordon said:
All of the tty devices can be treated as serial ports by software,
regardless of the physical device (if any) they represent.

So even though your software may not support USB directly, the device
has been identified as a USB serial device and it will be supported by
the usbserial driver, which has created /dev/ttyUSB0 for applications
to access it through.
I think you'll find that your real problem is serial port support in
Java. I had a hunt around a bit over a year ago and found the following
three possibilities:

- Sun's javax.comm - only available for Solaris and Linux
- SimpleSerial - used to talk to PIC chips. Requires javax.comm
- RXTX, which is essentially a javax.comm replacement.
Currently for Linux, OSX and Win32 (but this requires Cygwin).

These have all moved on since then. When I did the search RXTX needed a
Linux kernel module, SimpleSerial was standalone and only talked to a
PIC chip and javax.comm seemed to be moribund.

As a consequence I wrote SerialPort (serialport.sourceforge.net).
SerialPort is an interface class using sockets to talk to a C server
that drives the com ports. It is POSIX compliant, so should port easily
to any compiler/OS combo that has decent socket and poll() implementations.

HTH
 
A

Arash Nikkar

Gordon,

so basically your saying that the USB is being interpreted as a serial
device in linux (since it is a tty), and in order to port the code over
(and continue to use the comm package rather than jUSB) I would have to
figure out a way for windows to treat the USB as a serial device as
well?

Martin,

You are right, the comm package is very lacking, and from what I have
read, Sun has stopped maintaining it. I dont know anything about
simpleSerial, but from what I have read about RXTX it is now fully
compatible with windows as well. Seems like a very robust package...
 
M

Martin Gregorie

Arash said:
You are right, the comm package is very lacking, and from what I have
read, Sun has stopped maintaining it. I dont know anything about
simpleSerial, but from what I have read about RXTX it is now fully
compatible with windows as well. Seems like a very robust package...
Don't forget they you'll need to install Cygwin to use it. I don't know
what implications that has for your production environment.

Mind you, using serial ports under Windows is a pain anyway. I needed to
do it a while back and after trying to roll my own and then use some
PD drivers, neither being satisfactory, I ended up buying Willies
Software's COMM-DRV package, which worked very nicely. That was all C.
If I was doing it again I think I'd still go the COMM-DRV way.

When you've tried out RXTX touch base here again. I for one would like
to know how you get on with it.
 
M

Martin Gregorie

Arash said:
Hi Martin,

I think you are wrong about needing cygwin for windows and RXTX. I just
ran a quick test, and I was able to use RXTX with java without cygwin.
I think the new version of RXTX has provided this.
Fair enough. I was just quoting from the RXTX website. It wasn't obvious
why Cygwin was needed apart from the installation instructions looking
like shell commands.

At a guess RXTX has been completely written at least twice and gained a
lot flexibility and portability in the process. When I first looked at
it, it was Linux-only, being based round a kernel module and native
interface code.
As for my latest problem, I used IrCOMM2k to use my USB ir dongle as a
COMM port, and as far as java/windows is concerned everything seems to
be working fine. I dont get any exceptions, or anything of that sort,
but I can seem to get the communication across.
Cool. Good to know. I haven't played with IR links yet, but I found
exactly the same transparency with USB<->serial converters under Linux.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top