vb.net串口通訊 VB串口通訊

一個(gè)基于API的VB.net串口通訊類(lèi)

的串口通訊支持總是讓人覺(jué)得有所不足 在使用VB 的MsComm ocx時(shí) 很多人都會(huì)在的開(kāi)發(fā)中覺(jué)得很困擾 這里講述的串口通訊類(lèi)使用native代碼 并且它是通API調(diào)用實(shí)現(xiàn)的 你會(huì)發(fā)現(xiàn)的串口通訊就是這么簡(jiǎn)單 在說(shuō)明如何使用這個(gè)類(lèi)前 需要說(shuō)明的是 本類(lèi)只是一個(gè)的串口通訊演示 你可能需要根據(jù)你的情況修改后使用 另外 本類(lèi)的目的是通過(guò)例子教會(huì)你在無(wú)需ocx控件和第三方組件的支持下用開(kāi)發(fā)串口通訊程序 所有并沒(méi)有完善的異常錯(cuò)誤處理 1 初始化并打開(kāi)串口創(chuàng)建一個(gè)CRs 類(lèi)的實(shí)例 并在調(diào)用Open方法前設(shè)置好串口通訊參數(shù) 例 Dim moRS as New Rs ()With moRs Port = // Uses BaudRate =   // 波特率 DataBit =   //  data bits StopBit = Rs DataStopBit StopBit_ // 停止位 Parity = Rs DataParity Parity_None // 無(wú)奇偶校驗(yàn) Timeout =   //超時(shí)時(shí)間 msEnd With // 初始化并打開(kāi)串口moRS Open () // 串口打開(kāi)后 你可以隨意地控制DTR/RTSmoRS Dtr = TruemoRS Rts = True為了處理異常情況 建議你使用Try Catch 2 發(fā)送數(shù)據(jù)本類(lèi)為Rx和Tx準(zhǔn)備了兩個(gè)緩沖區(qū)(buffer) 發(fā)送數(shù)據(jù)時(shí) 只需要設(shè)置TxData屬性為你需要發(fā)送的數(shù)據(jù) 然后調(diào)用Tx方法就可以了 例如 moRS TxData = txtTx TextmoRS Tx() 接收數(shù)據(jù)先調(diào)用Rx方法(參數(shù)為您需要從串口讀取的字節(jié)數(shù)) 然后讀取RxData屬性 例如 moRS Rx( )  // 從串口通訊緩沖區(qū)里讀取 字節(jié)Dim sRead as String=moRs RxData需要注意的是 當(dāng)本類(lèi)無(wú)法用串口讀取到所需的字節(jié)數(shù) 程序線程在超時(shí)異常發(fā)生前是鎖死的 超時(shí)時(shí)間通過(guò)Timout屬性設(shè)置 如果你沒(méi)有指明需要讀取的字節(jié)數(shù) 本類(lèi)默認(rèn)使用 字節(jié)來(lái)讀取緩沖區(qū)里的數(shù)據(jù) 版本信息 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 finalRev ( )Added Async supportRev ( )Minor bugs fixedRev a ( / / )Fixed BuildCommmDCB problemRev ( / / )Fixed problem with ASCII Encoding truncating th bitRev ( / / )Added IDisposable / Finalize implementationRev ( / / )Fixed problem on DCB fields InitializationRev ( / / )Added XON/XOFF supportRev ( / / )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 modeRemoved unused Async methods and other stuff Rev ( / / )Fixed incorrect character handling when using EnableEvents()Rev ( / / )Fixed some bugs reported by usersRev ( / / )Removed AutoReset of internal buffers and added PurgeBuffer() methodRev ( / / )Update internal stuff now using Win Exception instead of GetLastError+FormatMessage APIsRev ( / / )Added IsPortAvailable() function (thanks to Tom Lafleur for the hint)Revised some API declarationFixed 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 threadRev ( / / )Fixed EscapeCommFunction declarationFixed incorrect Pariti enum entryRev ( / / )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

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括珠山網(wǎng)站建設(shè)、珠山網(wǎng)站制作、珠山網(wǎng)頁(yè)制作以及珠山網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,珠山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到珠山省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

VB.NET串口通訊怎么發(fā)送和接收十六進(jìn)制?

串口通信最終都是用二進(jìn)制傳輸?shù)?,你用代碼把二進(jìn)制轉(zhuǎn)化成十六進(jìn)制就可以了。不需要額外設(shè)置。

VB與VB.NET串口及以太網(wǎng)通迅

關(guān)于串口通訊的問(wèn)題:

很明顯,你還不知道(不會(huì)、不習(xí)慣)使用事件驅(qū)動(dòng)的方式接收數(shù)據(jù)。

建議你仔細(xì)看看串口組件(無(wú)論VB6還是VB.net)的OnComm事件,你的問(wèn)題很容易解決。

關(guān)于以太網(wǎng)通訊:

在TCP通訊中端口確實(shí)可以重用,你百度一下“TCP端口重用”能查到很多示例。

但絕大多數(shù)情況下不推薦端口重用,而應(yīng)該采取服務(wù)器端建立連接池的方法。

或者,干脆不用TCP,用UDP解決也可以。

文章名稱(chēng):vb.net串口通訊 VB串口通訊
URL網(wǎng)址:http://muchs.cn/article28/doccccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、企業(yè)建站、關(guān)鍵詞優(yōu)化網(wǎng)站排名、搜索引擎優(yōu)化、網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)