• Post Reply Bookmark Topic Watch Topic
  • New Topic

Response from RS232 device is not complete?  RSS feed

 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!