简体   繁体   English

使用SerialPort.ReadExisting()从串行端口接收无效数据

[英]Receiving Invalid Data From Serial Port using SerialPort.ReadExisting()

Recently we have connected one bluetooth device which receive data from our serial device. 最近,我们连接了一个蓝牙设备,该蓝牙设备从串行设备接收数据。 The bluetooth device further transmite this data to a Windows Mobile which is bluetooth enable. 蓝牙设备还将该数据传输到启用了蓝牙的Windows Mobile。 Baud rate of whole system is 19200 with 7 data bit and 1 stop bit.When we send command to serial device through bluetooth device it is accepting the command and respond accordingly. 整个系统的波特率为19200,有7个数据位和1个停止位。当我们通过蓝牙设备向串行设备发送命令时,它正在接受命令并做出相应的响应。 but some of data byte of string is desplay as question mark (?). 但是字符串的某些数据字节显示为问号(?)。

But in case we connect the serial device directly to PC, the string received is correct. 但是,如果我们将串行设备直接连接到PC,则接收到的字符串是正确的。

Code of vb.net program which I run in windows mobile is below: 我在Windows Mobile中运行的vb.net程序代码如下:

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

Command Given to Serial Port is " 09RD000001 23 给串口的命令是“ 09RD000001 23

Response given by Serial Port is "?09?D03?AD? 串行端口给出的响应是“?09?D03?AD?”。

But I Expect the following input from serial port : " 09RD033A DA 但我希望从串行端口输入以下内容:“ 09RD033A DA

Kindly provide the solution ASAP 请尽快提供解决方案

Have you tried 8 databits, no parity, 1 stop bit? 您是否尝试过8个数据位,无奇偶校验,1个停止位? As long as both ends are set the same... 只要两端设置相同...

The ? can indicate that the encoding needs to be set. 可以指示需要设置编码。

Dim myEnc As Encoding = Encoding.GetEncoding("Windows-1252")
port1.encoding=myEnc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM