Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Serial Port in code in VB.net

I am trying to create a serial port in VB.net using code only. Because I am creating a class library I cannot use the built-in component. I have tried instantiating a new SeialPort() object, but that does not seem to be enough. I'm sure there is something simple I am missing and any help would be greatly appreciated! Thanks!

P.S. I should add that the problem I am having at this time is getting the code to handle the datareceived event. Other than that it might be working, but I can't tell because of that problem.

like image 804
Kyle Gibbons Avatar asked Dec 01 '08 02:12

Kyle Gibbons


People also ask

How do I setup a serial port?

To configure the Serial Port for your device, on your computer go to Control Panel - Device Manager, select “High-Speed USB Serial Port (Com X)”, right click and select Properties. Click the Features tab. This tab is used to change the COM port number and configure the port.


2 Answers

If you want to use the events make sure you declare your serialPort object using the 'withevents'. The below example will allow you to connect to a serial port, and will raise an event with the received string.

Imports System.Threading

Imports System.IO

Imports System.Text

Imports System.IO.Ports


Public Class clsBarcodeScanner

Public Event ScanDataRecieved(ByVal data As String)
WithEvents comPort As SerialPort

Public Sub Connect()
    Try
        comPort = My.Computer.Ports.OpenSerialPort("COM5", 9600)
    Catch
    End Try
End Sub

Public Sub Disconnect()

    If comPort IsNot Nothing AndAlso comPort.IsOpen Then
        comPort.Close()
    End If

End Sub

Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
    Dim str As String = ""
    If e.EventType = SerialData.Chars Then
        Do
            Dim bytecount As Integer = comPort.BytesToRead

            If bytecount = 0 Then
                Exit Do
            End If
            Dim byteBuffer(bytecount) As Byte


            comPort.Read(byteBuffer, 0, bytecount)
            str = str & System.Text.Encoding.ASCII.GetString(byteBuffer, 0, 1)

        Loop
    End If

    RaiseEvent ScanDataRecieved(str)

End Sub
End Class
like image 182
Dilbert789 Avatar answered Sep 18 '22 01:09

Dilbert789


I found this article to be quite good.

The code i wrote from it is:

port = new System.IO.Ports.SerialPort(name, 4800, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
port.Open();

void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    buffer = port.ReadLine();
    // process line
}

Sorry it's C# but...

The only issue I have with it is if the port is dropped while it's open, the app seems to fail on exit.

like image 30
BCS Avatar answered Sep 18 '22 01:09

BCS