locked
SerialPort RRS feed

  • Question

  • Hi,

    I have created a serial port using the SerialPort class,

    Private serialPort As SerialPort,

    and intialized it,

    serialPort = New SerialPort().

     

    I have been using it to write and it works fine. I need also to read from the port and am not totally sure how. I have seen some previous posts where the SerialPort was dragged from the toolbox and then some events were used from the properties.

    I have a couple of questions.

    When creating the port in code as I have done how does one get at the events?

     

    I am trying to do something like below but not sure if this is correct, and I think that would go in the serial ports DataRecieved event. In the while I would then process the port data.

     

     

    Dim data As String

    data = serialPort.ReadExisting()

    While (serialPort.ReadByte > 0)

     

    End While

     

     

    Any Help, examples would be appreciated.

     

    Thanks

    Jeff

     

    Tuesday, April 17, 2007 8:16 PM

Answers

  • You can use AddHandler to have a routine in your app handle an event from an object (in your case, the DataReceived event).
    Tuesday, April 17, 2007 8:28 PM
  • Here's some code that shows how to create and read data from a serial port

     

    connection = New IO.Ports.SerialPort(Me.mPortName, Me.mBaudRate, Me.mParity, Me.mDataBits, Me.mStopBits)

    connection.ReceivedBytesThreshold = Me.mReceivedBytesThreshold

     

    AddHandler connection.DataReceived, New IO.Ports.SerialDataReceivedEventHandler(AddressOf Me.DataReceived)

     

    connection.Open()

     

    Now the contents of the DataReceived method that reads the incoming data

     

    Protected Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

    Dim serialPort As IO.Ports.SerialPort = TryCast(sender, IO.Ports.SerialPort)

    If serialPort Is Nothing Then Return 

    If Not serialPort.IsOpen Then Return

    If serialPort.BytesToRead <= 0 Then Return

    Dim msg As String = serialPort.ReadExisting

    End Sub

     

    It's by no means complete, but it'll get you started.

     

    Hope it helps.

     

    Richard

    Thursday, April 19, 2007 9:52 AM

All replies

  • You can use AddHandler to have a routine in your app handle an event from an object (in your case, the DataReceived event).
    Tuesday, April 17, 2007 8:28 PM
  • Here's some code that shows how to create and read data from a serial port

     

    connection = New IO.Ports.SerialPort(Me.mPortName, Me.mBaudRate, Me.mParity, Me.mDataBits, Me.mStopBits)

    connection.ReceivedBytesThreshold = Me.mReceivedBytesThreshold

     

    AddHandler connection.DataReceived, New IO.Ports.SerialDataReceivedEventHandler(AddressOf Me.DataReceived)

     

    connection.Open()

     

    Now the contents of the DataReceived method that reads the incoming data

     

    Protected Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

    Dim serialPort As IO.Ports.SerialPort = TryCast(sender, IO.Ports.SerialPort)

    If serialPort Is Nothing Then Return 

    If Not serialPort.IsOpen Then Return

    If serialPort.BytesToRead <= 0 Then Return

    Dim msg As String = serialPort.ReadExisting

    End Sub

     

    It's by no means complete, but it'll get you started.

     

    Hope it helps.

     

    Richard

    Thursday, April 19, 2007 9:52 AM
  • Thanks to both of you for the help and the direction. I am now able to read incoming data off of the serial port.

    Jeff

    Thursday, April 19, 2007 12:30 PM