我正在通过串口向我的计算机发送一些数据。因此,我想加载一个VBNet程序,一旦数据到达所述端口就加载。换句话说,程序将由到达端口的数据触发。
我该如何实现?
答案 0 :(得分: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