User Tools

Site Tools


info:vb_serial_comm

This is an old revision of the document!


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 method Here's an example: Dim moRS232 as New Rs232() With moRs232            .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 End With ' Initializes and Open moRS232.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.

info/vb_serial_comm.1216462665.txt.gz · Last modified: 2008/07/19 06:17 by tomgee