New Windows Java Serial Com Port Driver

Posted: February 26, 2011 in Java

New Windows Java Serial Com Port Driver

with proper handling of Timeouts

I had to interface a machine using Java and the serial port. It is not the first time that I need this and as usual I headed to Sun Java homepage to download the COM driver for Windows (XP)

Not anymore… Windows COM ports are not directly supported by Sun. It is not so bad since there is another package called rxtxthat is mostly suited. I did try it, amazing stuff, but it does not handle (at the time I checked and as far as I understand) serial port timeouts on TX and timeout on RX is done in some sort of polling mode.

For all of you wondering, proper timeout handling is essential if you want to avoid getting stuck when your machine (connected to the serial port) is not talking to you anymore and you want to recover nicely from this (without shutting down ports or applications)

Well, Java is open, and you can add pieces if you want, so I have written a Serial Driver for Windows XP that makes full use of all Windows features, and especially of the OVERLAPPED mode in COM handling. Using overlapped mode makes the whole driver quite simple and hopefully bug free 🙂

Thanks to Aleksandar Kanchev for pointing out that the event value does NOT come from a getOverlapped BUT from the previously called and possibly no more in scope GetEvent parameter. The new api does not have the bug and events should be returned correctly. At first I could not believe it, basically what Microsoft has done is a nice way to corrupt stack if one is not extra careful, but what could you expect from them ?

Simpler API more functions

If you want simpler API and more functions keep reading here, otherwise go on the “old API” section.

The new API provides the following:

  • Allow to know how many bytes have been read and written, even for partial read/write
  • Allow for discover/use of devices that are added at runtime or at later stage of the program
  • Allow use of devices that are not normally known to the driver
  • Allow debug enable, even at native level, without driver recompile
  • Allow for different drivers to support other devices that are not known now
  • Allow ports to be closed and reopened
  • Simpler and therefore easier to understand and use

There is a patched version

It solves a memory leak when opening the port and it explains the port enumeration better. You may not know but the name of a com port under windows is not as easy as it seems, the first nine ports are named COM1 … COM9 from then onward the name changes into \\.\com10 \\.\com11 and so on. To keep things simple the autodiscovery code stops at the port number 9, if you know that your system has more ports just use the correct name. (this did work even with the previous version)

The new version is released the 13jan2009 and you can download it from here (as usual source, jar, dll, javadoc). Everything is the same as the so called “new api”, this is just bug fix.

I leave the previous version online, just in case, if you want to try it out just download new API, source, jar, dll, javadoc, (a single 7zfile) unpack it, find the dll that is in the win32/jcomport directory and put it into the C:\WINDOWS, then you can just try it out by typing

java -jar jcomport.jar

To understand how to use it look at PortTester and use as a skeleton. If you are familiar with the old javx.comm API you will find many similarities, less code and more possibilities. Let me know what you think. Of course I wish to know about bugs and enhancements and if there is someone wishing for it I can write the Linux/UNIX version.

Download the package (OLD API)

Everything (click here to download) is in a single 7z file, the source, a ready made jar and a ready made dll and documentation on JNI and javacom

How do you use it ?

There are two pieces, really, the jar and the dll

  • get the jar that is in the java deploy directory and put it together with all the rest of jars to be loaded in your application
  • get the dll that is in the win32/jcomport directory and put it into the C:\WINDOWS

You can test it typing

java -jar jcomport.jar

The test should list the ports in your system and allow you to read/write. The initial parameters are 9600, 8bit, no parity

How do you change it ?

I suggest you give a look at the java code, it is simple and there are no tricks around. You will see that some parts like the management of ownership is not done, simply because I did not need it 🙂 You are free to add it if you feel like.

Bear in mind that declaring a port “free” in Java does not guarantee at all that some Windows processes did not grab it first..

I have also found out that the reliability of “signals” generated by various USB adapters/modems is not optimal. What surely works is reading/writing with timeout. All the rest…. well… it is Windows 🙂
Note that since I am using what Windows suggest to talk to COM ports, if some signals are not generated, well, it is not Java fault !

To compile the library you just need minGW, great stuff, I must say. Just download minGW (you do not really need MSYS, and download the ‘C’ development part (not c++). Set the PATH so you can have gcc anywhere you wish and then head for win32/jcomport In there you will find make-dll.bat that compiles the only source file that there is and even put it into the C:\WINDOWS directory

If you change the Java code, especially the native interface, you need to recreate the header file. Go into the java part of the winjcom and you will find a file called make-jni-header.bat. Note that most likely you have to change it to suit your needs (basically where java is and where winjcom is).

To compile the Java code you can use any decent IDE, for historical reasons I am used to Jdeveloper but there is nothing wrong with Eclipse and really, if you are also changing the ‘c’ source code that is probably the best option.

I hope you find it useful !

If you find some parts of the code that you do not like, feel free to change them as you please, you do not need to tell me or send changes back. Just one thing, if you find it useful, post a Blog entry on winjcom. I quite often have my own hard time to find stuff on Internet and it is really not fun to spend a couple of days working on something if there is the same solution already prepared somewhere around !


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s