C#串口通信總結(jié).doc_第1頁
C#串口通信總結(jié).doc_第2頁
C#串口通信總結(jié).doc_第3頁
C#串口通信總結(jié).doc_第4頁
C#串口通信總結(jié).doc_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

C#串口通信總結(jié)作者:不詳 出處:網(wǎng)絡(luò)轉(zhuǎn)載 2011/10/10 16:01:52 閱讀 0 次 我們知道對于標(biāo)準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 白金版(慧都獨家) 如果一些廠家比較懶的話,沒有提供相應(yīng)的dll,我們只能對它進行串口通信編程了。以前從沒接觸過串口編程,最近在一個項目中有幾個地方都需要采用串口通信,跟公司一個老手請教后,感覺學(xué)到了很多東西,特在此做個總結(jié): 一:首先我們來認識下什么是串口: 右鍵 我的電腦-管理-設(shè)備管理器-端口,選擇一個端口,點擊屬性。 我們可以看到該串口的屬性,在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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論