熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

一個基於API的VB.net串口通訊類

2013-11-13 09:52:55  來源: .NET編程 

  的串口通訊支持總是讓人覺得有所不足在使用VB的MsCommocx時很多人都會在的開發中覺得很困擾
  
  這裡講述的串口通訊類使用native代碼並且它是通API調用實現的你會發現的串口通訊就是這麼簡單
  
  在說明如何使用這個類前需要說明的是本類只是一個的串口通訊演示你可能需要根據你的情況修改後使用另外本類的目的是通過例子教會你在無需ocx控件和第三方組件的支持下用開發串口通訊程序所有並沒有完善的異常錯誤處理
  
  1初始化並打開串口
  
  創建一個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
  
  2發送數據
  
  本類為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)
From:http://tw.wingwit.com/Article/program/net/201311/11852.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.