分享
 
 
 

一个基于API的VB.net串口通讯类

王朝c#·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

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 = True

moRS232.Rts = True

为了处理异常情况,建议你使用Try...Catch。

2.发送数据

本类为Rx和Tx准备了两个缓冲区(buffer),发送数据时,只需要设置TxData属性为你需要发送的数据,然后调用Tx方法就可以了。

例如:

moRS232.TxData = txtTx.Text

moRS232.Tx()

3.接收数据

先调用Rx方法(参数为您需要从串口读取的字节数),然后读取RxData属性。

例如:

moRS232.Rx(10)

'// 从串口通讯缓冲区里读取10字节

Dim sRead as String=moRs232.RxData

需要注意的是,当本类无法用串口读取到所需的字节数,程序线程在超时异常发生前是锁死的,超时时间通过Timout属性设置。

如果你没有指明需要读取的字节数,本类默认使用512字节来读取缓冲区里的数据。

版本信息:

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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有