473,467 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Example of How to Implement SerialPort Class

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
Jan 6 '06 #1
4 29261
I forgot to mention I am using VS2005.

Ben
Jan 6 '06 #2
See
http://msdn.microsoft.com/vbasic/def...cyhardware.asp
"Ben Kim" <bk**@NOSPAMemergitech.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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

Jan 7 '06 #3
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

"Chuck Gantz" <cg********@yahoo.com> wrote in message
news:dDTvf.386$sa4.134@trnddc07...
See
http://msdn.microsoft.com/vbasic/def...cyhardware.asp
"Ben Kim" <bk**@NOSPAMemergitech.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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


Jan 10 '06 #4
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
Jan 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Moore | last post by:
I hit a problem yesterday with my mail connection. In a desparate attempt to understand what was going on, I wanted to log the connection traffic. After a bit of searching, I found a post on c.l.p...
13
by: Mtk | last post by:
Hi! Why does the following, simple, example produce such errors? I know it has to do with the two header files including each other and (moreover) the usage of the classes One and Two in the...
0
by: Nathan Ie | last post by:
Hi there! Not sure how many of you out there are familiar with the Serialport Class, but I'm trying to add an event handler for every time I receive new incoming data from the serial port by...
2
by: Peter Oliphant | last post by:
On this MSDN2 webpage describing the Framework 2.0 SerialPort class: http://msdn2.microsoft.com/en-us/library/30swa673(en-US,VS.80).aspx there is the following text under 'Example': "The...
0
by: no-one | last post by:
Hi Guys, I am fairly new to Dot Net and I am trying to write a program that listens to the serial port and displays the string in a text box or listbox. I am using VS 2005 and framework 2.0 ...
5
by: herbert | last post by:
back in 1978 (!) the VAX/VMS serial line driver offered everything a developer needs to develop protocols of all kinds: - read x bytes - read to end of line - read to special character - read...
2
by: awu | last post by:
All: I need to use SerialPort class in .NET MFC application. I need to create a thread for write/read of the port. Someone can tell me where I can find a good example? All the example using...
0
by: =?Utf-8?B?VG9tYnN0b25l?= | last post by:
Hi All, I have written an application which uses the .NET 2 SerialPort class to send data to an LCD display device. The idea is to be able to send simple text data to an RDP client connected...
2
by: Kevin | last post by:
Does anyone out there happen to have experience using VC# (or any .NET really) to control the serial port for demanding use? My ultimate goal would be to stream an arbitrarily large size (MB+)...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.