通过 RS232 发送大于 0x7F 的 Unicode 字符
Sending Unicode Characters greater than 0x7F through RS232
我的应用程序在 Windows CE 6.0 中使用 Compact Framework 并用于通过 RS-232 向设备发出远程命令。这些命令使用具有特定十六进制值的字节发送,例如发送 0x22 0x28 0x00 0x01 作为命令序列。我一次发送一个字节。十六进制值在内部存储在每个命令序列的字符串中,例如“22,28,00,01”。我正在使用以下代码发送字节。
Dim i As Integer
Dim SendString() As String
Dim SendByte, a As String
DutCommand = "22,0A,00,02,E7,83" 'Sample command string
SendString = Split(DutCommand, ",") 'Split the string
For i = 0 To UBound(SendString) 'Send each byte after encoding
SendByte = Chr(CInt("&H" & SendString(i)))
CommPort.Write(SendByte)
Next
即使对于大于 0x7F 的值,SendByte 也被正确编码,但发送的最后两个字节(0xE7 和 0x83)被发送为 0x3F,即“?”的 ASCII 代码。因为它大于 0x7F。
我是否缺少 Comm 端口处理编码的设置?有没有简单的方法发送值大于0x7F的数据?
我假设您正在使用 SerialPort.Write。
如果是这样,请注意文档中的内容:
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
看来解决方案已经很清楚了。您需要将 CommPort.Encoding
属性 设置为所需的值。
有关详细信息,请参阅 SerialPort.Encoding。
根据 the documentation 对于 SerialPort.Write
:
By default, SerialPort uses ASCIIEncoding to encode the characters.
ASCIIEncoding encodes all characters greater than 127 as (char)63 or
'?'. To support additional characters in that range, set Encoding to
UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
您还可以考虑 using the Write
overload 实际上只写入原始字节。
您只是忘记了将十六进制值转换为字节。它需要看起来像这样:
For i = 0 To UBound(SendString) 'Send each byte after encoding
Dim b = Byte.Parse(SendString(i), Globalization.NumberStyles.HexNumber)
CommPort.BaseStream.WriteByte(b)
Next
非字符串方式是:
Dim DutCommand As Byte() = {&H22, &H0A, &H00, &H02, &HE7, &H83}
CommPort.Write(DutCommand, 0, DutCommand.Length)
我的应用程序在 Windows CE 6.0 中使用 Compact Framework 并用于通过 RS-232 向设备发出远程命令。这些命令使用具有特定十六进制值的字节发送,例如发送 0x22 0x28 0x00 0x01 作为命令序列。我一次发送一个字节。十六进制值在内部存储在每个命令序列的字符串中,例如“22,28,00,01”。我正在使用以下代码发送字节。
Dim i As Integer
Dim SendString() As String
Dim SendByte, a As String
DutCommand = "22,0A,00,02,E7,83" 'Sample command string
SendString = Split(DutCommand, ",") 'Split the string
For i = 0 To UBound(SendString) 'Send each byte after encoding
SendByte = Chr(CInt("&H" & SendString(i)))
CommPort.Write(SendByte)
Next
即使对于大于 0x7F 的值,SendByte 也被正确编码,但发送的最后两个字节(0xE7 和 0x83)被发送为 0x3F,即“?”的 ASCII 代码。因为它大于 0x7F。
我是否缺少 Comm 端口处理编码的设置?有没有简单的方法发送值大于0x7F的数据?
我假设您正在使用 SerialPort.Write。
如果是这样,请注意文档中的内容:
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
看来解决方案已经很清楚了。您需要将 CommPort.Encoding
属性 设置为所需的值。
有关详细信息,请参阅 SerialPort.Encoding。
根据 the documentation 对于 SerialPort.Write
:
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
您还可以考虑 using the Write
overload 实际上只写入原始字节。
您只是忘记了将十六进制值转换为字节。它需要看起来像这样:
For i = 0 To UBound(SendString) 'Send each byte after encoding
Dim b = Byte.Parse(SendString(i), Globalization.NumberStyles.HexNumber)
CommPort.BaseStream.WriteByte(b)
Next
非字符串方式是:
Dim DutCommand As Byte() = {&H22, &H0A, &H00, &H02, &HE7, &H83}
CommPort.Write(DutCommand, 0, DutCommand.Length)