Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port
Listening to the Serial Port Listening to the Serial Port
Listening to the Serial Port
Go Back  Xtreme Visual Basic Talk > > > Listening to the Serial Port


Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2011, 09:05 AM
marcellus marcellus is offline
Newcomer
 
Join Date: Sep 2011
Posts: 2
Question Listening to the Serial Port


Hello,
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
Delegates in VB.Net

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.

Code:
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!
Reply With Quote
  #2  
Old 09-21-2011, 10:51 AM
DrPunk's Avatar
DrPunkListening to the Serial Port DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

At no point are you setting your Data variable to what has come from the comm port.

In DataReceived you should be setting Data = the received data.

Like...
Code:
Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
    data = Serial.ReadExisting
    RaiseEvent ReceivedSerialData(data)
End Sub
This assumes that that event is actually firing, i.e. your comms are set up correctly in the first place.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 09-21-2011, 11:37 AM
marcellus marcellus is offline
Newcomer
 
Join Date: Sep 2011
Posts: 2
Default

Thank you so much for your help.
I revised the code below. I looked around and did some modifications to the code but unfortunately it still doesn't work. When I debug the program the event is not even raised. It seems like the data is not coming through.
When I test with Arduino software (Serial Monitor) I can see that the data is actually transmitted. So I don't know where to begin looking for a solution...

According the: http://www.arduino.cc/en/Serial/Print
"Numbers are printed using an ASCII character for each digit".
My arduino platform is sending only 1 or 0
Serial.print(1) gives "1".

Code:
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, Parity.None, 8, StopBits.One)
        Serial.Encoding = System.Text.Encoding.ASCII
        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
        If Serial.BytesToRead = 0 Then Exit Sub
        Dim buf() As Byte = New Byte() {}
        Array.Resize(buf, Serial.BytesToRead)
        Serial.Read(buf, 0, buf.Length)
        data = buf.ToString
        RaiseEvent ReceivedSerialData(data)
    End Sub

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

End Class
Like I said above I see that Arduino is sending data over COM3. (attached screenshot)
Attached Images
File Type: png com34.PNG (11.9 KB, 8 views)
Reply With Quote
  #4  
Old 09-21-2011, 02:15 PM
azuckerman's Avatar
azuckerman azuckerman is offline
Newcomer
 
Join Date: Sep 2011
Location: Texas, USA
Posts: 13
Default

Quote:
Originally Posted by marcellus View Post
Code:
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, Parity.None, 8, StopBits.One)
        Serial.Encoding = System.Text.Encoding.ASCII
        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
        If Serial.BytesToRead = 0 Then Exit Sub
        Dim buf() As Byte = New Byte() {}
        Array.Resize(buf, Serial.BytesToRead)
        Serial.Read(buf, 0, buf.Length)
        data = buf.ToString
        RaiseEvent ReceivedSerialData(data)
    End Sub

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

End Class
If you change the Serial_DataReceived method to this, it will simplify your code somewhat and be a little faster:

Code:
    Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
        If Serial.BytesToRead > 0 Then
            Dim sBuf As String
            sBuf = Serial.ReadExisting()
            DisplaySerialData(data)
        End If
    End Sub
One other thing that you may need to do is to strip out Null characters from the data being displayed. Textboxes were written in C#, so they have some of the same limitations as strings in C# (the Null character is a string termination character - nothing gets displayed after it).
Reply With Quote
  #5  
Old 09-23-2011, 03:46 AM
DrPunk's Avatar
DrPunkListening to the Serial Port DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I had a look around the Arduino forum (I just searched for DataReceived) and there's quite a few people who seem to have the same problem.

There's a few things that people say fixed it for them. Someone says that Encoding UTF8 worked, someone says that enabling DTR on the serialport worked.

But most people seemed to resort to periodically polling the serialport and checking if there's anything to read. This might be the best first step for you because it would at least tell you the problem is with the event rather than you not receiving anything.

Simplest thing is just to stick a timer on the form and have azuckerman's DataReceived code above inside the timer tick event.

At least then you might be able to move forward.
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 09-23-2011, 05:14 PM
azuckerman's Avatar
azuckerman azuckerman is offline
Newcomer
 
Join Date: Sep 2011
Location: Texas, USA
Posts: 13
Default

If the Arduino control mimics the SerialPort class found in .NET, then the DataReceived event should fire any time there is data in the input buffer.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port Listening to the Serial Port
Listening to the Serial Port
Listening to the Serial Port
 
Listening to the Serial Port
Listening to the Serial Port
 
-->