Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2005

Tip: Looking for answers? Try searching our database.

J2ME and serial port

Thread view: 
caimaani - 13 Apr 2005 13:42 GMT
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?
Darryl Pierce - 16 Apr 2005 11:49 GMT
> 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

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.