可以將Form1里的串口公開,然后在Form2里添加對(duì)DataReceived事件的處理
創(chuàng)新互聯(lián)專注于雨湖網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供雨湖營(yíng)銷型網(wǎng)站建設(shè),雨湖網(wǎng)站制作、雨湖網(wǎng)頁(yè)設(shè)計(jì)、雨湖網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造雨湖網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供雨湖網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
Form1.Designer.cs中將
private System.IO.Ports.SerialPort port;改為
publice System.IO.Ports.SerialPort port;
然后在Form2里加上
Form1.port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
最后在
void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
在這里進(jìn)行處理就可以了;
}
這個(gè)是C#的,VB的你自己轉(zhuǎn)一下
的串口通訊支持總是讓人覺得有所不足 在使用VB 的MsComm ocx時(shí) 很多人都會(huì)在的開發(fā)中覺得很困擾 這里講述的串口通訊類使用native代碼 并且它是通API調(diào)用實(shí)現(xiàn)的 你會(huì)發(fā)現(xiàn)的串口通訊就是這么簡(jiǎn)單 在說明如何使用這個(gè)類前 需要說明的是 本類只是一個(gè)的串口通訊演示 你可能需要根據(jù)你的情況修改后使用 另外 本類的目的是通過例子教會(huì)你在無(wú)需ocx控件和第三方組件的支持下用開發(fā)串口通訊程序 所有并沒有完善的異常錯(cuò)誤處理 1 初始化并打開串口創(chuàng)建一個(gè)CRs 類的實(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 // 初始化并打開串口moRS Open () // 串口打開后 你可以隨意地控制DTR/RTSmoRS Dtr = TruemoRS Rts = True為了處理異常情況 建議你使用Try Catch 2 發(fā)送數(shù)據(jù)本類為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)本類無(wú)法用串口讀取到所需的字節(jié)數(shù) 程序線程在超時(shí)異常發(fā)生前是鎖死的 超時(shí)時(shí)間通過Timout屬性設(shè)置 如果你沒有指明需要讀取的字節(jié)數(shù) 本類默認(rèn)使用 字節(jié)來讀取緩沖區(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
直接發(fā)數(shù)字1。
一般可以直接發(fā)10進(jìn)制的數(shù)字,當(dāng)然要轉(zhuǎn)換,比如16進(jìn)制的21,你可以直接發(fā)33
是獨(dú)立的東西,沒有依賴,盡管在底層有可能是類似的調(diào)用
還有在.net里一般只認(rèn)為是普通類,并不屬于控件,因?yàn)闆]界面