using the serial port class in VB

Are there any samples around for using the serial port class in the framework for VB

All I want to do is read some data off the port.....

thanks....
Bruce.


Answer this question

using the serial port class in VB

  • mike_dowler

    Private WithEvents objPort As System.IO.Ports.SerialPort
        Private Sub frmGPSReader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                        If InitComPort("COM1") Then
                            If OpenComPort() Then
                                blnReturn = True
                            End If
                        End If
        End Sub
        Friend Function InitComPort(ByVal strPortName As String) As Boolean

            Dim blnOK As Boolean = False

            Try
                If objPort Is Nothing Then
                    objPort = New SerialPort(strPortName, 4800)
                End If

                objPort.ReceivedBytesThreshold = 512 '<- set limit before objPort_DataReceived event fires

                blnOK = True

            Catch ex As Exception
                blnOK = False

            End Try

            Return blnOK

        End Function
        Friend Function OpenComPort() As Boolean

            Dim blnOK As Boolean = False

            Try
                If objPort Is Nothing Then
                    blnOK = False
                Else
                    If objPort.IsOpen Then
                        blnOK = True
                    Else
                        objPort.Open()
                        blnOK = objPort.IsOpen

                    End If
                End If

            Catch ex As Exception
                blnOK = False

            End Try

            Return blnOK

    WARNING: objPort.ReadBufferSize = 8192 <- Has no effect on my device, max is 2047

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

            Dim strGPSData as String = objPort.ReadExisting

        End Sub

    voila!

  • Display Name*

    I worked on a VS2005  program many months ago to explore and use the com serial class with desktop CF2 (VS2005 Beta 1).  I  developed from a sample I found elsewhere.  It fully exercises the desktop serial class.

    I then ported it to  a smartdevice but ran into one problem.

    With the full FW you can get a list of all available COM ports.  This then is used as a dropdownlist to choose the COM port.  This capability is not available under CF.  It could be replaced with a manually entered list, but I haven't added this yet.

    You can get the projects from http://babbage.ece.rmit.edu.au/downloads  and use that as a start.

  • David PROVOST

    Hi Bruce,

    You can find a sample of managed serial port communication here:

    http://www.codeproject.com/csharp/SerialCommunication.asp

    The project is in C#, but you should be able to convert it to VB easily.

    In NETCF V2 Beta 2, we added the SerialPort.GetPortNames() method that returns an array of serial port names for the current device. You can find the documentation here:

    http://msdn2.microsoft.com/en-us/library/0st1yts8

    Cheers,
    Anthony Wong [MSFT]

  • FMartins

    I think it was the GetPortNames() method that wasn't available in the CF.
  • using the serial port class in VB