Determine the byte/buffer length iOS app
Moderator: sergey
Determine the byte/buffer length iOS app
I have used the airconsole SDK to write an app but I cannot figure out how to determine the correct byte count or buffer count. The code provided in the idk works but with a buffer length of 1024 some of the strings in "NSString s" are joined as if there is a null value between the strings. How die session class work and how can I determine the buffer or byte length before populating it into a string. Alternatively everything can be read and then added to a string.
Re: Determine the byte/buffer length iOS app
Hello,
The SDK example assumes that the byte stream sent by the serial device is pure text data with no nulls, strange codes, etc. It's not a bullet-proof example as the comments in the code indicate; it's more for demonstration purposes.
To answer your questions:
- when bytes are received on the serial port you get a callback "readBytesAvailable" with a count parameter. The count parameter indicates how many bytes are currently available to be read from the serial port at this instant.
- your code calls [AirconsoleSession read] and passes in a buffer of your choosing - the return code indicates how many bytes were transferred into your buffer. You choose the size of this buffer according to your needs; the SDK will fill it up as much as possible. The SDK example uses a fixed 1024 byte buffer, but this of course could be dynamically allocated to match the "count" parameter passed to readBytesAvailable.
- you now have a buffer of bytes you can use, if you want to convert this to a string, then you'll need to know what format your strings are in - e.g. what character encoding, and what line termination (if any). This shouldn't be a mystery as you know the type of serial device you have connected the Airconsole to. The SDK example assumes, a.) UTF-8 character encoding, and b.) no line termination - data is a stream of characters.
You may want to look at some articles such as http://stackoverflow.com/questions/2793 ... ted-string for help converting bytes into NSString objects.
Hope that helps
The SDK example assumes that the byte stream sent by the serial device is pure text data with no nulls, strange codes, etc. It's not a bullet-proof example as the comments in the code indicate; it's more for demonstration purposes.
To answer your questions:
- when bytes are received on the serial port you get a callback "readBytesAvailable" with a count parameter. The count parameter indicates how many bytes are currently available to be read from the serial port at this instant.
- your code calls [AirconsoleSession read] and passes in a buffer of your choosing - the return code indicates how many bytes were transferred into your buffer. You choose the size of this buffer according to your needs; the SDK will fill it up as much as possible. The SDK example uses a fixed 1024 byte buffer, but this of course could be dynamically allocated to match the "count" parameter passed to readBytesAvailable.
- you now have a buffer of bytes you can use, if you want to convert this to a string, then you'll need to know what format your strings are in - e.g. what character encoding, and what line termination (if any). This shouldn't be a mystery as you know the type of serial device you have connected the Airconsole to. The SDK example assumes, a.) UTF-8 character encoding, and b.) no line termination - data is a stream of characters.
You may want to look at some articles such as http://stackoverflow.com/questions/2793 ... ted-string for help converting bytes into NSString objects.
Hope that helps
Who is online
Users browsing this forum: No registered users and 11 guests