Converting TCP Serial Data

Hi Folks, I am working on an iOS project where I am receiving data over a TCPSocket in response to commands being sent. There are two types of data being received in response to a specific command. The first command returns typical ASCII text in which I successfully handle using the following:

[b]dim mBuffer As xojo.Core.MemoryBlock
dim buffer As Text

mBuffer = me.ReadData(AvailableData.Size)
buffer = TextEncoding.UTF8.ConvertDataToText(mBuffer)
View1(Globals.MainView).TextArea1.Text = buffer[/b]

The second command returns a string of ASCII data that I need to convert to integers and store in an array. The bytes are weighted and summed with every 4 bytes added to an UInt32 array. In a desktop app, I successfully do something similar to the following for each set of 4 bytes:

dim data_array()as UInt32
data_array.Append ascB(*16777216 + ascB(*65536 + ascB(*256 + ascB(

I am struggling with the second part in terms of converting the data in the iOS space. Any assistance with what that code may look like would be greatly appreciated.

@Sean Pons — You are struggling but what happens in detail? You don’t get the right values? (maybe an endianness problem) or do you get an error/crash?

Hi Stephane,

For the second command, I have the following line in DataAvailable:

buffer = TextEncoding.UTF16.ConvertDataToText(mBuffer)

… and for the moment, I have buffer to dump into a textarea just so I can see what is coming over. I get a list of Chinese characters.

UTF16 doesn’t fail but that encoding cannot be right. Using ASCII or UTF8 in the place of UTF16 causes: Runtime Exception: The data could not be converted to text with this encoding. No other encoding seems to work.

What I need is to read each byte as decimal ASCII and ultimately store in an integer array.

Thanks, Sean

@Sean Pons — Well, you are working with raw bytes in the second part so you absolutely don’t want to make any conversion and the TextEncoding should be nil (BTW, a string with a nil encoding will not be converted by Xojo). However, I am curious to have a hex-dump of the raw data.

Please give us the result of

EncodeHex( Serial1.ReadAll, true )

Stephane, it doesn’t appear that EncodeHex can be used in iOS. Item does not exist.

Thanks, Sean

you can write encode hex for yourself based on some code in this thread

On iOS memory block and string (text) are different.
On desktop you can switch from one to the other.

so keep your mBuffer as xojo.core.memoryblock and read directly the Uint32Value.

On serial you have a buffer (string/memory) and with read(1) you advance one byte for each read so you build you UInt32 byte by byte.
But if you do a read(4) and then from the string/memory you can get the UInt32Value in one step.

Very good Antonio, so this works:

do until i = mBuffer.Size

Thanks to all.