资料
  • 资料
  • 专题
一个基于API的VB.net串口通讯类(源码更新Rev.23)
推荐星级:
时间:2019-12-23
大小:22.16KB
阅读数:191
上传用户:二不过三
查看他发布的资源
下载次数
0
所需E币
4
ebi
新用户注册即送 300 E币
更多E币赚取方法,请查看
close
资料介绍
VB.net的串口通讯支持总是让人觉得有所不足,在使用VB6的MsComm32.ocx时,很多人都会在VB.net的开发中觉得很困扰。这里讲述的VB.net串口通讯类使用native代码,并且它是通API调用实现的,你会发现VB.net的串口通讯就是这么简单。在说明如何使用这个类前,需要说明的是,本类只是一个VB.net的串口通讯演示,你可能需要根据你的情况修改后使用。另外,本类的目的是通过例子教会你在无需ocx控件和第三方组件的支持下用VB.net开发串口通讯程序,所有并没有完善的异常错误处理。1.初始化并打开串口创建一个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 = TruemoRS232.Rts = True 为了处理异常情况,建议你使用Try...Catch。2.发送数据本类为Rx和Tx准备了两个缓冲区(buffer),发送数据时,只需要设置TxData属性为你需要发送的数据,然后调用Tx方法就可以了。例如:moRS232.TxData = txtTx.TextmoRS232.Tx() 3.接收数据先调用Rx方法(参数为您需要从串口读取的字节数),然后读取RxData属性。例如:moRS232.Rx(10) '// 从串口通讯缓冲区里读取10字节 Dim sRead as String=moRs232.RxData 需要注意的是,当本类无法用串口读取到所需的字节数,程序线程在超时异常发生前是锁死的,超时时间通过Timout属性设置。如果你没有指明需要读取的字节数,本类默认使用512字节来读取缓冲区里的数据。本文代码由www.codeworks.it提供版本信息:Project History1st Public release Beta2 (10/08/2001)Rev.1 (28.02.2002)1. Added ResetDev, SetBreak and ClearBreak to the EscapeCommFunction constants2. 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 finalRev.2 (01.03.2002)Added Async supportRev.3 (07.04.2002)Minor bugs fixedRev.3a (05/05/2002)Fixed BuildCommmDCB problemRev.4 (24/05/2002)Fixed problem with ASCII Encoding truncating 8th bitRev.5 (27/05/2002)Added IDisposable / Finalize implementationRev.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 modeRemoved 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 usersRev.12 (01/09/2003)Removed AutoReset of internal buffers and added PurgeBuffer() methodRev.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 declarationFixed 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 declarationFixed incorrect Pariti enum entryRev.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)……
版权说明:本资料由用户提供并上传,仅用于学习交流;若内容存在侵权,请进行举报,或 联系我们 删除。
相关评论 (下载后评价送E币 我要评论)
没有更多评论了
  • 可能感兴趣
  • 关注本资料的网友还下载了
  • 技术白皮书