J2ME and serial port

Discussion in 'Java' started by caimaani, Apr 13, 2005.

  1. caimaani

    caimaani Guest

    Hi,

    I am developing J2ME class that listens the serial port and reads all
    available bytes from the buffer in a single read event. The number of
    bytes and the structure of the message may vary very much (read: they
    are undefined).

    At the time I'm using InputStream.read() method to read the first byte
    and to trigger InputStream.available() to find out how many bytes the
    buffer still contains before I read them all. However it seems not to
    be very reliable without some kind of delay because sometimes the
    bytes are written to buffer so slowly (althought another component
    does this in a single write event). So reading is triggered too
    quickly and available() returns too small number.

    How can I ensure that InputStream.available() gives me the real number
    of bytes? Is it mandatory to add some kind of delay?
     
    caimaani, Apr 13, 2005
    #1
    1. Advertising

  2. caimaani wrote:
    > At the time I'm using InputStream.read() method to read the first byte
    > and to trigger InputStream.available() to find out how many bytes the
    > buffer still contains before I read them all.


    Don't use InputStream.available(). Instead, keep reading from the input
    stream until it returns a value of -1 (EOF).

    <snip>

    > How can I ensure that InputStream.available() gives me the real number
    > of bytes? Is it mandatory to add some kind of delay?


    No, and there's no way to know for sure. Your best bet is to read data
    from the input stream and write that data to an instance of
    ByteArrayOutputStream. Then, when you've finished, you can retrieve that
    data as an array of bytes from the ByteArrayOutputStream. Here's an
    example of how to do it:

    public byte[] readData(InputStream istream) throws IOException
    {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new buffer[4096];
    int read = istream.read(buffer);

    while(read != -1)
    {
    baos.write(buffer,0,read);
    read = istream.read(buffer);
    }

    return baos.toByteArray();
    }

    HTH

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
     
    Darryl Pierce, Apr 16, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael =?iso-8859-1?Q?K=FCbel?=

    J2ME Memory Image Source & Serial port question

    Michael =?iso-8859-1?Q?K=FCbel?=, Nov 17, 2003, in forum: Java
    Replies:
    3
    Views:
    1,006
    Darryl L. Pierce
    Nov 18, 2003
  2. Cyber

    J2ME serial port

    Cyber, May 8, 2006, in forum: Java
    Replies:
    1
    Views:
    5,365
    Darryl L. Pierce
    May 12, 2006
  3. Serial port on J2ME

    , Aug 1, 2006, in forum: Java
    Replies:
    1
    Views:
    860
    Vincent van Beveren
    Aug 2, 2006
  4. Pom
    Replies:
    2
    Views:
    1,679
    Bas-i
    Jan 31, 2007
  5. Boki
    Replies:
    0
    Views:
    692
Loading...

Share This Page