VB.NET VS2019, serialport收发数据,典型的代码,每200ms发一下命令,读取外部一个板子的数据。连接串口后,可以读到数据,但是此时点击‘关闭串口’,或者退出窗口,都要点很多次,才有效,好像收数据时,影响了其次按钮的执行一样,求解,,
Imports System
Imports System.IO.Ports
Public Class Form1
Dim PortSwitch As Boolean '定义串口是否打开标志
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub ComLoad() '显示可以用的串口号
Dim com_ports As String() = IO.Ports.SerialPort.GetPortNames
'Dim i As Integer
'i = 0
For Each com_port As String In com_ports
ComboBox_COM.Items.Add(com_port)
'If i = 0 Then '让框里面显示第一个读到的COM口
' ComboBox_COM.Text = com_port
' i = i + 1
'End If
Next
ComboBox_COM.SelectedIndex = 0
End Sub
Private Sub InitialPort(SerialPort As String, BaudRate As String)
If SerialPort1.IsOpen = False Then '只能再关闭串口的时候,修改串口的参数,否则报错
SerialPort1.PortName = SerialPort '串口名称
SerialPort1.BaudRate = BaudRate '波特率
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ToolStripStatusLabel_Bottom.Text = ""
ComLoad()
Timer1.Interval = 200 '100ms
End Sub
Private Sub Button_About_Click(sender As Object, e As EventArgs) Handles Button_About.Click
About.ShowDialog()
End Sub
Private Sub Button_Exit_Click(sender As Object, e As EventArgs) Handles Button_Exit.Click
'MyBase.Close()
Me.Dispose()
End Sub
Private Sub Button_OpenCloseCom_Click(sender As Object, e As EventArgs) Handles Button_OpenCloseCom.Click
InitialPort(ComboBox_COM.Text, ComboBox_Baudrate.Text)
If PortSwitch = False Then '如果串口关闭
Try
SerialPort1.Open() '打开串口
If SerialPort1.IsOpen = True Then
PortSwitch = True '则将串口打开标志设为允许打开
Button_OpenCloseCom.Text = "关闭串口"
ToolStripStatusLabel_Bottom.Text = "串口已连接"
'ToolStripStatusLabel_Bottom.BackColor = Color.Green
Timer1.Start()
End If
Catch ex As Exception
MessageBox.Show(ex.Message + "a")
End Try
Else
Try
Timer1.Stop()
SerialPort1.Close() '关闭串口
Sleep(1000)
If SerialPort1.IsOpen = False Then
PortSwitch = False '否则将串口打开标志设为禁止打开
Button_OpenCloseCom.Text = "打开串口"
ToolStripStatusLabel_Bottom.Text = "串口未连接"
'ToolStripStatusLabel_Bottom.BackColor = DefaultBackColor
End If
Catch ex As Exception
MessageBox.Show(ex.Message + "b")
End Try
End If
End Sub
Private Sub SendData(ByVal strdata As String)
Try
SerialPort1.Write(strdata)
StatusLabel_SendcharNum.Text = Str(Val(StatusLabel_SendcharNum.Text) + SerialPort1.BytesToWrite)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'每1000ms发送一次测试连通包,回应后,发送状态信息,如果未得到回应,则直接退出
'If mode = "发送端" Then
If PortSwitch = True Then
If SerialPort1.IsOpen = True Then
SendData("GD" & vbCrLf)
End If
End If
'End If
'每两秒查询一次数据库的状态,比对视频会议的时间,如果当前时间已经超过开会时间,则直接退出
End Sub
Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Dim strIncoming As String
Try
StatusLabel_ReceiveCharNum.Text = Str(Val(StatusLabel_ReceiveCharNum.Text) + SerialPort1.BytesToRead) '获取接收缓冲区中数据的字节数。
strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
Threading.Thread.Sleep(100) '线程的延时
TextBox_Receive.Text = TextBox_Receive.Text + strIncoming
TextBox_Receive.Focus()
TextBox_Receive.SelectionLength = 0
TextBox_Receive.SelectionStart = TextBox_Receive.Text.Length
TextBox_Receive.ScrollToCaret()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '开启一个新的线程,运行接收数据的函数。
End Sub
End Class