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