Receiving Serial output from Arduino on the PC (VB.net) [SOLVED]

Hello all!
After suggestions on this forum I tried to approach my problem differently.
Currently Arduino is sending data via Serial.
Serial.begin(9600);
Serial.println(somedata);
On the PC I am trying to listen to the Serial output and display that data on the screen.
I am hoping someone with some coding experience (unfortunately this was done in VB.net) could point my error...
The application starts - I receive a single "True" (that the connection is actually open) and then no data is displayed. Application just waits and does nothing.
Note: I am able to read data while using Arduino "Serial Monitor" so I know that data is actually being sent.

Imports System.IO.Ports
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.PortName = "COM3"
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Open()
        TextBox1.AppendText(SerialPort1.IsOpen)        
    End Sub

    Private Sub reportin(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        TextBox1.AppendText(SerialPort1.ReadLine)
    End Sub

End Class

Any help appreciated. Thanks!
To the moderator: This relates to the topic I started in the separate subject, but since the question is now different I thought I should start a new one. If this is a duplicate please remove the original topic.

Capture.JPG

When you use the datarecieved event you need to use a delegate. This is because the serial port code and the UI code runs in two different threads.

EDIT:

And you need to declare your serialport "with events"

MikMo:
When you use the datarecieved event you need to use a delegate. This is because the serial port code and the UI code runs in two different threads.

EDIT:

And you need to declare your serialport "with events"

Thanks for the tips! :slight_smile:
I tried following some examples I found on the net.
I followed these articles:

Here is what I got so far and it doesn't work :~

Imports System.IO.Ports
Imports System.Text

Public Class ArduinoSerial

    Public Event ReceivedSerialData(ByVal data As String)
    WithEvents Serial As SerialPort
    Dim data As String

    Private Sub ArduinoSerial_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Serial = My.Computer.Ports.OpenSerialPort("COM3", 9600)
        TextBox.AppendText(Serial.IsOpen & vbCrLf)
    End Sub

    Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
        RaiseEvent ReceivedSerialData(data)
    End Sub

    Public Sub DisplaySerialData(ByVal data) Handles Me.ReceivedSerialData
        TextBox.AppendText(data & vbCrLf)
    End Sub

End Class

I will try to find a solution but I am kind of stuck with this one...
Any help will be appreciated! Thanks!

Here's a very rough dumb terminal prog I wrote a few days ago. It is just a quickie to try something out so don't treat it as a model of programming to be aspired to :), but it might give you a framework to build on.

You'll need a text box and two buttons.

Public Class Form1
    Dim stop_serial As Boolean = False


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Button2.Enabled = True

        xx()
    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If

    End Sub

    Private Sub xx()

        Dim i As String
        TextBox1.Clear()
        Do
            Try
                i = SerialPort1.ReadLine()
                TextBox1.AppendText(i + vbCrLf)
             Catch ex As Exception
            End Try
            Application.DoEvents()
        Loop While Not stop_serial
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "Com39"
        SerialPort1.ReadTimeout = 10

        SerialPort1.Open()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        stop_serial = True
        Button1.Enabled = True
        Button2.Enabled = False

    End Sub

    Dim command As String

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim c As Char

        c = e.KeyChar

        Select Case c
            Case vbCrLf
                SerialPort1.WriteLine(command)
                command = ""
            Case Else
                TextBox1.AppendText(e.KeyChar)
                command += c

        End Select


        e.Handled = True
    End Sub

   
End Class

Rob

Solved!!!
I got it working by using Firmata after all. :slight_smile:
Basically the vb.net app is now able to display analog data from Arduino.

Below is the code for anyone interested. Thanks for your help!!

#include <Firmata.h>

void setup()
{
    Firmata.setFirmwareVersion(0, 2);
    Firmata.begin(115200);
}

void loop()
{
    while(Firmata.available())    
    Firmata.processInput();    
    Firmata.sendAnalog(0, analogRead(0));       
    delay(100);
}
Public Class Main

    Dim data As Integer = 0

    Private AnalogPins As New Hashtable

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FirmataVB1.Connect("COM3", 115200)
        For Each ctrl As System.Windows.Forms.Control In Me.Controls
            Dim ThisCtrl As Firmata.AnalogPinControl = Nothing
            If TypeOf ctrl Is Firmata.AnalogPinControl Then
                ThisCtrl = ctrl
                AnalogPins.Add(CStr(ThisCtrl.PinNumber), ThisCtrl)
                AddHandler ThisCtrl.AnalogOnOff_Changed, AddressOf AnalogPinControl_AnalogOnOff_Changed
            End If
        Next

    End Sub

    Private Sub AnalogPinControl_AnalogOnOff_Changed(ByVal PinNumber As Integer, ByVal OnOff As Integer)
        FirmataVB1.AnalogPinReport(PinNumber, OnOff)
    End Sub

    Private Sub DataReceived(ByVal pin As Integer, ByVal value As Integer) Handles FirmataVB1.AnalogMessageReceieved
        Dim AnalogPin As Firmata.AnalogPinControl
        AnalogPin = AnalogPins(CStr(pin))
        AnalogPin.SetAnalogValue(value)
        data = value        
    End Sub

    Private Sub displayValue()
        TextBox1.Text = data
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        displayValue()
    End Sub

End Class