Huwebes, Enero 31, 2013

Getting Data From Serial Port

Many are wondering on how to get data from a serial port using visual basic.net

I will be helping you with the one I have created using RS232 connector.Where in I also googled and just want to share and to help.

First...
       * Create a Project name it whatever you want.
       * Add Labels, 2 Combobox and a Textbox
       * Add Serialport Component from the Toolbox      

       Here a sample GUI:



 Heres the code:


Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports

'Added to prevent threading errors during receiveing of data


Public Class Form1
    Dim myPort As Array  'COM Ports detected on the system will be stored here

    Delegate Sub SetTextCallback(ByVal [text] As String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available

        cmbbaud.Items.Add(9600)     'Populate the cmbBaud Combo box to common baud rates used
        cmbbaud.Items.Add(19200)
        cmbbaud.Items.Add(38400)
        cmbbaud.Items.Add(57600)
        cmbbaud.Items.Add(115200)

        For i = 0 To UBound(myPort)
            cmbport.Items.Add(myPort(i))
        Next

        cmbport.Text = cmbport.Items.Item(0)    'Set cmbPort text to the first COM port detected

        cmbbaud.Text = cmbbaud.Items.Item(0)    'Set cmbBaud text to the first Baud rate on the list

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.PortName = cmbport.Text
        'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = cmbbaud.Text
        'Set Baud rate to the selected value on
        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8            'Open our serial port
        SerialPort1.Open()

    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        ReceivedText(SerialPort1.ReadExisting())    'Automatically called every time a data is received at the serialPort
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread

        If Me.TextBox1.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})

        Else
            Me.TextBox1.Text &= [text]
        End If
    End Sub

    Private Sub cmbport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbport.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbport.Text         'pop a message box to user if he is changing ports
        Else                                                                               'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If

    End Sub

    Private Sub cmbbaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbbaud.Text         'pop a message box to user if he is changing baud rate
        Else                                                                                'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If

    End Sub
End Class



Walang komento:

Mag-post ng isang Komento