aspose file tools*
The moose likes I/O and Streams and the fly likes problem reading data from serial port Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem reading data from serial port" Watch "problem reading data from serial port" New topic
Author

problem reading data from serial port

Cuneyt Taskiran
Greenhorn

Joined: Sep 18, 2008
Posts: 11
Hi,
I am trying to write a simple program to read data from the serial port coming from a closed-caption decoder. I first tried the SimpleRead.java example, but I modified it somewhat:


When I run this, I get the output:

Devel Library
=========================================
Native lib Version = RXTX-2.0-7pre2
Java lib Version = RXTX-2.0-7pre2
Found port: /dev/ttyS0
have 0 bytes

I verify (but displaying the captions on TV) that data is coming from the decoder. I am puzlled about the facts that
  • inputStream.available() returns 0 although there is data available. I know this because when I just do int c = inputStream.read() I continuously receive characters.
  • I only see a single "have 0 bytes" message, i.e. DATA_AVAILABLE event happens once.

  • Any ideas?

    Thanks,

    Cuneyt
    Cuneyt Taskiran
    Greenhorn

    Joined: Sep 18, 2008
    Posts: 11
    OK, now I'm really confused. I modified the reading part as follows

    When I run this, I get

    ...
    nBytes = 0
    read D
    nBytes = 0
    read
    nBytes = 0
    read H
    nBytes = 0
    read
    nBytes = 0
    read I
    nBytes = 0
    read S
    ... and it goes on

    Can anyone explain
  • [list] How inputStream.read() returns a character while inputStream.available() returns zero
  • How come inputStream never gets empty. I would have expected to see an "out of data" message from the IOException. Why doesn't this occur?


  • Thanks,

    Cuneyt
    Bobes Calin
    Greenhorn

    Joined: Nov 13, 2008
    Posts: 14


    of course it run and run......because you have



    and no breaking point. You need a break statement or modify the boolean value true.

    Cheers!
    Cuneyt Taskiran
    Greenhorn

    Joined: Sep 18, 2008
    Posts: 11
    Yes, the while loop is continuous but that was precisely my question. I was expecting the while loop to read all characters in the inputStream after which inputStream.read() would throw an exception. However, that doesn't happen. How is the inputStream replenished for a single DATA_AVAILABLE task?
    vipin jos
    Greenhorn

    Joined: Nov 18, 2008
    Posts: 24
    Check the following code snippet...
    Once we get to a case of DATA_AVAILABLE, we need to read until available() > 0
    Hope this helps

    public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
    case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[11];
    char[] data = new char[100];
    ctr++;
    int numBytes = 0;
    try {
    while (inputStream.available() > 0) {
    numBytes = inputStream.read(readBuffer);
    complete+=(new String(readBuffer));
    }

    } catch (IOException e) {}
    break;
    }
    Cuneyt Taskiran
    Greenhorn

    Joined: Sep 18, 2008
    Posts: 11
    Thanks for the suggestions guys. Vipin, as I was saying (my second post), the mysterious thing is that inputStream.available() returns 0 although there is data available in the port (I can inputStream.read() it). So I cannot use your solution.

    I am at a loss. Any other suggestions would be much appreciated.

    Thanks,

    C
    Cuneyt Taskiran
    Greenhorn

    Joined: Sep 18, 2008
    Posts: 11
    I have found a couple of posts that mention the same problem, i.e. inputStream.available() always returning zero, check out
    http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&thread.id=4710
    http://www.javareference.com/mvnforum/viewthread?thread=606

    The Javadoc for this method says:

    The available method for class InputStream always returns 0.
    This method should be overridden by subclasses.


    So, it seems that using available() is not recommended (how come it's used in all serial port reading example, though?). A better way is to just read from the stream (and check the return value) or use a input stream reader. I will look into these next.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: problem reading data from serial port
     
    Similar Threads
    URGENT - converting bytes to Strings
    Pooling Serial port
    Java Comm API
    how can I send the output of a servlet to a jsp page?
    Communicating over a virtual COM port