Creating a Serial Port in code in

I am trying to create a serial port in 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.


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()
        comPort = My.Computer.Ports.OpenSerialPort("COM5", 9600)
    End Try
End Sub

Public Sub Disconnect()

    If comPort IsNot Nothing AndAlso comPort.IsOpen Then
    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
            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)

    End If

    RaiseEvent ScanDataRecieved(str)

End Sub
End Class

Need Your Help

Change GET REQUEST header of an iframe via browser extension

javascript http firefox iframe

after some serious googleing AFAIK, there is no way to change http request header of an iframe. Since that is VERY important for our application, is there a way to do it via browser extensions (Fir...

Passing parameters to TestDelegate in NUnit

c# unit-testing delegates nunit

I am trying to create a method that takes a testdelegate or delegate and passes parameters to the delegate object. This is because I am creating a test for a methods in controllers that all takes the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.