Forums Register Login

Response from RS232 device is not complete?

+Pie Number of slices to send: Send
This is probably a common problem with reading responses from RS232 devices but I can't seem to find a concrete answer anywhere.
Basically I'm trying to get a response from a device when I send in a list of values from an array. E.g The array would contain a string for requesting the volume value from a device as well as microphone value as another value.

What seems to be happening from what I understand is there are not enough bytes available in the inputstream when a request is made so thats why the answer is in pieces. I.E printing out each line in the buffer returns:
Vol
080

Where it should be Vol080.

Here's is my class, ignore the parts with "oldvalues" and "parsedvalues" as thats code related to other parts of my code.

+Pie Number of slices to send: Send
 

Sean Michael Hayes wrote:What seems to be happening from what I understand is there are not enough bytes available in the inputstream when a request is made so thats why the answer is in pieces.



Yes, that sounds reasonable. So read the pieces until you have all of them, and put them back together. (Was that your question?)
+Pie Number of slices to send: Send
 

Paul Clapham wrote:

Sean Michael Hayes wrote:What seems to be happening from what I understand is there are not enough bytes available in the inputstream when a request is made so thats why the answer is in pieces.



Yes, that sounds reasonable. So read the pieces until you have all of them, and put them back together. (Was that your question?)



This is kinda embarassing but I'm not entirely sure how to go about doing that. Even a basic outline of what to do would help. I'm new to RS232.
+Pie Number of slices to send: Send
Well I realized my problem was that the inputstream is reading in the data immediately when it sees that the number of bytes available is greater than zero. Changing the size of my buffer array to the length of the value I want returned in bytes solved the problem. Problem. What if my values I return will be a different length in bytes each time and unknown in length? Will I have to look up the minimum and maximum possible size of the bytes returned each time I want a value returned?
+Pie Number of slices to send: Send
First of all you should design your communications protocol so that you don't have to ask those questions. If you have no idea when to stop reading then you have a design failure which can't be worked around by programming.

So either you have a rule which says "There are 38 bytes of data in this transmission" or you have a rule which says "First you read a byte, and the contents of that byte tells you how many more bytes to read" or you have a rule which says "Read bytes until you receive one which contains the ETX character" or some similar rules.

The buffer size tells you nothing, and neither does the number of bytes available. The device (and the software between it and your application) is free to clump the data together in groups of bytes in any way it sees fit.
+Pie Number of slices to send: Send
Well I took a different approach after some research and did this instead. I used a bufferedReader containing and InputStreamReader that in turn contains the inputStream I was reading from. Using its readline() method I was able to get the full line of bytes put out by the serialport without having to define a size or the like.


The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1100 times.
Similar Threads
NullPointerException: name can't be null
Problem with COMM API
how can I send the output of a servlet to a jsp page?
sending sms through gsm modem
Send SMS by using Serial Port
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:39:33.