vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

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


发布日期:2023年01月20日
 
一个基于API的VB.net串口通讯类

的串口通讯支持总是让人觉得有所不足在使用VB的MsCommocx时很多人都会在的开发中觉得很困扰

这里讲述的串口通讯类使用native代码并且它是通API调用实现的你会发现的串口通讯就是这么简单

在说明如何使用这个类前需要说明的是本类只是一个的串口通讯演示你可能需要根据你的情况修改后使用另外本类的目的是通过例子教会你在无需ocx控件和第三方组件的支持下用开发串口通讯程序所有并没有完善的异常错误处理

初始化并打开串口

创建一个CRs类的实例并在调用Open方法前设置好串口通讯参数

Dim moRS as New Rs()

With moRs

Port = //Uses COM

BaudRate = //波特率

DataBit = // data bits

StopBit = RsDataStopBitStopBit_//停止位

Parity = RsDataParityParity_None//无奇偶校验

Timeout = //超时时间 ms

End With

// 初始化并打开串口

moRSOpen ()

// 串口打开后你可以随意地控制DTR/RTS

moRSDtr = True

moRSRts = True

为了处理异常情况建议你使用TryCatch

发送数据

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

例如

moRSTxData = txtTxText

moRSTx()

接收数据

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

例如

moRSRx() // 从串口通讯缓沖区里读取字节

Dim sRead as String=moRsRxData

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

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

版本信息

Project History

st Public release Beta (//)

Rev ()

Added ResetDev SetBreak and ClearBreak to the EscapeCommFunction constants

Added the overloaded Open routine

Added the modem status routines properties and enum

If a read times out it now returns a EndOfStreamException (instead of a simple Exception)

Compiled with VSNet final

Rev ()

Added Async support

Rev ()

Minor bugs fixed

Reva (//)

Fixed BuildCommmDCB problem

Rev (//)

Fixed problem with ASCII Encoding truncating th bit

Rev (//)

Added IDisposable / Finalize implementation

Rev (//)

Fixed problem on DCB fields Initialization

Rev (//)

Added XON/XOFF support

Rev (//)

Added support to COM port number greater than

Rev (//)

Added CommEvent to detect incoming chars/events(!)

Updated both Tx/Rx method from NonOvelapped to Overlapped mode

Removed unused Async methods and other stuff

Rev (//)

Fixed incorrect character handling when using EnableEvents()

Rev (//)

Fixed some bugs reported by users

Rev (//)

Removed AutoReset of internal buffers and added PurgeBuffer() method

Rev (//)

Update internal stuff now using WinException instead of GetLastError+FormatMessage APIs

Rev (//)

Added IsPortAvailable() function (thanks to Tom Lafleur for the hint)

Revised some API declaration

Fixed some problems with Win/Me OS (thanks to Alex Komissarov for the feedback)

Rev (//)

Fixed bug introduced on rev (sorry for that)

Rev (//)

Added SetBreak/ClearBreak methods for sending break signal over the line

Rev (//)

Fixed incorrect field on COMMCONFIG Structure

Rev (//)

Fixed bug causing troubles accessing already in use ports (folks thanks for the feedback!)

Rev (//)

Fixed bug on DTR property (thanks to CharlesOlivier Théroux)

Rev (//)

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 (//)

Fixed EscapeCommFunction declaration

Fixed incorrect Pariti enum entry

Rev (//)

Fixed memory leak causing random program termination without any message

Thanks to Ralf Gedrat for testing this scenario

Rev (//)

Fixed bug DisableEvents not working bug (Thanks to Jean Bédard)

上一篇:如何应用VB.NET MonthCalendar控件

下一篇:VB.Net编程实现Web Service的基础