Listening to the Serial Port - Need help (vb.net)

Hello,

Hopefully someone with some vb.net experience is reading this… :wink:

I am trying to listen to the COM3 port and display the output in the TextBox.

If I understood correctly I will have to implement this by using eventhandlers, delegates.

I tried following these articles:

http://www.informit.com/articles/article.aspx?p=23020

http://www.xtremevbtalk.com/showthread.php?t=159976

And this is the code I came up with…

Unfortunately it doesn’t display anything. App just starts display “True” and no data is being displayed.

I am monitoring Arduino platform which I know is sending data constantly.

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 am really new into the vb.net. I am sure there are some horrible mistakes with this code but I am kind of stuck for the past 2 days on this one. Maybe someone could point me in the right direction. Thanks! :wink:

First don’t believe everything you read. :wink:

In your project add:

Imports System.IO

Imports System.IO.Ports

I have this in my main form.

        For i As Integer = 0 To _
           My.Computer.Ports.SerialPortNames.Count - 1
            cbbComPorts.Items.Add( _
               My.Computer.Ports.SerialPortNames(i))
        Next
        btnconnect.Enabled = False
        btnDisconnect.Enabled = False
        lblMessage.Text = "Not Connected"

cbb is a combobox.

btconnect is a button text = Connect.

btnDisconnect is a button text = Close.

WARNING do not exit the app without closing your connection.

btnConnect Code:

 If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

Try
            With SerialPort1
                .PortName = cbbComPorts.Text
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
            End With
            SerialPort1.Open()

            StrTime = Environment.TickCount
            
            lblMessage.Text = cbbComPorts.Text & " connected."
            btnconnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

This should get your com port opened or complain.

Once you have it open reading data is this simple. :!:

Dim NewTemp() As String = Split(SerialPort1.ReadLine, ",")

NewTemp is a string because I am passing a comma seperated list of values each time.

btnDisconnect code:

Try
            SerialPort1.Close()
            lblMessage.Text = SerialPort1.PortName & " disconnected."
            btnConnect.Enabled = True
            btnDisconnect.Enabled = False
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

I have A MEGA with 5 temp sensors passing information to my VB.NET app graphing the results as fast as the MEGA can push them over.

Enjoy!

Ted