SerialPort1.BaudRate = 9600 'BMS波特率
SerialPort1.PortName = "COM1" 'BMS串口
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = 1 '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
Timer1.Interval = 3000
Timer1.Enabled = True
SerialPort1.ReceivedBytesThreshold = 1 '事件触发频率,每收到1个字节长度数据即出发接收事件
SerialPort1.Open()
If SerialPort1.IsOpen = True Then
StatusLabel.Text = "串口已连接"
StatusLabel.ForeColor = Color.Blue
End If
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ReDim sendbuff(7)
Try
SerialPort1.Write(sendbuff, 0, sendbuff.Length)‘
在这个位置程序就停止了
Threading.Thread.Sleep(1000) '添加的延时
SerialPort1.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'触发接收事件
Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数
End Sub