串口读写

时间:2019-11-21 10:22:39

标签: vb.net serial-port

我正在一个项目中,必须将数据发送到控制器设备并从中获取响应。它与RS232协议一起使用。 1号要将数据发送到控制器,需要一个结束字符(分号;),例如:

?PA;?IA;?S;发送这3条命令

第二。控制器用PA=123;I=234;S=0;回答上述命令,并且用相同的结束字符(每个命令的末尾用分号)。

我的问题是我想将控制器的答案(接收数据)分成2个文件,分别是通过Timer事件连续发送的数据和通过button事件连续发送的数据。

我该如何创建将它们分开的函数。

这是我到目前为止使用的代码:

我创建了一个类,用于与控制器和接收到的数据建立连接

Public Class SerialController
     Public Event DataReceived(sender As Object, e As DataReceivedEventArgs)

     Private Sub sPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sPort.DataReceived
        Try
            Dim c As Char = ";"

            If sPort.BytesToRead = 0 Then Exit Sub

            Dim bytsToRead As Integer = sPort.BytesToRead
            '  MessageBox.Show(bytsToRead.ToString)
            Dim buf(bytsToRead - 1) As Byte

            bytsToRead = sPort.Read(buf, 0, bytsToRead)

            Threading.Monitor.Enter(rcvQLock)
            Try
                rcvQ.Append(Encoding.ASCII.GetString(buf))

            Finally
                Threading.Monitor.Exit(rcvQLock)
            End Try

            While rcvQ.ToString.Contains(c)

                Dim l As Integer = rcvQ.ToString.IndexOf(c)

                Dim ca(l + 1) As Char

                Threading.Monitor.Enter(rcvQLock)
                Try
                    rcvQ.CopyTo(0, ca, 0, l + 1)
                    'MessageBox.Show(ca)
                    rcvQ.Remove(0, l + 1)
                Finally
                    Threading.Monitor.Exit(rcvQLock)
                End Try
                RaiseEvent DataReceived(Me, New DataReceivedEventArgs With {.Data = New String(ca)})
            End While
        Catch

        End Try

    End Sub
End Class

以及在主界面

Imports System.ComponentModel
Imports System.Globalization
Imports System.Threading

Public Class Form1

    Private serialDataInputs As New List(Of SerialController)
    Dim sc As SerialController
    Dim parameter As String
    Dim AdHocState as inteeger

    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        For Each sc As SerialController In serialDataInputs
            sc.CloseSerialPort()
        Next
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim strComPort As String = "COM16"
        sc = New SerialController(strComPort)
        AddHandler sc.DataReceived, AddressOf sc_DataReceived

        sc.OpenSerialPort(strComPort, 115200, IO.Ports.Parity.None)

        Timer1.Enabled = True

    End Sub
    Private Sub sc_DataReceived(sender As Object, e As DataReceivedEventArgs)


            Try

                AddOutput("Recieving<---" & e.Data)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

        End If

    End Sub
    Private Delegate Sub TextDelegate(s As String)
    Private Sub AddOutput(s As String)

        If ListBox2.InvokeRequired Then
            Dim d As New TextDelegate(AddressOf AddOutput)
            ListBox2.Invoke(d, {s})
        Else
            ListBox2.Items.Add(s)

        End If

    End Sub



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      AdHocState = 1

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If AdHocState = 0 Then

            'sc.WriteData("?PA;?IA;?OA;")
            sc.WritePort("?PA;")
        ElseIf AdHocState = 1 Then  'the user wants to send a string to the device
            AdHocState = 2   'Flag that we expect a response to the user string

            sc.WritePort(TextBox1.Text)
        End If

        AdHocState = 0


    End Sub

End Class

0 个答案:

没有答案