[Rxtx] read() not blocking?

Trent Jarvi tjarvi at qbang.org
Wed Sep 27 18:08:37 MDT 2006


On Sun, 24 Sep 2006, Knute Johnson wrote:

> So I've redone my tests and specifically set the receive timeout and
> receive threshold states.
>
> receiveTimeoutDisabled() and receiveThresholdDisabled()
>
> import java.io.*;
> import java.text.*;
> import java.util.*;
> import gnu.io.*;
> //import javax.comm.*;
>
> public class test {
>    public static void main(String[] args) throws Exception {
>
>        CommPortIdentifier cpi =
> CommPortIdentifier.getPortIdentifier("COM1");
>        SerialPort sp = (SerialPort)cpi.open("test",2500);
>
>        // set serial port parameters
>        sp.setSerialPortParams(9600,SerialPort.DATABITS_8,
>        SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
>        sp.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
>
>        // both receive timeout and receive threshold disabled
>        sp.disableReceiveTimeout();
>        sp.disableReceiveThreshold();
>
>        System.out.println("isReceiveThresholdEnabled: " +
>         sp.isReceiveThresholdEnabled());
>        System.out.println("isReceiveTimeoutEnabled: " +
>         sp.isReceiveTimeoutEnabled());
>
>        InputStream is = sp.getInputStream();
>
>        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
>        System.out.println("Started "+sdf.format(new Date()));
>
>        while (true) {
>            int n = is.read();
>            System.out.println(sdf.format(new Date())+" "+n);
>        }
>    }
> }
>
> C:\com\knutejohnson\redrock\race>java test
> Stable Library
> =========================================
> Native lib Version = RXTX-2.1-7
> Java lib Version   = RXTX-2.1-7
> isReceiveThresholdEnabled: false
> isReceiveTimeoutEnabled: false
> Started 12:06:48
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
> 12:06:48 -1
>
> Note that it returns very quickly from the read, many times per
> second.  This should block yes?
>
> Next is:
>
> enableReceiveTimeout(Integer.MAX_VALUE)  disableReceiveThreshold()
>
> import java.io.*;
> import java.text.*;
> import java.util.*;
> import gnu.io.*;
> //import javax.comm.*;
>
> public class test {
>    public static void main(String[] args) throws Exception {
>
>        CommPortIdentifier cpi =
> CommPortIdentifier.getPortIdentifier("COM1");
>        SerialPort sp = (SerialPort)cpi.open("test",2500);
>
>        // set serial port parameters
>        sp.setSerialPortParams(9600,SerialPort.DATABITS_8,
>        SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
>        sp.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
>
>        // both receive timeout and receive threshold disabled
>        sp.enableReceiveTimeout(Integer.MAX_VALUE);
>        sp.disableReceiveThreshold();
>
>        System.out.println("isReceiveThresholdEnabled: " +
>         sp.isReceiveThresholdEnabled());
>        System.out.println("isReceiveTimeoutEnabled: " +
>         sp.isReceiveTimeoutEnabled());
>
>        InputStream is = sp.getInputStream();
>
>        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
>        System.out.println("Started "+sdf.format(new Date()));
>
> 		while (true) {
>            int n = is.read();
>            System.out.println(sdf.format(new Date())+" "+n);
>        }
>    }
> }
>
> C:\com\knutejohnson\redrock\race>java test
> Stable Library
> =========================================
> Native lib Version = RXTX-2.1-7
> Java lib Version   = RXTX-2.1-7
> isReceiveThresholdEnabled: false
> isReceiveTimeoutEnabled: true
> Started 12:09:11
> 12:09:51 -1
> 12:10:31 -1
> 12:11:11 -1
> 12:11:51 -1
> 12:12:32 -1
> 12:13:12 -1
>
> Note the approximately 40 second timeout when Integer.MAX_VALUE
> should yield about 562 hours.
>
> I did just find something interesting, a timeout setting of 65535
> gave a 1:26 timeout.  I'm trying 3,600,000 now to see if it will be
> close to an hour.
>
> Any suggestions of what to try next would be great.
>
> Thanks,
>
>
Hi Knute

This does appear to be a bug in the windows version.  I did confirm th$ 
the timeout is not behaving as expected on w32.  The Linux binaries do not 
display the problem.

I guess many people do like I do in reality and implement their own 
timeout logic in their applications.

I tried both int n = read() and read(byte[]).  They both are returning 
right away which was not expected.  The read() is obviously more of a 
problem returning 255/-1.  There must be a mistake in the src/termios.c 
code.

You might be able to get around the problem with your own code using the 
byte[] version of read with your own timeout logic.  You could also read 
only when bytes are available (event).  You can also check on the number 
of bytes available before the read.

We will have to fix this bug for the next version of rxtx but thats not 
going to help with whats already there.  If you spot the fix before me, 
please send it to the list.  Otherwise I'll try to get to it as time 
allows on the weekends.

--
Trent Jarvi
tjarvi at qbang.org



More information about the Rxtx mailing list