我是vb的新手,我正在尝试拆分从Visual Basic上的Arduino板收到的串行数据。我已经观看并关注了在线教程,这些教程在一个文本框中输出所有的串行数据,并且可以正常工作。但现在我需要将数据分成与传感器对应的文本框(我有5个传感器)。
我尝试使用.Split和数组拆分然后存储数据,然后将其移动到相应的文本框,但它不起作用。
感谢我能得到的所有帮助
Dim receivedData As String = ""
Private Sub AirQuality_Load(sender As Object, e As EventArgs) Handles Me.Load
GetSerialPortNames()
Timer1.Enabled = False
txtPmax.Text = 14.7
txtPmin.Text = 10
txtO2max.Text = 21
txtO2min.Text = 15
txtCO2.Text = 1000
txtP25.Text = 25
txtP10.Text = 50
txtCO.Text = 35
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
cbCOMPort.Items.Add(sp)
Next
cbCOMPort.SelectedIndex = 0
End Sub
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Try
If (btnConnect.Text = "Connect") Then
If (cbCOMPort.Text <> "") Then
'Open Serial Port
SerialPort1.Close()
SerialPort1.PortName = cbCOMPort.Text
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
SerialPort1.ReadTimeout = 10000
SerialPort1.Open()
btnConnect.Text = "Dis-connect"
Timer1.Enabled = True
btnUpdate.PerformClick()
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
btnConnect.Text = "Connect"
Timer1.Enabled = False
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Function ReceiveSerialData() As String
Dim Incoming As String
Dim Array() As String
Incoming = SerialPort1.ReadExisting
Array = Split(Incoming, ";")
Pactual.Text = Array(0)
O2actual.Text = Array(1)
COactual.Text = Array(2)
CO2actual.Text = Array(3)
P25actual.Text = Array(4)
P10actual.Text = Array(5)
txtUpdate.Text = Array(6)
End Function
Sub numberValidate()
Try
If (CDbl(txtPmax.Text) < CDbl(txtPmin.Text) Or (CDbl(txtO2max.Text) < CDbl(txtO2min.Text))) Then
MsgBox("Error. Must be numbers or Max < Min")
End If
Catch ex As Exception
MsgBox("Error. Must be numbers or Max < Min")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static bFlash As Boolean
receivedData = ReceiveSerialData()
If receivedData IsNot Nothing Then
txtUpdate.Text = receivedData
' Warning in GUI
Dim bWar As Boolean = receivedData.Contains("--Pressure not OK--" Or "--CO2 not OK--" Or "--O2 not OK--")
If receivedData.Contains("--Pressure not OK--" Or "--CO2 not OK--" Or "--O2 not OK--") Then
bFlash = Not bFlash
If bFlash Then
txtUpdate.BackColor = Color.FromArgb(255, 0, 0)
Else
txtUpdate.BackColor = Color.FromArgb(255, 255, 255)
End If
Else
txtUpdate.BackColor = Color.FromArgb(255, 0, 255)
End If
If receivedData.Contains("--Condition: Normal--") Then
txtUpdate.BackColor = Color.FromArgb(255, 255, 255)
End If
End If
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim writeText As String
writeText = "<" & CDbl(txtPmax.Text).ToString("00.0") & CDbl(txtPmin.Text).ToString("00.0") & CInt(txtO2max.Text).ToString("00") & CInt(txtO2min.Text).ToString("00") & CInt(txtCO2.Text).ToString("0000") & ">"
txtUpdate.Text = writeText
SerialPort1.Write(writeText)
End Sub