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
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.
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.
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.
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.