发一个例子,已经调试通过。Public Class Form1 Private Delegate Sub QR_Read(ByVal iContent As String) Private pmReadStop As Boolean = False Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectPort.Click Dim plSettings() As String plSettings = TextBox2.Text.Split(",") SerialPort1.PortName = TextBox1.Text With SerialPort1 .BaudRate = plSettings(0) '/ ボーレート設定 ' Select Case plSettings(1) '/ パリティ設定 Case "E" .Parity = IO.Ports.Parity.Even Case "N" .Parity = IO.Ports.Parity.None Case "O" .Parity = IO.Ports.Parity.Odd Case "S" .Parity = IO.Ports.Parity.Space Case "M" .Parity = IO.Ports.Parity.Mark End Select ' .DataBits = plSettings(2) '/ データビット設定 ' .StopBits = plSettings(3) '/ ストップビット設定 '' If .IsOpen Then .Close() End If .Open() End With End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClosePort.Click With SerialPort1 If .IsOpen Then .Close() End If End With End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sender.Click With SerialPort1 .WriteLine(TextBox3.Text) End With End Sub Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim plReadString As String If pmReadStop Then pmReadStop = False Return End If Threading.Thread.Sleep(300) plReadString = SerialPort1.ReadExisting 'TextBox4.Text = plReadString Dim plInvokeFunc As New QR_Read(AddressOf Me.QRRead) Me.Invoke(plInvokeFunc, plReadString) End Sub Private Sub QRRead(ByVal ReceivedString As String) TextBox4.Text = ReceivedString End Sub
由于DataReceived事件在辅线程被引发,当收到完整的一条数据,返回主线程处理或在窗体上显示时,请注意跨线程的处理,C#可采用控件异步委托的方法Control.BeginInvoke及同步委托的方法Invoke。
Private pmReadStop As Boolean = False Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectPort.Click
Dim plSettings() As String plSettings = TextBox2.Text.Split(",")
SerialPort1.PortName = TextBox1.Text With SerialPort1
.BaudRate = plSettings(0) '/ ボーレート設定
'
Select Case plSettings(1) '/ パリティ設定
Case "E"
.Parity = IO.Ports.Parity.Even
Case "N"
.Parity = IO.Ports.Parity.None
Case "O"
.Parity = IO.Ports.Parity.Odd
Case "S"
.Parity = IO.Ports.Parity.Space
Case "M"
.Parity = IO.Ports.Parity.Mark
End Select
'
.DataBits = plSettings(2) '/ データビット設定 '
.StopBits = plSettings(3) '/ ストップビット設定
''
If .IsOpen Then
.Close()
End If
.Open()
End With End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClosePort.Click
With SerialPort1
If .IsOpen Then
.Close()
End If
End With End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sender.Click
With SerialPort1
.WriteLine(TextBox3.Text)
End With
End Sub Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Dim plReadString As String If pmReadStop Then
pmReadStop = False
Return
End If
Threading.Thread.Sleep(300)
plReadString = SerialPort1.ReadExisting
'TextBox4.Text = plReadString
Dim plInvokeFunc As New QR_Read(AddressOf Me.QRRead) Me.Invoke(plInvokeFunc, plReadString) End Sub Private Sub QRRead(ByVal ReceivedString As String)
TextBox4.Text = ReceivedString
End Sub
End Class