Serial Communication with VB.Net
One of the thing I really miss while using VB.Net is the lack
of serial communication support.Yes, you can use VB6's
MsComm32.ocx but if you, like me, have installed VB.Net
on a separate hard disk you may experience some problems
installing and using it in design mode.
Don't forget moreover that if you use that control, you must register it on user machine when you deploy your application, loosing the 'XCopy installation mode' provided by VB.Net.
With this class you can easily use serial communication using native VB.Net and API features.
Before describing you how to use the class, let me point out that I've just written it as a demonstration of VB.Net interface to serial communication, you'd probably need to customize it for your special 'Rs232' needs.
Initializing and Opening the Com port
Create an instance of CRs232 then set COM parameters before
invoking the Open
Here's an example:
Dim moRS232 as New Rs232()
.Port = 1 '// Uses COM1
.BaudRate = 2400 ' // 2400 baud rate
.DataBit = 8 ‘// 8 data bits
.StopBit = Rs232.DataStopBit.StopBit_1 '// 1 Stop bit
.Parity = Rs232.DataParity.Parity_None '// No Parity
.Timeout = 500 '// 500 ms of timeout admitted to get all required bytes
'// Initializes and Open
You can, optionally control the state of DTR/RTS lines after the Port is open
'// Set state of RTS / DTS
moRS232.Dtr = True
moRS232.Rts = True
In case of an error/problems an exception is raised, so i suggest you to enclose the code within a Try...Catch block.
Transmitting data to COM Port
The class has 2 buffers one for Tx and one for Rx, to transmit data just set the TxData property with the informations you wish to send then invoke the Tx method.
Receiving data from COM Port
Just invoke the Rx method passing it the number of bytes you want to read from COM port, then read the Rxdata property.
moRS232.Read(10) '// Gets 10 bytes from serial communication buffer
Dim sRead as String=moRs232.InputStreamString
Please note that thread is blocked for the period of time set by Timeout property and that in case the class cannot read all required bytes from COM buffer a Timeout exception is raised.
If the number of bytes to read is omitted, the class assumes 512 bytes have to be read from buffer.
The class is very simple and surely misses some error control, but as prefaced I just wanted to give you an example of what you can do with VB.Net without resorting to ocx'es or other 3rdy parties controls
The zip file that you can download here includes also a small Windows Form example that can help you understand how to use my class.
How to use the sample
If not enough character are received inside
allocated timeout, you will have an error and you will se what has
been read from serial port.
RTS and DTR checkboxes will allow you to
change the status of RTS and DTR lines respectively.
Events are signaled by a CommEvent event, and
you can use Mask parameter to detect what event has occurred, if you
set a number of character into Bytes to read texbox, as soon as
those character arrives into serial port you will get a CommEvent
that you can use to get received characters.
You can download latest version
here (example requires Visual Studio
Previous version is available here
To load my project on Visual Studio 2002
All code is freely redistributable, just let me know if you enjoy using it....
1st Public release Beta2 (10/08/2001)
1. Added ResetDev, SetBreak and ClearBreak to the EscapeCommFunction constants
2. Added the overloaded Open routine.
3. Added the modem status routines, properties and enum.
4. If a read times out, it now returns a EndOfStreamException (instead of a simple Exception).
5.Compiled with VS.Net final
Added Async support
Minor bugs fixed
Fixed BuildCommmDCB problem
Fixed problem with ASCII Encoding truncating 8th bit
Added IDisposable / Finalize implementation
Fixed problem on DCB fields Initialization
Added XON/XOFF support
Added support to COM port number greater than 4
Added CommEvent to detect incoming chars/events(!)
Updated both Tx/Rx method from Non-Ovelapped to Overlapped mode
Removed unused Async methods and other stuff.
Fixed incorrect character handling when using EnableEvents()
Fixed some bugs reported by users
Removed AutoReset of internal buffers and added PurgeBuffer() method
Update internal stuff now using Win32Exception instead of GetLastError+FormatMessage APIs
Added IsPortAvailable() function (thanks to Tom Lafleur for the hint)
Revised some API declaration
Fixed some problems with Win98/Me OS (thanks to Alex Komissarov for the feedback)
Fixed bug introduced on rev.14 (sorry for that...)
Added SetBreak/ClearBreak methods for sending break signal over the line.
Fixed incorrect field on COMMCONFIG Structure.
Fixed bug causing troubles accessing already in use ports (folks, thanks for the feedback!)
Fixed bug on DTR property (thanks to Charles-Olivier Théroux)
CommEvent is no more raised on a secondary thread (please note that this is valid only if event handler is not associated with a static method)
pEventsWatcher now uses a background thread
Fixed EscapeCommFunction declaration
Fixed incorrect Pariti enum entry
Fixed memory leak causing random program termination without any message.
Thanks to Ralf Gedrat for testing this scenario.
Fixed bug DisableEvents not working bug (Thanks to Jean Bédard)
Fixed memory leak on Read() method
Added InBufferCount property
IsPortAvailable method is now shared (Thanks to Jean-Pierre ZANIER for the feedback)
back to home page