VB6和VB.NET串行(COM1)数据错误
你好,
我是.NET的新手.我有一个在VB6中运行的应用程序.它通过COM1端口将文本发送到另一台设备.
当我将VB6代码转换为.NET时,它发出的字符串是不同的!我已经尝试了很多事情,但不知道该怎么办.我使用了串行数据分析器,发现两个字符串都不同,即使源代码相同.
VB6源代码:
Hello,
I''m new to .NET. I have an application that is running in VB6. It sends text through COM1 port to another device.
When I converted the VB6 code to .NET the string it sends out is different! I have tried so many things and don''t know what to do. I used a serial data analyzer and found the two strings are different even the the source code is the same.
VB6 source code:
MSComm1.CommPort = 1
MSComm1.Settings = "9600,O,8,1"
MSComm1.PortOpen = True
MSComm1.Output = Chr(&H2B) & _
Chr(&H4) & _
Chr(&H3) & _
Chr(&HE8) & _
Chr(&H0) & _
Chr(&H2) & _
Chr(&HF6) & _
Chr(&H71)
MSComm1.PortOpen = False
分析仪获取以下十六进制字符串:2B 04 03 E8 00 02 F6 71
VB.NET源代码:
The analyzer gets the following string in hex: 2B 04 03 E8 00 02 F6 71
VB.NET source code:
Dim Port As SerialPort = New SerialPort("COM1", 9600, Parity.Odd, 8, StopBits.One)
Port.Open()
Port.Write(System.Convert.ToChar(&H2B) & _
System.Convert.ToChar(&H4) & _
System.Convert.ToChar(&H3) & _
System.Convert.ToChar(&HE8) & _
System.Convert.ToChar(&H0) & _
System.Convert.ToChar(&H2) & _
System.Convert.ToChar(&HF6) & _
System.Convert.ToChar(&H71))
Port.Close()
分析仪获取以下十六进制字符串:2B 04 03 0F 00 02 3F 71
为什么字符串有区别?
请帮我改正...
感谢
The analyzer gets the following string in hex: 2B 04 03 0F 00 02 3F 71
Why is there a difference in the string?
Please help me correct it...
Thanks
我认为ToChar()
转换实际上将8位值转换为其等效的Unicode,从而创建了一个不同的值.尝试改用ToByte()
.
I think theToChar()
conversion actually converts the 8 bit value to its Unicode equivalent thus creating a different value. Try usingToByte()
instead.