This question is an exact duplicate of:
I am working with Arduino Uno, I have tracker application running on Arduino Uno board. I am sending data in comma separated value. Now I want to view data into Visual Basic. I have code as below. The below code is working with fake values but as soon it live stream data split functions gives out error.
Serial output data looks like this.
Start,Latitude:13.00,longitude:60.00,MODE:1,DIR:1,Local Date:0/0/0,Local Time:5:30:2,tracker_des_angle:0.00,tracker_actual_pos:57.22,Wind Speed :0.00,END
Start,Latitude:13.00,longitude:60.00,MODE:1,DIR:1,Local Date:0/0/0,Local Time:5:30:4,tracker_des_angle:43.00,tracker_actual_pos:69.20,Wind Speed :0.95,END
Start,Latitude:13.00,longitude:60.00,MODE:1,DIR:0,Local Date:0/0/0,Local Time:5:30:6,tracker_des_angle:43.00,tracker_actual_pos:16.34,Wind Speed :1.85,END
Start,Latitude:13.00,longitude:60.00,MODE:1,DIR:0,Local Date:0/0/0,Local Time:5:30:8,tracker_des_angle:43.00,tracker_actual_pos:11.89,Wind Speed :2.77,END
visual basic code:
Option Explicit On
Imports System
Imports System.IO.Ports
Imports System.Text
Public Class Solar_Track
Dim comPORT As String
Dim receivedData As String = ""
Dim strArray() As String
Dim Data As String
Dim latitude() As String
Dim longitude() As String
Dim Local_date() As String
Dim Local_Time() As String
Dim Mode_oper() As String
Dim Dir_motor() As String
Dim Act_pos() As String
Dim Dis_pos() As String
Dim wind_pos() As String
Dim start As Boolean
Dim End_Result As Boolean
Dim result As Boolean
Sub Parse_receivedata(ByVal msg As String)
RichTextBox1.Text &= msg
Data = msg
strArray = Split(Data, ",")
latitude = Split(strArray(1), ":")
Text_Latitude.Text = latitude(1)
longitude = Split(strArray(2), ":")
Text_Longitude.Text = longitude(1)
Mode_oper = Split(strArray(3), ":")
Text_Mode.Text = Mode_oper(1)
Dir_motor = Split(strArray(4), ":")
Text_MotorDir.Text = Dir_motor(1)
Local_date = Split(strArray(5), ":")
Text_LocalDate.Text = Local_date(1)
Local_Time = Split(strArray(6), ":")
Label_hour.Text = Local_Time(1)
Label_min.Text = Local_Time(2)
Label_Sec.Text = Local_Time(3)
Dis_pos = Split(strArray(7), ":")
Text_TrackerDesire.Text = Dis_pos(1)
Act_pos = Split(strArray(8), ":")
Text_TrackerActual.Text = Act_pos(1)
wind_pos = Split(strArray(9), ":")
Text_Windspeed.Text = wind_pos(1)
End Sub
Sub data_parse()
Data = "Start,Latitude:13.00,longitude:60.00,MODE:1,DIR:1,Local Date:0/0/0,Local Time:5:30:2,tracker_des_angle:0.00,tracker_actual_pos:54.48,Wind Speed :0.00,END"
strArray = Split(Data, ",")
latitude = Split(strArray(1), ":")
Text_Latitude.Text = latitude(1)
longitude = Split(strArray(2), ":")
Text_Longitude.Text = longitude(1)
Mode_oper = Split(strArray(3), ":")
Text_Mode.Text = Mode_oper(1)
Dir_motor = Split(strArray(4), ":")
Text_MotorDir.Text = Dir_motor(1)
Local_date = Split(strArray(5), ":")
Text_LocalDate.Text = Local_date(1)
Local_Time = Split(strArray(6), ":")
Label_hour.Text = Local_Time(1)
Label_min.Text = Local_Time(2)
Label_Sec.Text = Local_Time(3)
Dis_pos = Split(strArray(7), ":")
Text_TrackerDesire.Text = Dis_pos(1)
Act_pos = Split(strArray(8), ":")
Text_TrackerActual.Text = Act_pos(1)
wind_pos = Split(strArray(9), ":")
Text_Windspeed.Text = wind_pos(1)
End Sub
Private Sub Solar_Track_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = False
comPORT = ""
For Each sp As String In My.Computer.Ports.SerialPortNames
comPort_ComboBox.Items.Add(sp)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
If (comPort_ComboBox.SelectedItem <> "") Then
comPORT = comPort_ComboBox.SelectedItem
End If
End Sub
Private Sub BtConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect_BTN.Click
If (connect_BTN.Text = "Connect") Then
If (comPORT <> "") Then
SerialPort1.Close()
SerialPort1.PortName = comPORT
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.ReadTimeout = 10000
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
Timer1.Enabled = True
Timer_LBL.Text = "Timer: ON"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
connect_BTN.Text = "Connect"
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
receivedData = ReceiveSerialData()
''RichTextBox1.Text &= receivedData
Parse_receivedata(receivedData)
receivedData = ""
'' data_parse()
End Sub
Function ReceiveSerialData() As String
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then
Return "nothing" & vbCrLf
Else
Return Incoming
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
End Function
Private Sub BtClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtClear.Click
RichTextBox1.Text = ""
End Sub
End Class