2

I would like to send hex commands to my device because it can only understand hex.

Because of that I manage to create a function that can validate if the users input which is a string has a valid corresponding hex. The question is here.

So, by validating if the users input has a corresponding hex equivalent I am confident that what my system sends will be read by my device. By searching I realized that it needs to be converted to bytes, it states

Use the ASCIIEncoding class to convtert strings to an array of bytes you can transmit.

Code:

Dim str as String = "12345678"
Dim bytes() as Byte = ASCIIEncoding.ASCII.GetBytes(strNumbers)
' Then Send te bytes to the reader
sp.Write(bytes, 0, bytes.Length)

You do not need to covert the values to HEX, in this case HEX is mearly a different way of displaying the same thing.

My code:

'This is a string with corresponding hex value
Dim msg_cmd as string  = "A0038204D7" 
'Convert it to byte so my device can read it
Dim process_CMD() As Byte = ASCIIEncoding.ASCII.GetBytes(msg_cmd) 
'Send it as bytes
ComPort.Write(process_CMD, 0, process_CMD.Length) 

My Output:

41 30 30 33 38 32 30 34 44 37

Desired output:

A0 03 82 04 D7
Community
  • 1
  • 1
Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60
  • Is the only way to talk to the device thru some command line tool? Isnt there an API where you can send actual bytes? – Ňɏssa Pøngjǣrdenlarp Feb 23 '16 at 01:24
  • There is an API, I'm using [Terminal](https://sites.google.com/site/terminalbpp/) as the receiver to test if what my system sends is really hex or not. – Cary Bondoc Feb 23 '16 at 01:26
  • 1
    So you want it to send the hex digits as a string? If so, which encoding do you want to use for the string? ASCII? If you don't want to send it as a string, then you are totally confused, because that's what hex is. Hex is a string representation of a numeric value. – Steven Doggart Feb 23 '16 at 01:35
  • `it can only understand hex` It wont know the difference between a *byte* value of `16` decimal and `&H10` because they are the same value. – Ňɏssa Pøngjǣrdenlarp Feb 23 '16 at 01:37
  • 2
    A number is a number is a number. It doesn't matter how you *represent* the number, the numeric value of it is the same. For instance, the numbers 255 in decimal, "Two hundred fifty five" in english, 11111111 in binary, FF in hex, and 两百五十五 in Chinese are all the same number. They are just written in a different form, but the value is exactly the same. – Steven Doggart Feb 23 '16 at 01:40
  • Sorry, I'm so confused.I think I really need to spend more time understanding that. That's why I have a feeling that I'm missing something here. – Cary Bondoc Feb 23 '16 at 01:50

1 Answers1

4

To send a specific sequence of bytes, don't send a string--just send the bytes:

Dim process_CMD() As Byte = { &HA0, &H03, &H82, &H04, &HD7 }
ComPort.Write(process_CMD, 0, process_CMD.Length)

As I mentioned in the comments above, the values are just numeric values. There is nothing special about hex. Hex is just another way to represent the same values. In other words, the code above does exactly the same thing as this:

Dim process_CMD() As Byte = { 160, 3, 130, 4, 215 }
ComPort.Write(process_CMD, 0, process_CMD.Length)

If you have the hex digits in a string, you can convert a string representation of a hex number to a byte value by using the appropriate overload of the Convert.ToByte method. However, that only converts one byte at a time, so, first you need to split the string into bytes (two hex digits per byte. For instance:

Dim input As String = "A0038204D7"
Dim bytes As New List(Of Byte)()
For i As Integer = 0 to input.Length Step 2
    bytes.Add(Convert.ToByte(input.SubString(i, 2), 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)

However, it would be easier if the string had spaces between the bytes. Then you could just use the String.Split method:

Dim input As String = "A0 03 82 04 D7"
Dim hexBytes As String() = input.Split(" "c)
Dim bytes As New List(Of Byte)()
For Each hexByte As String in hexBytes
    bytes.Add(Convert.ToByte(hexByte, 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)

Or, more simply:

Dim input As String = "A0 03 82 04 D7"
Dim process_CMD() As Byte = input.Split(" "c).Select(Function(x) Convert.ToByte(x, 16)).ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105