Vb.net 使用SerialPort.ReadExisting()从串行端口接收无效数据
最近,我们连接了一个蓝牙设备,该设备从串行设备接收数据。蓝牙设备进一步将此数据传输到启用蓝牙功能的Windows Mobile。整个系统的波特率为19200,有7个数据位和1个停止位。当我们通过蓝牙设备向串行设备发送命令时,它接受命令并做出相应的响应。但字符串的某些数据字节被显示为问号(?) 但如果我们将串行设备直接连接到PC,则接收到的字符串是正确的 我在windows mobile中运行的vb.net程序的代码如下:Vb.net 使用SerialPort.ReadExisting()从串行端口接收无效数据,vb.net,windows-mobile,Vb.net,Windows Mobile,最近,我们连接了一个蓝牙设备,该设备从串行设备接收数据。蓝牙设备进一步将此数据传输到启用蓝牙功能的Windows Mobile。整个系统的波特率为19200,有7个数据位和1个停止位。当我们通过蓝牙设备向串行设备发送命令时,它接受命令并做出相应的响应。但字符串的某些数据字节被显示为问号(?) 但如果我们将串行设备直接连接到PC,则接收到的字符串是正确的 我在windows mobile中运行的vb.net程序的代码如下: Imports System Imports System.IO.Port
Imports System
Imports System.IO.Ports
Imports System.Windows.Forms.TextBox
Public Class frmSelectComPort
Dim WithEvent port1 as serialport = _
New SerialPort(“Com2”,19200,Parity.Even, 7, StopBits.One)
Private Sub MnuConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuConnect.Click
If port1.IsOpen Then
port1.Close()
End If
port1.DtrEnable = True
port1.RtsEnable = True
port1.ReceivedBytesThreshold = 1
port1.Open()
Dim str1,strcmd,strReadSegment0 As String
Str1="09RD000001"
strchksum=23
strcmd = New String(Chr(2) + str1 + Chr(3) + strchksum) + Chr(0)
Delay(5000)
port1.Write(strcmd, 0, strcmd.Length)
System.Threading.Thread.Sleep(70)
Delay(2000)
strReadSegment0 = port1.ReadExisting
‘here I receive the following string "?09?D03?A D?
End Sub
Private Sub Delay(ByVal num As Double)
Dim i As Double
For i = 0 To num
Next
End Sub
End Class
给串行端口的命令是“09RD000001 23
串口给出的响应为?09?D03?A D
但我希望从串行端口获得以下输入:“09RD033A DA
请尽快提供解决方案您是否尝试过8个数据位、无奇偶校验、1个停止位?只要两端设置相同 ?表示需要设置编码
Dim myEnc As Encoding = Encoding.GetEncoding("Windows-1252")
port1.encoding=myEnc