




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
C#串口通信總結(jié)作者:不詳 出處:網(wǎng)絡轉(zhuǎn)載 2011/10/10 16:01:52 閱讀 0 次 我們知道對于標準DLL,可以采用DllImport進行調(diào)用。例如:DllImport(KMY350X.dll)private static extern int OpenPort(int PortNum, int BaudRate); 甘特圖Xgantt(全球首款甘特圖32年歷史) Visual WebGui(完美的用戶界面解決方案) Stimulsoft Reports.Net(報表生成控件) DXperience Uni Premium 白金版(慧都獨家) 如果一些廠家比較懶的話,沒有提供相應的dll,我們只能對它進行串口通信編程了。以前從沒接觸過串口編程,最近在一個項目中有幾個地方都需要采用串口通信,跟公司一個老手請教后,感覺學到了很多東西,特在此做個總結(jié): 一:首先我們來認識下什么是串口: 右鍵 我的電腦-管理-設備管理器-端口,選擇一個端口,點擊屬性。 我們可以看到該串口的屬性,在C#中我們使用SerialPort類來表示串口 ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/數(shù)據(jù)位comm.serialPort.DataBits = 8;/兩個停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/無奇偶校驗位comm.serialPort.Parity = System.IO.Ports.Parity.None;comm.serialPort.ReadTimeout = 100;comm.serialPort.WriteTimeout = -1; 二:串口調(diào)試工具: 在對串口進行編程時候,我們要向串口發(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); 注冊串口: Comm comm = new Comm();ConfigClass config = new ConfigClass();comm.serialPort.PortName = config.ReadConfig(SendHealCard);/波特率comm.serialPort.BaudRate = 9600;/數(shù)據(jù)位comm.serialPort.DataBits = 8;/兩個停止位comm.serialPort.StopBits = System.IO.Ports.StopBits.One;/無奇偶校驗位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ā)卡到機口/ 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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 場地擴建打樁方案(3篇)
- DB23-T3028-2021-鮮食甜玉米機械化栽培技術規(guī)程-黑龍江省
- DB23-T2928-2021-基于水生態(tài)功能分區(qū)的流域水環(huán)境監(jiān)測方法指南-黑龍江省
- DB23-T2839-2021-政務信息資源目錄第1部分:編碼規(guī)則-黑龍江省
- 農(nóng)業(yè)基地現(xiàn)場管理制度
- 公司接待廚房管理制度
- 崗位定員設置管理制度
- 工程現(xiàn)場質(zhì)量管理制度
- 文明施工措施方案(3篇)
- 建筑圍擋招標方案(3篇)
- 2025年繼續(xù)教育公需科目網(wǎng)絡考試試題及答案
- 2025高考終極押題范文6篇與題目
- DB32/T 4220-2022消防設施物聯(lián)網(wǎng)系統(tǒng)技術規(guī)范
- 車位轉(zhuǎn)讓合同協(xié)議書
- 合伙經(jīng)營貨車輛協(xié)議書
- 2025年農(nóng)村個人果園承包合同
- 上海上海市普陀區(qū)融媒體中心專業(yè)技術人員招聘筆試歷年參考題庫附帶答案詳解
- 湖北省武漢市2025屆高三年級五月模擬訓練試題數(shù)學試題及答案(武漢五調(diào))
- 企業(yè)管理流程數(shù)字化轉(zhuǎn)型計劃
- 2025年湖北省襄陽市襄州區(qū)中考數(shù)學二模試卷
- 2024年浙江省單獨考試招生文化考試語文試卷真題(含答案詳解)
評論
0/150
提交評論