Embedded Freaks..

August 12, 2008

Serial port event in rxtx

Filed under: java — Tags: , , — kunilkuda @ 6:26 am

So, what do you expect from serial port event ? Most probably is the notification whether you’ve got new data in the buffer or not. But for some people (like me) who deals with real modem (I use GSM modem for server communication), we need RTS/CTS notification as well. Otherwise, we will flood the modem with data, since most PCs are faster than modem.

I’ve tested some of rxtx serial port events in Linux, and here’s some report that you might be interested.

The Theory

To handle serial port’s event, you’ll need a class that implements SerialPortEventListener. The signature for the class is like this:

class SerialListener implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        // do your event handler stuff here

To determine which kind of serial event that you want to handle (ie. new data received in the buffer, CTS raised, parity error, etc), check the SerialPortEvent documentation. Here’s some example of how to check whether there’s new data on buffer or not:

public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
        case SerialPortEvent.DATA_AVAILABLE:
            System.out.println("Data available");

After that, as usual, if you have event handler, you have to register your handler to the library. Like this:

serialPort.addEventListener(new SerialListener());

serialPort is the SerialPort’s object, while SerialListener is a class that implements SerialPortEventListener. Anyway, this event listener is a bit different, since you have to describe what kind of event that you want to handle. Like this (for example):


If you want to check other event, other than notify when data available in buffer, check out the SerialPort’s documentation here.

And that’s the end of the theory..Let’s see on what I’ve found during testing.

The Test Results

  • There is no new data event notification (my mistake in testing application..sorry)

This is true if you use USB-serial converter, under Linux. It has been reported here, but we cannot change it since it is linux’s usbserial driver problem. If you use normal serial port (with /dev/ttyS0 or /dev/ttyS1), this event is reported.

  • CTS event works like a charm

No matter if you use USB-serial or normal serial port, this event works like a charm. Here’s a code pieces to make it work:

// Enable CTS event

..lots of code

// The CTS event handler
switch (event.getEventType()) {
    case SerialPortEvent.CTS:
        System.out.println("CTS changed");
        if (serialPort.isCTS()) {
            System.out.println("CTS asserted");
        else {
            System.out.println("CTS de-asserted");
  • DSR event works perfectly

I need DSR to check whether the modem is ready or not to operate. Using my testing program, I confirm that it works perfectly, using USB-serial and normal serial port.


The event is suppose to be fired if the outputstream buffer is empty, but it is system dependable (not available in all of the platform). Not sure about the other platform, but rxtx on Linux obviously doesn’t have this event. It would not be triggerred. Futhermore, enabling this event will make other events hang.

  • DATA_AVAILABLE event works like level triggered interrupt

Simply means that this event will trigger as long as the data from serial port has not been read.


  1. Thanks for the response – I’m doing it on windows (cause I’m lazy – or really cause I’ve got windows on my laptop). I’ve got an SLES box I’ll give it a go on that see what happens. If this turns out to be a windows issue thats a major bummer as that is of course (one of) the big plus(es) to rxtx vs. sun’s crappy library.

    Comment by arduino and rxtx guy — August 22, 2008 @ 11:45 pm

  2. we are trying to detect the RS232 RI
    (Ring Indicator) by;


    where serialPort is an instance of the RxTxPort.
    But the rings are not detected.
    The OS is Windows XP Service Pack 2.
    Any ideas pls?.
    Thanking in advance,

    Comment by sanjaya — November 11, 2008 @ 2:50 pm

  3. It suppose to be:

    To test it, apply +9V on RING INDICATOR pin.

    If you test it with real modem, make sure serial cable has all the wires attached (instead of just RX, TX, and GND pin) and the modem does assert the pin.

    Most of modern modem don’t support this line anymore, unless you’ve configured them to do so (using AT command).

    Comment by kunilkuda — November 12, 2008 @ 9:19 am

  4. Thanks for the response. i’ll try it.

    Comment by sanjaya — November 17, 2008 @ 10:52 am

  5. Hi,
    After following your advice, now it’s working
    like a charm!
    Thank you very much…..!

    Comment by sanjaya — November 17, 2008 @ 5:30 pm

  6. […] ago Cloud Computing Event Announced in Newcastle First saved by zaclurve | 11 days ago Serial port event in rxtx First saved by lordashram | 12 days ago Back from “Pooh Camp” First saved by jimray | 13 […]

    Pingback by Recent Faves Tagged With "event" : MyNetFaves — February 1, 2009 @ 7:20 pm

  7. Thanks for some brilliant articles!

    I’m struggling with a event driven reading from one of my units that constantly sends data to my computer through RS-232. Reading is all fine, but I am not able to send data to it while my readerevent is running?

    If I just use the app to send data to a terminal, it all works fine!

    Any ideas?

    Comment by George.F — June 16, 2009 @ 8:43 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: