Hex with Serial

HI

can someone help me with this problem?

I want to read some Hex values with the Serial port for further usage. My device is answering following data for example: 02 11 05 01 FF. I would like to read and display these values in a Textarea i called TA
In Serial Data available i have this line: TA.text=TA.text + DefineEncoding(Serial1.ReadAll(), Encodings.ASCII) . With this i can only print Ascii Text received from the Serial port, is that right? So how do i get it to print the data given above?
It would nice to have a routine like Serial.ReadAscii or Serial.ReadHex
Thanks for some help.
voy

Use EncodeHex

Hi Tim
This would result in 30323031303530314646. That is not what i need. I want 02 01 05 01 FF as a binary value.

Voy, if you need the data as binary values store the Serial1.ReadAll in a MemoryBlock and don’t use a string.

Then you want the converse function: DecodeHex.

What is the source of the “values”? Is it sending numeric data (as raw numbers) or is it sending ASCII data?

For example, the device might send an 8-bit integer value for the number 2 as 0x02 (raw number) or it might send it as 0x32 (the ASCII representation of the character “2”). Or, it might do several other things.

Jim Wagner
Oregon Research Electronics

I think that it is not clear what is it needed.
It seems that it needs both the binary value of the received data and also a string representing the binary value.
In the example none of the values it is a valid ascii printable character.

So:

dim mb as MemoryBlock = Serial1.ReadAll()
returns:
mb.size = number of bytes received -> 5
mb.byte(0) -> a byte value equals to 02
mb.byte(1) -> a byte value equals to 01 and so on…

and

dim s as string = EncodeHex(mb)
returns:
02010501FF

i.e. the string of 10 ascii chars representing the binary data in hex format.

Maybe this can help?

@ James

my devices sends raw data and not Ascii. Just to explain the data:
02 - two values are coming
01 - first value
05 - second value
01 - one value is coming
Ff - first value
08 - eight values coming
xx and so on
The devices stops sendind when it has sent all the data. When sending a command to the device i don’t know that how many bytes are to come.
I will give Maurizios suggestion a try.

Hi Voy,

you can use a MemoryBlock when you are dealing with binary values.
A MemoryBlock is a block of bytes where it’s up to you to give to this bytes a meaningful interpretation.
And a MemoryBlock can be used anywhere it’s required a string.
But be careful at the contents if you pass a MemoryBlock to something requiring an String i.e. an array of bytes representing valid printable chars.

If you are familiar with the C language a MemoryBlock it is very like what malloc() function gives you.

Thanks to all.