Toggling RS232 RTS pin on WinXP machine using Java

*Hello Java-on-Windows gurus.

Is there a simple way, in Java, to access an RS232 port on a WinXP machine for the sole purpose of toggling the RTS pin?


It’s not as simple as using c/c++ and the win32 api - it’s been a while since Sun/Oracle have supported a Java serial communications API on Windows. But it can be done if you track down the necessary drivers/libraries - try starting with RXTX

If you’re not constrained to use Java, it’s probably simpler/faster to use the win32 api with c/c++, or the serialport api in .NET

Hope that helps…feel free to PM me if questions

  • Ron
    Team #2607 controls mentor

*Thanks, but… All I want to do is allocate an RS232 port, and toggle the RTS pin.

I don’t need a full serial port library with transmit and receive interrupts and buffers and XON/XOFF and RTS/CTS handshaking protocols and baud rate, parity, etc etc etc.

All I want to do is toggle the RTS pin. Isn’t there a simple way to do this?

Using Java on Windows, that is the simple way :slight_smile:

Joking aside, the issue is that serial comms isn’t part of the “core” Java libraries - it’s an extension - so isn’t part of the standard JDK distribution. That means that without the additional library/driver, you don’t have any way to access the serial port hardware in the Java runtime - even just to do something simple like toggle RTS.

That’s why you need the additional library/driver like RXTX. Even though you don’t need the full comms library functionality, you need it to get access to the serial port hardware.

  • Ron
    Team #2607 controls mentor

My question is this: why do I need a full comms library… why can’t I just do a Windows API call to allocate a serial port, and then another API call to toggle the RTS?

If the question seems naive, it’s because I know very little about Java, and I’ve never looked at the Windows API… this question is based on what I’ve done in other contexts with other APIs.



No problem, sorry my explanations are a little incomplete. This is one of those cases where a simple question yields a reasonably complex answer. :slight_smile:

You don’t have direct access to the Windows API in the out-of-the-box Java environment like you do in C, C++, etc. That’s because one of Java’s goals is platform-independence, and platform-independence means there are extra layers of abstraction between your java code and the operating system. Your java code can only access underlying “native” functions that the intervening layers expose for you via the Java API. This wikipedia article may help picture it a bit better.

To help illustrate further, instead of accessing the serial port let’s say you want to read a file in a Java program. You would use something like FileReader in the Java API to open and access the file. The implementation of the FileReader API on whatever platform you’re using (Windows, Linux, etc) takes care of mapping the Java API to whatever native functions are needed. That’s why each platform (Windows, Linux, etc) has it’s own JDK implementation - so your Java code can run across them without changing (at least in theory :wink: )

It so happens that FileReader is part of the out-of-the-box Java environment, so you don’t need to install anything extra to do file i/o in Java. However, that’s not the case with the serial port API (JavaComm). JavaComm isn’t included out-of-the-box, so you need an extra library (like RXTX) that implements JavaComm or a similar API on the platform you want to use.

As an alternative, there is also a general-purpose “native access” API called JNA that opens up direct access to native code. You could use that to directly access the Windows API functions you need (like EscapeCommFunction) provided you’re already familiar with the Windows API.

But the point is you still need some additional library (RXTX, JNA, etc) to do this in Java. And if you’re going to end up writing direct calls to the underlying Windows API at the end of the day anyway, it may be simpler to just use .NET, or C, etc.

Hope that helps, sorry for the long explanation. There’s lot’s of additional material available out there in the google-sphere as well.

  • Ron
    Team #2607 controls mentor