View Full Version : Java and I/O thru a COM port

10-05-2006, 02:30 PM
I've had a project dumped in my lap that was started in Java. I dream in C/C++...

Most of what I see I understand, but I'm kind of flying blind because I don't yet know where to find the Java documentation to the level I'm accustomed to in C/C++.

What is the proper way to set up I/O through a serial COM port? The easiest way I know is to define the port as a file, and read or write using the old file procedures or the file stream stuff. There is also the primitive way by messing with buffers, pointers, and device registers.

How do I accomplish the same thing in Java? I assume that I can make use of the java.io package to do the file type of port manipulation.

10-05-2006, 04:10 PM
I had a similar project dumped in my lap a while back. I wrote one end of the connection in C for an embedded instrument and the "receiving" end in Java. Sun has the Java Communications API (http://java.sun.com/products/javacomm/) but it's only available for Solaris SPARC, Solaris x86, and Linux x86. Since I was writing for windows the alternative that I ended up using is RXTX (http://www.rxtx.org/). If I recall correctly, I essentially ended up writing a wrapper class for RXTX which opens the port, listens for incoming reads and places them in a BlockingQueue. It also has methods for writing to the port and closing it. My main app starts a seperate thread which waits for reads to enter the BlockingQueue and then does something with them.

Take a look at RXTX and see if it helps.


10-06-2006, 07:47 AM
Thanks, Lars...

Send questions for Cecil Adams to: cecil@straightdope.com

Send comments about this website to: webmaster@straightdope.com

Terms of Use / Privacy Policy

Advertise on the Straight Dope!
(Your direct line to thousands of the smartest, hippest people on the planet, plus a few total dipsticks.)

Copyright 2018 STM Reader, LLC.