• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem reading data from serial port

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic