0

I'm new to JAVA programming. I was assigned with the task to write a JAVA GUI application that will interact with external devices via serial port. I've managed to make my code compile and run with jSSC (Java Simple Serial Connector) package under both Mac OS X and Win32 platform. The jSSC project provides decent sample codes and it's fairly easy to implement with javax.comm style SerialPortEventListener interface.

But soon I've learned that I'd need to add "ACK timeout" feature to my implementation, for error prevention. In C language this can easily be achieved via select() API call with 5th parameter set to desired duration dynamically. But in JAVA, I've no single clue how to do it.

Should I setup another time thread and fire timer-generated event by directly calling serialEvent() method ? It may lead to some racing condition though.


(03/27 10:43 Edit): OK, I've traced source code of jSSC. It seemed that jSSC just creates a new Runnable thread in JVM, when a Listener is registered to it; and that thread will continuously polling status with native function call, which will invoke system api calls like ioctl(). So it would be impossible to add timeout event without modifying the native library.

Guess that I have to change the way for serial port event handling. I'll need to create another Runnable thread to handle the incoming data process, instead of directly parsing data within serialEvent() method call from jSSC event thread. And I can setup a timer to throw timeout event into that thread.

I that case, I'd need to setup an event dispatching thread, just like SwingUtility.invokelater(). But I've no idea whether I should implement one from the bottom-up, or I can just use the AWT EDT to handle it.

Can anyone give me some advice ?

1

2 Answers 2

1

I have worked with java comm, never with jssc. JavaComm provide an open method were a timeout can be configured:

serialPort1 = (SerialPort) portId1.open("ComControl", 2000);

From jssc javadoc the open method doesn't manage a timeout mechanism.

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately, no. jSSC only provides openPort() method without parameter. I know that javax.comm had provided richer features, but my target platform is Win32, and there is no official javax.comm package available for that. I've also checked rxtx package, but it requires manually setup by end users, and that's unacceptable in my case.
@RichardLiu What do you mean RXTX requires manual setup by end-users? Actually, in RXTX serial library, there is a function called CommPortIdentifier.getPortIdentifiers() method to enumerate through available serial ports in a system. From here you can open each serial port and detect what kind of data it returns. If the data is the right one, you can stop the enumeration and start to use the correct serial port. For a good example: code.google.com/p/aima-java/source/browse/trunk/aimax-osm/src/…
@RichardLiu If you need another serial comm library, check PureJavaComm which also provides a way to time out sparetimelabs.com/purejavacomm/index.html
@ee I'm following the instruction of this document: rxtx.qbang.org/wiki/index.php/Installation_for_Windows. RXTX needs to install some .dll/.jar files into JRE home path. But my client had specified that he want something that can be directly executed from a USB flash drive, no other installer except for JRE. jSSC bundled all native library files within a single .jar, so I can distribute it within my project folder.
@ee JTermios looks promising, I'll try it. Thanks.
|
1

I am not familiar with jSSC, but when there is no possibility to pass a timeout through the API and you've to do it yourself, then ThreadMonitor from Apache may be of your interest.

Usage looks like this:

long timeoutInMillis = 1000;
try {
    Thread monitor = ThreadMonitor.start(timeoutInMillis);
    // do some work here
    ThreadMonitor.stop(monitor);
} catch (InterruptedException e) {
    // timed amount was reached
}

We are using RXTX here for serial port communication. We have barcode scanners connected via serial port and modems sending SMS to our admins when critical system states occur in monitoring. Works flawless.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.