从串行端口读取数据,但接收的数据被编码为显示系列的正方形 [英] Read Data From serial Port, but data received is coded as displays series of squares

查看:107
本文介绍了从串行端口读取数据,但接收的数据被编码为显示系列的正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友





我正在VB.NET 2010中编写程序,通过rs232端口从设备读取数据/>

Dear Friends


I am writing a program in VB.NET 2010 to read data from a device through rs232 port

btnStart.Enabled = False
btnStop.Enabled = True
If SerialPort1.IsOpen Then SerialPort1.Close()
SerialPort1.PortName = cmbPorts.Text
SerialPort1.BaudRate = cmbBaudRate.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits =  IO.Ports.StopBits.One
SerialPort1.DataBits =  8
SerialPort1.Encoding =  System.Text.Encoding.Unicode
SerialPort1.ReadBufferSize = 4096
SerialPort1.Open()





然后我有写下来读取数据如下





then I have written to read the data as follows

'Declared this delegate at top of form
Delegate Sub SetTextCallback(ByVal [text] As String) 

Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            ReceivedText(SerialPort1.ReadExisting())    
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
           If Me.TextBox1.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.TextBox1.Text &= [text]
        End If
    End Sub

推荐答案

更改是您接收的数据不是直字符,而是二进制值。这可能是因为您连接的设备不是发送字符串数据而是发送二进制值,或者可能是因为您的端口配置不正确:波特率,每个字符的位数或奇偶校验等。



尝试从端口读取数据作为字节,并显示十六进制值:

The changes are that the data you are receiving is not straight characters, but binary values. This may be because the device you are connected to is not sending string data but binary values, or it could be because your port configuration is incorrect: baud rate, bits per character, or parity for example.

Try reading the data from the port as bytes, and displaying the hex values:
Dim i As Integer = SerialPort1.BytesToRead
Dim data As Byte() = New Byte(i - 1) {}
i = SerialPort1.Read(data, 0, i)
Dim s As String = String.Join(" ", data.[Select](Function(c) c.ToString("X02")))

这至少应该让您知道要返回的数据。

That should at least give you an idea what data is being returned.


这篇关于从串行端口读取数据,但接收的数据被编码为显示系列的正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆