[prev in list] [next in list] [prev in thread] [next in thread] 

List:       rxtx
Subject:    Re: 100% cpu usage
From:       Trent Jarvi <taj () hex ! linuxgrrls ! org>
Date:       2002-07-08 23:34:14
[Download RAW message or body]



On Mon, 8 Jul 2002, Baldur Norddahl wrote:

> Hi,
>
> I have a small program that uses a simple while loop to read from the serial
> port. It works fine, but always uses 100% cpu.
>
> The program uses a loop like this:
>
>   while(true)
>   {
>     int nextValue = in.read();
>     cat.debug("input thread got "+nextValue);
>     if (nextValue == -1) break;
>     // code to handle the input
>   }
>
> >From the debug output you can tell that it is waiting inside the read()
> function as it is supposed to. So it has to be read() that is using the cpu
> time.
>
> The port is initialized as follows:
>
>   CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
>   SerialPort port = (SerialPort) identifier.open("SMSC Server",1000);
>   port.setSerialPortParams(57600,port.DATABITS_8,port.STOPBITS_1,port.PARITY_NONE);
>   port.setDTR(true);
>   InputStream in = port.getInputStream();
>   OutputStream out = port.getOutputStream();
>
> I am using:
>
> java version "1.4.0"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
> Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
>
> And rxtx-1.5-9pre7.
>
> Any idea what could be wrong?
>

You can try a few things.  My first though is you should be using the
events unless latency is the primary concern.  The SimpleRead.java demo
that comes with Sun's commapi is a good example of doing this.  See how
they handle the DATA_AVAILABLE events.

You could also try looking into the timeout and threshold to make sure
the read is blocking until data is recieved.

If none of this is helping, You can try placing System.sleep() in the
loop (5 MSec should be safe).  usleep() is the native version if you want
to slow things down in the native read().

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic