Home arrow static arrow Java Programming [Archive] - End of the inputStream
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - End of the inputStream
This topic has 5 replies on 1 page.

Posts:15
Registered: 6/15/04
End of the inputStream  
Jun 29, 2004 1:35 PM



 
I'm using an inputStream to receive char(s) from a serial device. The problem I am having is that I want to read the char(s) until the device stops sending char(s). Normally, you could read until there is nothing in the inputStream at which point read() returns -1. But with this device, I can make a never ending while loop and never receive -1. When there is nothing left to read, it just hangs up on the inputStream.read(). Any suggestions.
 

Posts:37,103
Registered: 3/30/99
Re: End of the inputStream  
Jun 29, 2004 1:56 PM (reply 1 of 5)



 
I'm using an inputStream to receive char(s) from a
serial device. The problem I am having is that I want
to read the char(s) until the device stops sending
char(s). Normally, you could read until there is
nothing in the inputStream at which point read()
returns -1. But with this device, I can make a never
ending while loop and never receive -1.

What do you mean? If you make this never ending while loop, then what does it mean for the device to "stop sending chars"?

The sender and receiver have to agree on some way to determine that the communication is done. If the sender never stops sending, then it's never done, right? The sender has to either first send the number of chars that will be sent, or send a special end-of-transmission char or close the stream. (Is it a socket? If so, the sender can call shutDownOutput() or close().) Or else the receiver couuld timeout after a while. (You might need the NIO stuff for this--not sure.)

Maybe you could post some code--not necessarily your whole app, just enough to show how you're sending and receiving and what the problem is regarding ending the transmission.

When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
 

Posts:15
Registered: 6/15/04
Re: End of the inputStream  
Jun 29, 2004 2:22 PM (reply 2 of 5)



 
This is the end of my input 48 48 48 53 49 13 10 13 10. The data comes in multiple lines. It is merely coincidence that two 13 10 (s) came in a row because there was just enough data to complete the row. My problem is that I have nothing to wait for to tell me that the transmission is over.
 

Posts:31,095
Registered: 4/30/99
Re: End of the inputStream  
Jun 29, 2004 2:36 PM (reply 3 of 5)



 
Well, if the design doesn't describe how you tell when there is no more data to come over a serial port, then no amount of programming can overcome that. The design needs to be fixed.

By the way when ASCII was invented over 40 years ago it had characters named SOT, STX, ETX, and EOT to bracket transmissions exactly because of this.
 

Posts:15
Registered: 6/15/04
Re: End of the inputStream  
Jun 29, 2004 2:41 PM (reply 4 of 5)



 
I think I will have to know exactly how much data I'm requesting and then base my reading off of that, a pain. I appreciate your help though.
 

Posts:1,111
Registered: 1/13/04
Re: End of the inputStream  
Jun 30, 2004 5:28 PM (reply 5 of 5)



 
It's up to you how you design your application.

But jverd probably gave the most sensible solution in the first response: For each response, have the server return lead-bytes that define the amount of data following.
 
This topic has 5 replies on 1 page.