Re: VB code for serial port
CLASS MAIN:
Public Delegate Sub rcvDelegate()
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'REM This is to send the code to get the sensor names
TextBox6.Text = ""
requestType = "N"
Try
SerialPort1.Write("N" & vbCrLf)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
System.Threading.Thread.Sleep(1000)
End Sub
Public Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'REM BufferHold is a global variable that holds the string
BufferHold += SerialPort1.ReadExisting
TextBox6.Invoke(New rcvDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Private Sub updateTextBox()
'TextBox6.Text = TextBox6.Text + SerialPort1.ReadExisting()
TextBox6.Text = BufferHold
If requestType = "N" Then
'This is a request for sensor names
SensorNames = BufferHold
End If
If requestType = "R" Then
'This is a request for sensor values
SensorValues = BufferHold
End If
SN1 = SensorNames.Substring(0, 3)
Label3.Text = SN1
SN2 = SensorNames.Substring(5, 3)
Label4.Text = SN2
SN3 = SensorNames.Substring(10, 3)
Label5.Text = SN3
SN4 = SensorNames.Substring(15, 3)
Label6.Text = SN4
SN5 = SensorNames.Substring(20, 3)
Label7.Text = SN5
End Sub
From Viet Nam -Mail:chutinh