Hello i'm not an experienced coder but can normally get by and make things work, not this time though
So basically i'm trying to send some temperature readings from the arduino through bluetooth to a program made in VB.
Here's a snippet of the arduino code that sends the data:
Serial3.flush();
Serial3.print("a");
Serial3.print(tempc2);
Serial3.print(",b");
Serial3.print(des2);
Serial3.print(",c");
Serial3.print(des);
Serial3.print(",d");
Serial3.print(tempc);
Serial3.print('\n');
Serial3.flush();
delay(100);
And that outputs correctly like this using a bluetooth serial terminal:
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
But in Visual Basic i get the odd messed up lines like:
a13,b1,c0,d13
313
a13,b1,c0,d13
a12,b1,c0,d13
a12,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
3,b1,c0,da13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
213
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
a13,b1,c0,d13
Here's my VB code, as you can see towards the bottom i've tried to separate the contents of a line into separate text box's but it's basically useless because none of the data is shown correctly.
Public Class Form1
'------------------------------------------------
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
'------------------------------------------------
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
Button2.Enabled = False
End Sub
'------------------------------------------------
Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
End Sub
'------------------------------------------------
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
Button1.Enabled = False
Button2.Enabled = True
Button4.Enabled = True
End Sub
'------------------------------------------------
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
SerialPort1.Write(RichTextBox1.Text & vbCr) 'concatenate with \n
RichTextBox1.Clear()
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
SerialPort1.Close()
Button1.Enabled = True
Button2.Enabled = False
Button4.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] 'append text
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Write("t1")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
SerialPort1.Write("t2")
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
SerialPort1.Write("t3")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SerialPort1.Write("t4")
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
SerialPort1.Write("t5")
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen = True Then
Dim data As String = SerialPort1.ReadLine 'this is the data string you received from the controller
Dim a As String = String.Empty
Dim b As String = String.Empty
Dim c As String = String.Empty
Dim d As String = String.Empty
'Split the data to an array
Dim parts() As String = data.Split(","c)
'Now work the data array to extract each piece of data
For Each part As String In parts
If part.StartsWith("a") Then
a = part.Substring(2)
ElseIf part.StartsWith("b") Then
b = part.Substring(2)
ElseIf part.StartsWith("c") Then
c = part.Substring(2)
ElseIf part.StartsWith("d") Then
d = part.Substring(2)
End If
Next
TextBox1.Text = a + "°c"
TextBox2.Text = b + "°c"
TextBox3.Text = c + "°c"
TextBox4.Text = d + "°c"
End If
End Sub
Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged
RichTextBox2.Select(RichTextBox2.TextLength - 1, 1)
RichTextBox2.ScrollToCaret()
End Sub
End Class
If anybody can help with the jumbled lines it would be much appreciated, Cheers