Results 1 to 4 of 4

Thread: Listening to the Serial Port - Need help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Question Listening to the Serial Port - Need help

    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
    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.

    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!

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: Listening to the Serial Port - Need help

    First let me say you have found in this forum a great resource when it comes to all things vb. There is a wealth of knowledge and experience that already has been shared. I suggest you use the search function on this site and do a search for "serial port" and derivatives.

    In saying that part of your problem is here.
    Code:
     Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
            RaiseEvent ReceivedSerialData(data)
        End Sub
    How do you imagine your received data ends up in your data string?
    Its up to you to put it there.
    you need to "READ"the data.
    look here.
    http://msdn.microsoft.com/en-us/library/30swa673.aspx
    and
    http://msdn.microsoft.com/en-us/libr...port.read.aspx

    What sort of data are you expecting from your serial port?

    bytes? strings of human readable text?

    Also Im sure other more knowledgable people will be along to help as well.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Listening to the Serial Port - Need help

    Quote Originally Posted by 2ndmessiah View Post
    What sort of data are you expecting from your serial port?
    bytes? strings of human readable text?
    Also Im sure other more knowledgable people will be along to help as well.
    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 Attached Images  

  4. #4
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Listening to the Serial Port - Need help

    It would be simpler if you simply dropped a SerialPort control from the toolbox onto the design surface. When you do that, the event handler is built for you. If you want to do this in code, the correct declaration would be

    Code:
        Private WithEvents SerialPort As New System.IO.Ports.SerialPort
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            With SerialPort
                .PortName = "Com2"   'for example
                .BaudRate = 9600
                .RtsEnable = True
                .Open()
                'etc.
            End With
        End Sub
    
        Private Sub SerialPort_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
            Dim DataFromPort As String = SerialPort.ReadExisting
            Debug.Print(DataFromPort)
        End Sub
    Now, this is pretty simple-minded, and is only a starting point. If you go to my website, www.hardandsoftware.net and download Enhanced SerialPort (this is free), I have example code that is more complete. The example code can be used with the built-in SerialPort object, as long as you do not call any of the Enhanced SerialPort extensions -- however, you might as well use Enhanced SerialPort, since it is just a wrapper for SerialPort, and does not remove any functions.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width