与COM端口上的串行设备的通信在VB

时间:2018-09-27 21:01:27

标签: vb.net serial-port serial-communication

我正在尝试与CBC自动分析仪机器通信,该机器通过RS232串行端口发送数据。这些是设备设置

enter image description here

我正在使用串行USB适配器将其连接到com4端口

在COM4端口上,我正在使用以下VB代码读取通过的数据。

Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel


Public Class ComReadWrite
    Dim myPorts As Array
    Dim txtline As String
    Dim txtchar As String
    Dim txtbyte As String
    Dim txtexisting As String
    Delegate Sub setTextCallBack(ByVal txt As String)


    Private Sub ComReadWrite_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        myPorts = IO.Ports.SerialPort.GetPortNames()
        portnamecombo.Items.AddRange(myPorts)
        WriteButton.Enabled = False
        CloseButton.Enabled = False

    End Sub

    Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
        SerialPort1.PortName = portnamecombo.Text
        SerialPort1.BaudRate = BaudRateBox.Text
        SerialPort1.ReadTimeout = 500

        SerialPort1.Parity = Parity.None
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = StopBits.One

        SerialPort1.Open()

        Start.Enabled = False
        WriteButton.Enabled = True
        CloseButton.Enabled = True


    End Sub

    Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
        SerialPort1.Write(WriteBox.Text)
    End Sub

    Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
        SerialPort1.Close()
        Start.Enabled = True
        WriteButton.Enabled = False
        CloseButton.Enabled = False
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        RecievedText(SerialPort1.ReadExisting())

    End Sub

    Private Sub RecievedText(ByVal txt As String)
        If Me.ReadBox.InvokeRequired Then
            Dim x As New setTextCallBack(AddressOf RecievedText)
            Me.Invoke(x, New Object() {(txt)})
        Else

            Me.ReadBox.Text &= txt

        End If
    End Sub


    Private Sub portnamecombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles portnamecombo.SelectedIndexChanged

    End Sub

    Private Sub Label5_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged

    End Sub
End Class

在运行程序时,该表格仅读取奇怪的不可读字符,例如OOOOOOOOO,但不读取如图所示的任何可读内容

only shows weird characters

设备随附的文档包含以下页面,这些页面似乎相关。

page1

page2

1 个答案:

答案 0 :(得分:1)

我们需要以ASCII代码传达命令。 Enq是Chr(5),ack是Chr(6)。这些是不可读的字符。

嘿,谢谢Stack溢出社区的帮助。我找到了解决方法如下

该机器应询问应该对哪个主机回复ack的询问。但是,当我阅读串行端口机器时,似乎正在发送一个无法读取的字符。

那么enq等于ASCII码5,它没有任何关联的字符,因此它是不可读的字符。因此,如果我写了serialport1.readchar()而不是serialport1.readexisting(),我知道机器说的是5。那是机器实际上正在发送enq。

现在我们需要发送ASCII值为6

如果我说serialport1.write('6')那就不起作用。 可以使用的是serialport1.write(Chr(6))

然后我用它来发送数据。