




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C#串口通信總結(jié)作者:不詳 出處:網(wǎng)絡(luò)轉(zhuǎn)載 2011/10/10 16:01:52 閱讀 0 次 我們知道對(duì)于標(biāo)準(zhǔn)DLL,可以采用DllImport進(jìn)行調(diào)用。例如:DllImport(KMY350X.dll)private static extern int OpenPort(int PortNum, int BaudRate); 甘特圖Xgantt(全球首款甘特圖32年歷史) Visual WebGui(完美的用戶界面解決方案) Stimulsoft Reports.Net(報(bào)表生成控件) DXperience Uni Premium 白金版(慧都獨(dú)家) 如果一些廠家比較懶的話,沒有提供相應(yīng)的dll,我們只能對(duì)它進(jìn)行串口通信編程了。以前從沒接觸過串口編程,最近在一個(gè)項(xiàng)目中有幾個(gè)地方都需要采用串口通信,跟公司一個(gè)老手請(qǐng)教后,感覺學(xué)到了很多東西,特在此做個(gè)總結(jié): 一:首先我們來認(rèn)識(shí)下什么是串口: 右鍵 我的電腦-管理-設(shè)備管理器-端口,選擇一個(gè)端口,點(diǎn)擊屬性。 我們可以看到該串口的屬性,在C#中我們使用SerialPort類來表示串口 ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/數(shù)據(jù)位comm.serialPort.DataBits = 8;/兩個(gè)停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/無奇偶校驗(yàn)位comm.serialPort.Parity = System.IO.Ports.Parity.None;comm.serialPort.ReadTimeout = 100;comm.serialPort.WriteTimeout = -1; 二:串口調(diào)試工具: 在對(duì)串口進(jìn)行編程時(shí)候,我們要向串口發(fā)送指令,然后我們解析串口返回的指令。在這里向大家推薦一款工具。 串口調(diào)試助手.exe 將要發(fā)送的指令用空格隔開,選擇HEX顯示為放回的字符串。 三:正式編程: 編寫Comm類: public class Commpublic delegate void EventHandle(byte readBuffer);public event EventHandle DataReceived; public SerialPort serialPort;Thread thread;volatile bool _keepReading; public Comm()serialPort = new SerialPort();thread = null;_keepReading = false; public bool IsOpengetreturn serialPort.IsOpen; private void StartReading()if (!_keepReading)_keepReading = true;thread = new Thread(new ThreadStart(ReadPort);thread.Start(); private void StopReading()if (_keepReading)_keepReading = false;thread.Join();thread = null; private void ReadPort()while (_keepReading)if (serialPort.IsOpen)int count = serialPort.BytesToRead;if (count 0)byte readBuffer = new bytecount;tryApplication.DoEvents();serialPort.Read(readBuffer, 0, count);if(DataReceived != null)DataReceived(readBuffer);Thread.Sleep(100);catch (TimeoutException) public void Open()Close();serialPort.Open();if (serialPort.IsOpen)StartReading();elseMessageBox.Show(串口打開失敗!); public void Close()StopReading();serialPort.Close(); public void WritePort(byte send, int offSet, int count)if (IsOpen)serialPort.Write(send, offSet, count); 注冊(cè)串口: Comm comm = new Comm();ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/數(shù)據(jù)位comm.serialPort.DataBits = 8;/兩個(gè)停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/無奇偶校驗(yàn)位comm.serialPort.Parity = System.IO.Ports.Parity.None;comm.serialPort.ReadTimeout = 100;comm.serialPort.WriteTimeout = -1;comm.Open();if (comm.IsOpen)comm.DataReceived += new Comm.EventHandle(comm_DataReceived); 發(fā)送指令: / / 發(fā)卡到機(jī)口/ private void SendCardToOut()is_read_card = false;sendCardToOut = true;byte send = 0x02, 0x46, 0x43, 0x34, 0x03, 0x30 ;if (comm.IsOpen)comm.WritePort(send, 0, send.Length); 收到指令,并解析: void comm_DataReceived(byte readBuffer1)/log.Info(HexCon.ByteToString(readBuffer);if (readBuffer1.Length = 1)receive = HealCardClass.ByteToString(readBuffer1);string str = 06;if (string.Equals(receive.Trim(), str, StringComparison.CurrentCultureIgnoreCase)tryif (is_read_card)byte send = new byte1;send0 = 0x05;comm.WritePort(send, 0, send.Length);Thread.Sleep(500);comm.DataReceived -= new Comm.EventHandle(comm_DataReceived);InitReadComm();if (sendCardToOut)byte send = new byte1;send0 = 0x05;comm.WritePort(send, 0, send.Length); readComm.DataReceived -= new Comm.EventHandle(readComm_DataReceived);readComm.Close(); log.Info(發(fā)卡完成!);lblMsg.Text = 發(fā)卡成
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 基于平衡計(jì)分卡的華辰集團(tuán)零售部績(jī)效考核方案優(yōu)化研究
- 地鐵上蓋混凝土樓板結(jié)構(gòu)地鐵振動(dòng)響應(yīng)特性和傳播規(guī)律研究
- 學(xué)生外出教育主題活動(dòng)方案
- 小學(xué)生安全教育知識(shí)
- 產(chǎn)后媽媽健康管理
- 2025年北京市中考招生考試數(shù)學(xué)真題試卷(真題+答案)
- 預(yù)防火災(zāi)小學(xué)生課件
- 預(yù)防學(xué)生欺凌班會(huì)課件
- 預(yù)防兒童殘疾課件
- 生理衛(wèi)生健康課件
- 護(hù)理核心制度考試試卷(附答案)
- 尾礦工安全培訓(xùn)
- 西安高新區(qū)管委會(huì)招聘筆試真題2024
- 2025年中國工商銀行招聘筆試備考題庫(帶答案詳解)
- 研發(fā)項(xiàng)目工時(shí)管理制度
- 浮選藥劑安全管理制度
- 會(huì)陰水腫硫酸鎂濕敷專題報(bào)告
- 技術(shù)異化的解放路徑-洞察及研究
- 2025年連云港市中考語文試卷真題(含標(biāo)準(zhǔn)答案)
- 2025年學(xué)校校長(zhǎng)公開選拔筆試試題及參考答案校長(zhǎng)招聘考試筆試真題
- T/CGMA 033002-2020壓縮空氣站節(jié)能設(shè)計(jì)指南
評(píng)論
0/150
提交評(píng)論