一、串口初始化
定義:
using System.IO.Ports;
SerialPort myPort = new SerialPort()
初始化:
? ? ? ??//port初始化
????????public void _port_Init(string comName)
????????{
????????????myPort.PortName = comName;
????????????myPort.BaudRate = 9600;
????????????myPort.DataBits = 8;
????????????myPort.Parity = Parity.None;
????????????myPort.ReadTimeout = 1000;
????????????myPort.Open();
????????????myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);
????????}
二、串口接收事件
? ? ? ??//接收事件
????????void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
????????{
????????????if (myPort.IsOpen)
????????????{
????????????????try
????????????????{
? ? ? ? ? ? ? ? ? ? byte[] receiveData = new byte[myPort.BytesToRead];//用receiveData數(shù)組讀取
? ? ? ? ? ? ? ? ? ? myPort.Read(receiveData, 0, receiveData.Length);//讀取數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? //myPort.DiscardInBuffer();
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < receiveData.Length; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? check[i] = receiveData[i]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? //簡(jiǎn)單的定義了一個(gè)byte check[1000]接收數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? Console.Write(check[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? string strRcv = null;
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < receiveData.Length; i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? strRcv += receiveData[i].ToString("X2");//十六進(jìn)制顯示
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? /*使用委托,如果需要修改控件*/
????????????????}
????????????????catch (System.Exception ex)
????????????????{
????????????????????MessageBox.Show(ex.Message, "出錯(cuò)提示");
????????????????}
????????????}
????????}
三、串口發(fā)送
對(duì)應(yīng)單片機(jī)里的串口中斷
? ? ? ? ? ??cmd = "55";
????????????bytCmd[0] = Convert.ToByte(cmd.Substring(0, 2), 16);
????????????myPort.Write(bytCmd, 0, 1);
存留備份
//port發(fā)送
????????public void _port_DataSend(string strCommand)
????????{
????????????//處理數(shù)字轉(zhuǎn)換
????????????string sendBuf = strCommand;
????????????string sendnoNull = sendBuf.Trim();
????????????string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗號(hào)
????????????string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗號(hào)
????????????string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
????????????strSendNoComma2.Replace("0X", ""); //去掉0X
????????????//用' '分開(kāi)成多個(gè)字符串,用strArray裝
????????????string[] strArray = strSendNoComma2.Split(' ');
????????????int byteBufferLength = strArray.Length;//獲取strArray個(gè)數(shù)
????????????for (int i = 0; i < strArray.Length; i++)//排除空格數(shù)字
????????????{
????????????????if (strArray[i] == "")
????????????????{
????????????????????byteBufferLength--;
????????????????}
????????????}
????????????// int temp = 0;
????????????byte[] byteBuffer = new byte[byteBufferLength];
????????????int ii = 0;
????????????for (int i = 0; i < strArray.Length; i++) //對(duì)獲取的字符做相加運(yùn)算
????????????{
????????????????int decNum = 0;
????????????????decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12時(shí),temp == 18
????????????????try //防止輸錯(cuò),使其只能輸入一個(gè)字節(jié)的字符
????????????????{
????????????????????byteBuffer[ii] = Convert.ToByte(decNum);
????????????????}
????????????????catch (System.Exception ex)
????????????????{
????????????????????MessageBox.Show("字節(jié)越界,請(qǐng)逐個(gè)字節(jié)輸入!", "Error");
????????????????????//tmSend.Enabled = false;
????????????????????return;
????????????????}
????????????????ii++;
????????????}
????????????myPort.Write(byteBuffer, 0, byteBuffer.Length);
????????}
四、一些參考目錄:
http://blog.csdn.net/lllljz/article/details/7603400
http://www.cnblogs.com/elaron/archive/2011/03/15/1985378.html
http://blog.csdn.net/geekwangminli/article/details/7851673
http://blog.csdn.net/cy757/article/details/4474930
http://www.cnblogs.com/screes/p/5633383.html
? ? ? ? ? ? ? ?
?
? ? ? ? ? ? ? ?閱讀(70) | 評(píng)論(0) | 轉(zhuǎn)發(fā)(0) | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0
上一篇:光衰代碼整理學(xué)習(xí)
下一篇:STM32的結(jié)構(gòu)和啟動(dòng)模式
相關(guān)熱門(mén)文章 ? ? ? ? ? ?C# + Socket斷線(xiàn)重連 ?C#解決EventHandler參數(shù)無(wú)法傳... ?解決因?yàn)閟ql server 存儲(chǔ)過(guò)程... ?水晶報(bào)表(使用VS2010配合水晶... ?.Net/C# 開(kāi)發(fā)WinFrom布局詳解... ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? 給主人留下些什么吧!~~
? ? ? ? ?評(píng)論熱議