您现在的位置是:首页 >

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

火烧 2021-11-23 20:28:16 1037
一个基于API的VB. et串口通讯类   的串口通讯支持总是让人觉得有所不足 在使用VB 的M Comm ocx时 很多人都会在的开发中觉得很困扰     这里讲述的串口通讯类使用 ative代码

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

用VB 一个基于API的VB.net串口通讯类
  的串口通讯支持总是让人觉得有所不足 在使用VB 的MsComm ocx时 很多人都会在的开发中觉得很困扰     这里讲述的串口通讯类使用native代码 并且它是通API调用实现的 你会发现的串口通讯就是这么简单     在说明如何使用这个类前 需要说明的是 本类只是一个的串口通讯演示 你可能需要根据你的情况修改后使用 另外 本类的目的是通过例子教会你在无需ocx控件和第三方组件的支持下用开发串口通讯程序 所有并没有完善的异常错误处理     1 初始化并打开串口    创建一个CRs 类的实例 并在调用Open方法前设置好串口通讯参数     例     Dim moRS as New Rs ()    With moRs      Port =                            // Uses      BaudRate =                     // 波特率      DataBit =                         //  data bits     StopBit = Rs DataStopBit StopBit_        // 停止位      Parity = Rs DataParity Parity_None       // 无奇偶校验     Timeout =                       //超时时间 ms    End With       // 初始化并打开串口    moRS Open ()     // 串口打开后 你可以随意地控制DTR/RTS    moRS Dtr = True    moRS Rts = True    为了处理异常情况 建议你使用Try Catch     2 发送数据    本类为Rx和Tx准备了两个缓冲区(buffer) 发送数据时 只需要设置TxData属性为你需要发送的数据 然后调用Tx方法就可以了     例如     moRS TxData = txtTx Text    moRS Tx()     接收数据    先调用Rx方法(参数为您需要从串口读取的字节数) 然后读取RxData属性     例如     moRS Rx( )            // 从串口通讯缓冲区里读取 字节    Dim sRead as String=moRs RxData    需要注意的是 当本类无法用串口读取到所需的字节数 程序线程在超时异常发生前是锁死的 超时时间通过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 VS Net final    Rev ( )  Added Async support    Rev ( )  Minor bugs fixed    Rev a ( / / )  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 port number greater than     Rev ( / / )  Added CommEvent to detect ining chars/events(!)  Updated both Tx/Rx method from Non Ovelapped 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 Win Exception 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 MCONFIG Structure     Rev ( / / )  Fixed bug causing troubles accessing already in use ports (folks thanks for the feedback!)    Rev ( / / )  Fixed bug on DTR property (thanks to Charles Olivier 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) lishixinzhi/Article/program/net/201311/11852  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码