SerialPort.DataReceived Event : SerialPort « Development « VB.Net






SerialPort.DataReceived Event

 

Imports System
Imports System.IO.Ports

Class PortDataReceived
    Public Shared Sub Main()
        Dim mySerialPort As New SerialPort("COM1")

        mySerialPort.BaudRate = 9600
        mySerialPort.Parity = Parity.None
        mySerialPort.StopBits = StopBits.One
        mySerialPort.DataBits = 8
        mySerialPort.Handshake = Handshake.None

        AddHandler mySerialPort.DataReceived, AddressOf DataReceviedHandler

        mySerialPort.Open()

        Console.WriteLine("Press any key to continue...")
        Console.ReadKey()
        mySerialPort.Close()
    End Sub

    Private Shared Sub DataReceviedHandler(sender As Object,e As SerialDataReceivedEventArgs)
        Dim sp As SerialPort = CType(sender, SerialPort)
        Dim indata As String = sp.ReadExisting()
        Console.WriteLine("Data Received:")
        Console.Write(indata)
    End Sub
End Class

   
  








Related examples in the same category

1.SerialPort Class Represents a serial port resource.
2.Available Ports
3.Available Parity options
4.Available Stop Bits options
5.Available Handshake options
6.SerialPort.GetPortNames Method Gets an array of serial port names for the current computer.