none
Serial port receiving several string simultaneously and i want to separate them, HELP PLS. RRS feed

  • Question

  • hi there, im trying to transmit data from my arduino ONE to my vb. net App that i designing, the problem here is that i write from the arduino to PC this.

    (LITTLE OF THE ARDUINO CODE)

    void loop() {

      Serial.print("*");
      Serial.print("p_");

      Serial.print(i);    ---> weight variable.

      Serial.print("*");
      Serial.print("t_");
    Serial.print(ktc.readCelsius());    ---> sensor temp. variable 

    }

    so, the result it's this 

    *p_1523.5*t_34.45*p_1523.5*t_34.45*p_1523.5*t_34.45

    Iwant to read it and separate in vb.net app, i was trying with serialport.readexisting() 

    but i got problems getting the reading values into an array to then use the .split().

    then with those values i want to Graphic them. 

    Monday, May 13, 2019 1:44 PM

Answers

  • As mentioned it helps to indicate the end of message, in the arduino to VB that can be done using Serial.println(); like this

    arduino

      Serial.print("*");
      Serial.print("p_");
      Serial.println(i);   
    
      Serial.print("*");
      Serial.print("t_");
      Serial.println(ktc.readCelsius()); 
     
      delay(500);

    then using ReadLine at the VB end you should capture an entire string like below

    VB

    Public Delegate Sub myDelegate(ByVal myData As String)
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
                SerialPort1.NewLine = vbCrLf
                
    End Sub
    
    
    
    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) HandlesSerialPort1.DataReceived
           
     Dim myData As String
    
            Do While SerialPort1.BytesToRead > 0
                Try
    
                    myData = SerialPort1.ReadLine
    
                    Me.BeginInvoke((New myDelegate(AddressOf data_display)), myData)
    
                Catch ex As Exception
    
                End Try
            Loop
    
     End Sub
    
    
    
    
    Private Sub data_display(ByVal s As String)
    
            Dim str As String = s.Substring(0, 3)
            If str = "*t_" Then
                RichTextBox1.AppendText("Weight " & s & vbCr)
            ElseIf str = "*p_" Then
                RichTextBox1.AppendText("Temperature " & s & vbCr)
            End If
    
    End Sub

    to try this just create 1 Form with 1 RichTextBox and configure a serial port named SerialPort1, the Newline property in the Form Load is set for vbCrLf which is the terminator for the arduino println command

    • Marked as answer by krlos95 Thursday, May 23, 2019 5:28 PM
    Tuesday, May 14, 2019 8:49 PM

All replies

  • Hello,

    What is the problem with split? Seems you would remove the first asterix then do the split on "*" then perform a split on the underscore for each array element. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 13, 2019 2:14 PM
    Moderator
  • hi there, im trying to transmit data from my arduino ONE to my vb. net App that i designing, the problem here is that i write from the arduino to PC this.

    (LITTLE OF THE ARDUINO CODE)

    void loop() {

      Serial.print("*");
      Serial.print("p_");

      Serial.print(i);    ---> weight variable.

      Serial.print("*");
      Serial.print("t_");
    Serial.print(ktc.readCelsius());    ---> sensor temp. variable 

    }

    so, the result it's this 

    *p_1523.5*t_34.45*p_1523.5*t_34.45*p_1523.5*t_34.45

    Iwant to read it and separate in vb.net app, i was trying with serialport.readexisting() 

    but i got problems getting the reading values into an array to then use the .split().

    then with those values i want to Graphic them. 


    There is no guarantee that a serial port read will have an entire message.  You will need to accumulate reads until you have an entire message.  In the arduino add something to indicate the end-of-message and it will help on the VB side.

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, May 13, 2019 2:27 PM
  • like the Serial.flush() function?

    Tuesday, May 14, 2019 1:24 AM
  • As mentioned it helps to indicate the end of message, in the arduino to VB that can be done using Serial.println(); like this

    arduino

      Serial.print("*");
      Serial.print("p_");
      Serial.println(i);   
    
      Serial.print("*");
      Serial.print("t_");
      Serial.println(ktc.readCelsius()); 
     
      delay(500);

    then using ReadLine at the VB end you should capture an entire string like below

    VB

    Public Delegate Sub myDelegate(ByVal myData As String)
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
                SerialPort1.NewLine = vbCrLf
                
    End Sub
    
    
    
    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) HandlesSerialPort1.DataReceived
           
     Dim myData As String
    
            Do While SerialPort1.BytesToRead > 0
                Try
    
                    myData = SerialPort1.ReadLine
    
                    Me.BeginInvoke((New myDelegate(AddressOf data_display)), myData)
    
                Catch ex As Exception
    
                End Try
            Loop
    
     End Sub
    
    
    
    
    Private Sub data_display(ByVal s As String)
    
            Dim str As String = s.Substring(0, 3)
            If str = "*t_" Then
                RichTextBox1.AppendText("Weight " & s & vbCr)
            ElseIf str = "*p_" Then
                RichTextBox1.AppendText("Temperature " & s & vbCr)
            End If
    
    End Sub

    to try this just create 1 Form with 1 RichTextBox and configure a serial port named SerialPort1, the Newline property in the Form Load is set for vbCrLf which is the terminator for the arduino println command

    • Marked as answer by krlos95 Thursday, May 23, 2019 5:28 PM
    Tuesday, May 14, 2019 8:49 PM
  • Got it ;) its working thanks 
    Tuesday, May 21, 2019 1:01 AM
  • Hi,

    I am glad you have got your solution, please mark the useful replies as answers.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 5:04 AM