View Single Post
  #6   Spotlight this post!  
Unread 26-11-2011, 14:26
rrossbach rrossbach is offline
Registered User
AKA: Ron R
FRC #2607 (RoboVikings)
Team Role: Mentor
 
Join Date: Nov 2008
Rookie Year: 2008
Location: Warrington PA
Posts: 90
rrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to beholdrrossbach is a splendid one to behold
Re: Toggling RS232 RTS pin on WinXP machine using Java

Quote:
Originally Posted by Ether View Post
... 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.

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 )

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
__________________

FIRST Mid-Atlantic Volunteer
FRC Team #2607 Mentor
Reply With Quote