Simple Example of How to Implement SerialPort Class

  • Thread starter Thread starter Ben Kim
  • Start date Start date
B

Ben Kim

Hello all,

Does anyone have an example on how to implement the new SerialPort Class in
VB.NET? I have been able to create the class, send (.WriteLine) to the port
and Read from the port but cannot figure out how to use the Events to tell
my program that data is available in the serial port buffer.

I have searched the internet and MSDN and there doesn't appear to be any
full example on how to do this simple task.

Any help would be greatly appreciated!

Ben Kim
 
Thank you Chuck. Exactly what I was looking for. However this has brought
up a different issue.

Here is the code I created based on the example you gave. However I get an
exception error whenever the remote port responds with text:
"InvalidOperationException was unhandled", Cross-thread operation not valid:
Control 'txtOutput' accessed from a thread other than the thread it was
created on.

Code:
--------------------------------
Imports System.IO.Ports

Public Class Form1
Dim WithEvents mySerialPort As SerialPort = New
System.IO.Ports.SerialPort

Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpenPort.Click
With mySerialPort
.PortName = "COM" & txtPortNum.Text
.BaudRate = 57600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
End With

mySerialPort.Open()
End Sub

Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSendText.Click
mySerialPort.Write(txtSendText.Text)
End Sub

Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClosePort.Click
mySerialPort.Close()
End Sub

Private Sub mySerialPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles mySerialPort.DataReceived
txtOutput.Text = txtOutput.Text + mySerialPort.ReadExisting()
'Console.Write(mySerialPort.ReadExisting())
End Sub
End Class
--------------------------------
After reading the help about this specific error I am not sure I understand
why mySerialPort is running on another thread since it is part of my form
class.

Any ideas?

Ben Kim
 
OK got it to work. Seems like a lot of work for something that should be
inherently thread safe already (Window form controls). You have to create a
delegate that enables asynchronous calls to the window control in this case
txtOutput.

Here is the code for all interested in how the new SerialPort object class
works.

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

Public Class Form1
Dim WithEvents mySerialPort As SerialPort = New
System.IO.Ports.SerialPort

' This delegate enables asynchronous calls for setting
' the text property on a TextBox control.
Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpenPort.Click
With mySerialPort
.PortName = "COM" & txtPortNum.Text
.BaudRate = 57600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
End With

mySerialPort.Open()
End Sub

Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSendText.Click
mySerialPort.Write(txtSendText.Text)
End Sub

Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClosePort.Click
mySerialPort.Close()
End Sub

Private Sub mySerialPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles mySerialPort.DataReceived
SetText(mySerialPort.ReadExisting())
'Console.Write(mySerialPort.ReadExisting())
End Sub

Private Sub SetText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.txtOutput.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.txtOutput.Text &= [text]
End If
End Sub

End Class
 
Back
Top