1 Replies - 363 Views - Last Post: 17 March 2019 - 01:54 PM Rate Topic: -----

#1 Damein   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 29-March 14

Problem with SerialPort (ReadExisting vs ReadLine)

Posted 17 March 2019 - 12:09 PM

So I am communicating with an Arduino and have some issues getting the full text being sent by the Arduino.

Using this code;

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

Public Class Form1

    Dim WithEvents SerialPort As New IO.Ports.SerialPort

    Delegate Sub myMethodDelegate(ByVal [text] As String)
    Dim myD1 As New myMethodDelegate(AddressOf myShowStringMethod)
    Dim QBoxData As String
    Dim CountText As Integer


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConnectSerial()
        CountText = 0
    End Sub

    Sub myShowStringMethod(ByVal myString As String)
        'display text to our textbox called SerialText
        SerialData.AppendText(myString)
        'MessageBox.Show(myString)





    End Sub

    Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived


        Dim str As String = SerialPort.ReadExisting()

        Invoke(myD1, str)
    End Sub

    Private Sub ConnectSerial()
        Try
            SerialPort.BaudRate = 9600
            SerialPort.PortName = "COM3" 'notice how the ports are named? they HAVE to have COM in front of the number
            SerialPort.Open()
        Catch
            SerialPort.Close()
        End Try
    End Sub
End Class




It works 100% fine, but in essence the data comes in two stages. IE;

Data sent = ^CELL=CLEAR

If I enable the message box I see two Messageboxes. One with "^" and the other is "CELL=CLEAR" so I can determine now that its partial.

Now I did some digging and saw that some people said to use

SerialPort.ReadLine() instead of SerialPort.ReadExisting()

I tried changing that but nothing happens, no message box and no text appending. So I'm trying to figure out a way to either get it all in one fell swoop or maybe make some type of loop in the ReadExisting() until no more data is sent?

Not quite sure, help would be appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with SerialPort (ReadExisting vs ReadLine)

#2 Damein   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 29-March 14

Re: Problem with SerialPort (ReadExisting vs ReadLine)

Posted 17 March 2019 - 01:54 PM

After further digging I realized the problem with ReadLine was on the Arduino side. I didn't have it end with a "\n" to determine the line end. So yeah, that fixed my issues. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1