Question Serial port data representation

Dakta

Member
Joined
Dec 12, 2012
Messages
7
Programming Experience
Beginner
I'm a bit puzzled, wondering if anyone can offer advice on how best to visualize a solution to this.

Just for fun, I've been writing a program that will communicate with an embedded controller via a serial port. The controller contains callibration data which I would like to read, and perhaps even write - which the controller has it's own 'commandset' (quite similar to AT commands really).

I've written a program in vb.net which can communicate with the controller, no problems there, what I'm struggling with is how to interpret the data i'm receiving.

I'm handling all received data as a byte array, so in the serialport datareceived event, a byte array is incremented with the bytes received - when a command is sent to the controller, this array is cleared, so the array only contains the response to a specific command.

I wasn't sure how to convert a byte array to a string - so i googled it. It became clear I would have to encode the sequence, but after using the getstring method with all the encoding options (UT7/8/32/ASCII/Unicode) all I am getting if I try to display the string in a messagebox or textbox is an empty line.

Yet, if I search for a value in that string, the search is able to read the data within it -

VB.NET:
'RX_Buffer is byte array for received data

RX_BufferString = System.Text.Encoding.ASCII/Utf8/UTF7/Unicode.GetString(RX_Buffer) 'tried all encoding options

textbox1.text = RX_BufferString 'should display string in textbox however textbox remains empty

if RX_Bufferstring.contains("Hello") = true then msgbox("Hello was found in the string")  'messagebox DOES display

Does anyone know where I'm going wrong? I appreciate this is probably quite a n00b question...

cheers
Kris
 
The microcontroller's datasheet should tell you all you need to know, including packet structure and timings. 8-bit controllers tend to transmit data in binary form rather than readable text, so it might well be that the data you receive just cannot be represented as a string. Just for fun what bytes (hex value) are you receiving and what are they supposed to represent?
 
Hi mate, there are no datasheets (this is why this is a project) - actually I lie - the microcontroller is an infineon C167 but it forms part of an automotive controller so the code for PC->vehicle comms is kept within the industry.

I do know, because i've cracked elements of the comm protocol how data is received though - i thought like yourself it would come through as binary and not make sense if encoded, however it actually comes through as text - if I analyse the communication with a terminal program like 'termite' or hyperterminal, you can actually see the data -so if the binary data for one memory address I want to read was 0x00 (hex) it would transmit 00 as seen on a terminal program, not the ascii encoding of 00.

Does that make sense? It's mind blowing I know.

So an example data read from using a terminal from the controller would be, in (what i assume is ascii form):

13 8A 00 B2 C3

And what I'd have to do to collect that data is put it into a string, split it so each of those hex numbers was in a string of it's own and parse it.

However when i try to encode the received byte array, if i put that string into a textbox, the textbox seems empty, yet if I run a

'if string.contains("13 8A")' command, it returns true. So the string after encoded does contain that text, but the textbox, or messagebox, or whatever control i pass the string to display, just comes up blank? weird.



I really hope this makes sense, I'm struggling in my wording to describe the problem :(
 
That data is binary, it is not transmitted as a string containing "13 8A 00 B2 C3" it is transmitted as 5 bytes containing values 13, 8a, 00, b2, c3 (represented here in hex). The terminal program shows it like this because there is no other way to show binary data as a string. If you wanna do the same, just show each byte value passed through the Hex method (Dim myHexByte As String = Conversion.Hex(myByteArray(myByteIndex)))
 
thanks for the help

okay so if for example I do that for each element in my receive buffer, I end up with a lot of strings containing one hex byte. string1 = "13" string2 = "8a" etc

I really need the text representation of that - each hex value represents a letter which forms a serial code for the controller (all i'm trying to do is display the ID of the unit!)

Now, you're probably thinking 'aha all you have to do is forget the hex conversion, and encode the receive buffer byte array in it's entirety to ascii with something like:

encodedstring = system.text.encoding.ascii.getstring(receivebuffer)

which DOES work to an extent - if I search for the serial number in code, i.e if encodedstring.contains("SerialNo:3123") = true - when this 'if' is executed, it is found by the program

YET and this is the heart of the problem - if I try and put encodedstring into a textbox, or label, or msgbox, I get nothing, as if the string is empty.
 
Byte value 0 in strings is a string terminator, you will not see anything passed that value in encoded string. Ascii encoding does have a numeric character range that you can utilize to figure out which parts of the byte array that may be encoded as string.
 
Back
Top