Search code examples
vb.netserial-porthexreceiver

Read hex values as is when receiving data from serial


I have a system capable of sending hex commands as is, meaning that if I send A0 02 50 0E the receiver should receive A0 02 50 0E as a hex. It is now working fine, thanks to StackOverlfow.

My problem now is to receive hex data and convert it as string equivalent of that hex, it means that if I receive the hex A0 03 82 04 D7 my system can receive it as string of A0 03 82 04 D7.

This is my code: In this example I send a hex value of A0 03 82 04 D7

Dim s As String = ""
s += CStr(ComPort.ReadExisting)
'Just to check the output
Console.WriteLine("s: " & s) 'Outputs "s: ?P" instead of "s: P", this is my first problem

Dim result As String = Nothing
Dim c As Char = Nothing
For Each c In s
   Console.WriteLine("haha: " & c)

   Dim asd As String = Convert.ToUInt16(c).ToString("D2")


   Console.WriteLine("Uint16: " & Convert.ToUInt16(c).ToString("D2"))
   Console.WriteLine("Uint32: " & Convert.ToUInt32(c).ToString("D2"))
   Console.WriteLine("Uint64: " & Convert.ToUInt64(c).ToString("D2"))
   Console.WriteLine("Byte: " & Convert.ToByte(c).ToString("D2"))
   Console.WriteLine("Hex: " & c)

   result &= asd
Next
message_command(msg_usage_enum.serial_received_ascii, result)
Console.WriteLine(result) 'Output is 63028014

The result of the loop

haha: ?
Uint16: 63
Uint32: 63
Uint64: 63
Byte: 63
Hex: ?
haha: 
Uint16: 02
Uint32: 02
Uint64: 02
Byte: 02
Hex: 
haha: P
Uint16: 80
Uint32: 80
Uint64: 80
Byte: 80
Hex: P
haha: 
Uint16: 14
Uint32: 14
Uint64: 14
Byte: 14
Hex: 

The last code, which is Console.WriteLine(result) 'Output is 63028014 is partially correct, 028014 is the correct decimal value the only problem is the first 2 digits which is 63.

My approach here is to convert it first to decimal value, then from decimal to it's hex equivalent then make it string, but I'am not sure if it will work.

Edit: Thanks to Ian I am now having progress.

It's working now, Im just having a problem.Whenever I send A0038204D7 then my system receives 3F 03 3F 04 3F which is partially wrong 3F should be A0, 82 and D7, while sending A002500E my system receives 3F 02 50 0E which is correct except for 3F. It seems that my system can't process other so he outputs 3F.

This is the latest code I have so far

Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(ComPort.ReadExisting())

Dim strs As String() = (From b In bytes
                                        Select b.ToString("X2")).ToArray()
Dim str As String = String.Join(" ", strs)

Console.WriteLine("Result " & str)

This is the output in console.writeline:

Result 3F 02 50 0E

Solution

  • Use ToString("X2") for every byte you receive. You can use LINQ to do that, and String.Join to combine them back:

    Dim bytes As Byte() = {&HA0, &H3, &H82, &H4, &HD7}
    Dim strs As String() = (From b In bytes
                        Select b.ToString("X2")).ToArray()
    Dim str As String = String.Join(" ", strs)
    'Your result is in str