我想发送hex
命令到我的设备,因为它只能理解hex
。发送字符串到它的十六进制等效
正因为如此我设法创建一个可以验证,如果用户输入这是一个string
有有效相应hex
功能。问题是here。
因此,通过验证users input
是否具有相应的hex
等价物,我确信我的系统发送的内容将被我的设备读取。 By searching我意识到,这需要被转换为字节,它指出
使用ASCIIEncoding类convtert字符串字节 的阵列可以传送。
代码:
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)
你并不需要的值隐蔽,以十六进制,在这种情况下HEX是 mearly显示同样的事情不同的方式。
我的代码:
'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)
我的输出:
41 30 30 33 38 32 30 34 44 37
所需的输出:
A0 03 82 04 D7
是交谈的设备将向您介绍一些命令行工具的唯一途径?没有一个API可以发送实际字节? – Plutonix
有一个API,我使用[Terminal](https://sites.google.com/site/terminalbpp/)作为接收器来测试我的系统发送的内容是否真的是十六进制。 –
所以你想要它发送一个字符串的十六进制数字?如果是这样,你想使用哪种编码的字符串? ASCII?如果你不想将它作为字符串发送,那么你完全困惑,因为这就是十六进制。十六进制是一个数值的字符串表示。 –