问题描述
你好,
我有问题,我不知道如何将正确的方式写入串行端口十六进制代码.
我必须将以下内容写入串行端口.
& H02& H00& H09& H35& H32& H08& H99
我有一个演示程序,在那里我可以看到将要写入串行端口的代码.
发送:02 00 09 35 32 08 99
回答:06
发送:05
回答:02 00 04 035 32 08 59 03 53
我尝试执行以下操作:
SerialPort1.Open()
Hello,
I have a problem , i dont know how to write the correct way to the serial port Hex code''s.
I have to write the following to the serialport.
&H02 &H00 &H09 &H35 &H32 &H08 &H99
I have a demo programm and there i can see what codes will be written to the serial port.
Send : 02 00 09 35 32 08 99
Answer : 06
Send : 05
Answer : 02 00 04 035 32 08 59 03 53
I have tried to do the following :
SerialPort1.Open()
Dim code As String = ""
Dim receiveddata As Byte = 0
代码= Chr(& H2)+ Chr(& H0)+ Chr(& H9)+ Chr(& H35)+ Chr(& H32)+ Chr(& H8)+ Chr(& H99)
SerialPort1.Write(code)
receivedata = SerialPort1.ReadByte()''正常,我应该收到06,我收到21所以不正确.
我认为这是因为手册上的chr(& Hxx)发送了错误的代码,例如说发送0x02 0x00 0x09 0x35 0x32 0x08 0x99
所以我认为我的系统可能不正确.
谢谢.
最好的问候
Didier
code = Chr(&H2) + Chr(&H0) + Chr(&H9) + Chr(&H35) + Chr(&H32) + Chr(&H8) + Chr(&H99)
SerialPort1.Write(code)
receiveddata = SerialPort1.ReadByte() '' Normal i should receive 06 , i receive 21 so it is not correct.
I think that i send the wrong code this becaude of the chr(&Hxx) on the manual it says for example send 0x02 0x00 0x09 0x35 0x32 0x08 0x99
So i think that maybe my system is not correct.
Thanks.
Best regards
Didier
推荐答案
receiveddata = SerialPort1.ReadByte()
您收到了21,这是NAK(十六进制15)的Ascii十进制版本.这表明设备不理解您要发送的内容.要接收十六进制值的回复,您可以执行以下操作:
You received a 21, this is Ascii decimal version of NAK (Hex 15). This indicates the device does not understand what you are sending it. To receive the reply as a hex value you can do this:
receiveddata = Hex(SerialPort1.ReadByte())
要将数据字符串发送到设备,我将使用:
For sending a data string to the device I would use:
SerialPort1.WriteLine(code)
问候
WriteLine将换行符追加到字符串,因此使用起来可能不太好.
使用Write分别发送每个邮件.
我将检查0x99代码,这是扩展的ascii格式的一部分,当我将其发送给命令时,它读取的内容有所不同,其余的看起来都不错.再次查看设备手册.
Regards
WriteLine appends a line feed to the string, so maybe not so good to use.
Send each one out individually using Write.
I would check on the 0x99 code, that is part of the extended ascii format and when I send that out the comport it reads something different all the rest look OK. Check the device manual again .
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Encoding = System.Text.Encoding.GetEncoding(28605)
'Then for your output array
'(Note I am ussing an array here this will only work with integers)
Dim arCommand(6) As Int16
arCommand(0) = &H2
arCommand(1) = &H0
arCommand(2) = &H9
arCommand(3) = &H35
arCommand(4) = &H32
arCommand(5) = &H8
arCommand(6) = &H99
Dim x As Int16 'This one and the array should be declared at the class level
For x = 0 To 6
SerialPort1.Write(Convert.ToChar(arCommand(x)))
Next
有关编码功能的完整列表,请参见此MSDN链接.
http://msdn.microsoft.com/en-us/library/system. text.encoding.getencodings.aspx [ ^ ]
好吧迪迪埃,请给这5个票投票.
问候
For a complete list of encoding capabilities see this MSDN link.
http://msdn.microsoft.com/en-us/library/system.text.encoding.getencodings.aspx[^]
Alright Didier, Vote this one a 5 please.
Regards
这篇关于对于串行连接的RFID读卡器,将十六进制值写入串行端口的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!