0

我正在通过串行端口向我的计算机发送一些数据。因此,一旦数据到达所述端口,我想加载一个要加载的 VBNet 程序。换句话说,程序将由到达端口的数据触发。

我该如何实施?

4

1 回答 1

1

试试这个。它假设将收到 3 个字节。您需要根据要发送的字节数对其进行更改。正如 Mark 建议的那样,使用 Process 来启动程序。或者,您可以使用 Shell。

Imports System.IO.Ports

Public Class COMControl

Private WithEvents comPort As SerialPort

Public Sub New(comPortNumber As Integer)

    comPort = New SerialPort
    With comPort
        .BaudRate = 9600
        .Parity = IO.Ports.Parity.None
        .StopBits = IO.Ports.StopBits.One
        .DataBits = 8
        .PortName = "COM" & comPortNumber.ToString
        .ReceivedBytesThreshold = 3
        .Open()
    End With

End Sub


Private Sub comPort_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived

    If comPort.BytesToRead = 3 Then
        Dim by(comPort.BytesToRead - 1) As Byte

        'Read the bytes from the port...
        comPort.Read(by, 0, comPort.BytesToRead)

        '...into an array of bytes
        Dim byList As New List(Of Byte)
        byList.AddRange(by)

        Dim inp As String = System.Text.Encoding.ASCII.GetString(by)

        Select Case inp

            Case "P01"
                Process.Start("PO1.exe")

            Case "P02"
                Process.Start("PO2.exe")

        End Select

    End If
End Sub
End Class
于 2013-02-03T17:51:04.777 回答