VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net教程之VB.net的串口通讯支持

VB.net的串口通讯支持总是让人觉得有所不足,在使用VB6的MsComm32.ocx时,很多人都会在VB.net的开发中觉得很困扰。
这里讲述的VB.net串口通讯类使用native代码,并且它是通API调用实现的,你会发现VB.net的串口通讯就是这么简单。
 
在说明如何使用这个类前,需要说明的是,本类只是一个VB.net的串口通讯演示,你可能需要根据你的情况修改后使用。另外,本类的目的是通过例子教会你在无需ocx控件和第三方组件的支持下用VB.net开发串口通讯程序,所有并没有完善的异常错误处理。
 
.初始化并打开串口
创建一个CRs232类的实例,并在调用Open方法前设置好串口通讯参数。
例:
Dim moRS232 as New Rs232()
With moRs232
           .Port = 1                                                    '// Uses COM1
           .BaudRate = 2400                                     '// 波特率 2400
           .DataBit = 8                                              '// 8 data bits                                 
           .StopBit = Rs232.DataStopBit.StopBit_1         '// 停止位 1
           .Parity = Rs232.DataParity.Parity_None          '// 无奇偶校验
           .Timeout = 500                                                '//超时时间500 ms
End With
 
'// 初始化并打开串口
moRS232.Open ()
'// 串口打开后,你可以随意地控制DTR/RTS
moRS232.Dtr = True
moRS232.Rts = True
 
为了处理异常情况,建议你使用Try...Catch
 
 
.发送数据
本类为RxTx准备了两个缓冲区(buffer),发送数据时,只需要设置TxData属性为你需要发送的数据,然后调用Tx方法就可以了。
例如:
moRS232.TxData = txtTx.Text
moRS232.Tx()
 
 
3.接收数据
先调用Rx方法(参数为您需要从串口读取的字节数),然后读取RxData属性。
例如:
moRS232.Rx(10)                       '// 从串口通讯缓冲区里读取10字节
Dim sRead as String=moRs232.RxData
 
需要注意的是,当本类无法用串口读取到所需的字节数,程序线程在超时异常发生前是锁死的,超时时间通过Timout属性设置。
如果你没有指明需要读取的字节数,本类默认使用512字节来读取缓冲区里的数据。
 
http://www.biplip.com/Portals/0/NewsImgs/264-8843.gif
 
 
本文代码由www.codeworks.it提供
版本信息:
Project History
1st Public release Beta2 (10/08/2001)
Rev.1 (28.02.2002)
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
Rev.2 (01.03.2002)
Added Async support
Rev.3 (07.04.2002)
Minor bugs fixed
Rev.3a (05/05/2002)
Fixed BuildCommmDCB problem
Rev.4 (24/05/2002)
Fixed problem with ASCII Encoding truncating 8th bit
Rev.5 (27/05/2002)
Added IDisposable / Finalize implementation
Rev.6 (14/03/2003)
Fixed problem on DCB fields Initialization
Rev.7 (26/03/2003)
Added XON/XOFF support
Rev.8 (12/07/2003)
Added support to COM port number greater than 4
Rev.9 (16/07/2003)
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.
Rev.10 (21/07/2003)
Fixed incorrect character handling when using EnableEvents()
Rev.11 (12/08/2003)
Fixed some bugs reported by users
Rev.12 (01/09/2003)
Removed AutoReset of internal buffers and added PurgeBuffer() method
Rev.13 (02/09/2003)
Update internal stuff now using Win32Exception instead of GetLastError+FormatMessage APIs
Rev.14 (14/09/2003)
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)
Rev.15 (24/09/2003)
Fixed bug introduced on rev.14 (sorry for that...)
Rev.16 (16/10/2003)
Added SetBreak/ClearBreak methods for sending break signal over the line.
Rev.17 (02/11/2003)
Fixed incorrect field on COMMCONFIG Structure.
Rev.18 (03/03/2004)
Fixed bug causing troubles accessing already in use ports (folks, thanks for the feedback!)
Rev.19 (08/04/2004)
Fixed bug on DTR property (thanks to Charles-Olivier Théroux)
Rev.20 (12/07/2004)
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
Rev.21 (24/10/2004)
Fixed EscapeCommFunction declaration
Fixed incorrect Pariti enum entry
Rev.22 (05/03/2005)
Fixed memory leak causing random program termination without any message.
Thanks to Ralf Gedrat for testing this scenario.
Rev.23 (05/04/2005)
Fixed bug DisableEvents not working bug (Thanks to Jean Bédard)

返回
 
http://www.biplip.com/Portals/0/Containers/BiplipContainer/spacer.gif
 
*        http://www.biplip.com/Portals/0/Containers/BiplipContainer/spacer.gif http://www.biplip.com/Portals/0/Containers/BiplipContainer/spacer.gif
 
TRT7H-KD36T-FRH8D-6QH8P-VFJHQ

相关教程