My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://mindsiview.wordpress.com/ and update your bookmarks.

Wednesday, December 08, 2004

The Java Communications API Revisited...

Several months ago I wrote an article for the Borland Developer Network site (http://bdn.borland.com) on the Java Communications API. Since that time I've received numerous emails about the article. To those who've emailed me, thanks for your kind words.

I've compiled the following FAQ based on some of your questions and my responses. If you're working with the Java Communications API, you might find this useful.

Question 1:
If I'm trying to send a character through serial port communication to another computer how can I verify that the signal was sent?

Answer:
I used an old WYSE serial terminal and a null modem cable to test the code. Then I set both the serial ports to COM2 (the serial port on my laptop). I also used a separate communications port monitor of the type used for network troubleshooting.

If you have access to another PC with a serial port, you should be able to hook it to the one you're running the program on (via some serial cables, a null modem adapter, and maybe a gender bender). On the second PC fire up a communications program like HyperTerm or Procomm and set it to communicate on the serial port you're connected to. This will allow you to send and receive data between the PCs.

Question 2:
Is your code correct?

Answer:
The code runs correctly using J2SE 1.4 on a Windows 2000/XP. I've also run it on Red Hat Linux 7.2 using the RXTX version of the comm.jar (there's a link to RXTX in the article).

If you compile my example, execute it, and it doesn't give you any exceptions, it should be working properly. You'll get exceptions if the program can't get handles for the COMn devices or if the comm.jar, win32comm.dll, or javax.comm.properties files can't be found.

Question 3:
is possible to send and receive at commands using java comm?

Answer:
Yes.

Question 4:
Does Java send any additional characters with its strings to the serial port?

Answer:
Java does not send additional characters down the port (I know this because I've tested it with a port monitor). It sends what your program sends.

Question 5:
Does Java send the strings in ASCII values to the port?

Answer:
As far as data encoding is concerned, unless you specify something different, you'll be sending data in the default encoding of your hardware.

Question 6:
I am trying to send serial data using a usb to serial converter since my laptop does not have a serial port. Problem is, in Windows device manager, the usb to serial converter shows up as "Prolific USB-to-Serial Comm Port (COM4)", but running ComControl.java returns:

COM4 null
msg1 - javax.comm.NoSuchPortException

Any suggestions?

Answer:
I tried the usb to serial port converter that I have (it is a Targus PA090) and it worked properly. On the PA090, the serial port cannot run faster than 9600 baud. You may want to check the baud settings on yours. You may also want to experiment with the stop bits, parity, etc. The PA090 also has a serial port reset button. If your converter has one, try resetting it. Check for the latest drivers also.

Question 7:
I've tried running your code and I'm getting a "javax.comm.NoSuchPortException" message. Any clues?

Answer:
First, check your PC to see if the COMn ports you are using have been defined. Next, check the location of win32com.dll and javax.comm.properties. They are required for the comm API to work properly in a Windows environment. Usually the javax.comm.properites file is the culprit. It needs to be located in the classpath of your java program.

Technorati search