Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > J2ME and serial port

Reply
Thread Tools

J2ME and serial port

 
 
caimaani
Guest
Posts: n/a
 
      04-13-2005
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?
 
Reply With Quote
 
 
 
 
Darryl Pierce
Guest
Posts: n/a
 
      04-16-2005
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 <(E-Mail Removed)>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
J2ME, MIDP 2.0, serial port control. Boki Java 0 05-31-2007 09:35 AM
Serial port on J2ME romain.alnet@gmail.com Java 1 08-02-2006 09:58 AM
J2ME serial port Cyber Java 1 05-12-2006 04:11 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
J2ME Memory Image Source & Serial port question Michael =?iso-8859-1?Q?K=FCbel?= Java 3 11-18-2003 05:04 PM



Advertisments