C# 串口总结
来源:互联网 发布:网络教育专科报名 编辑:程序博客网 时间:2024/06/06 19:26
一、串口初始化
定义:
初始化:
二、串口接收事件
三、串口发送
对应单片机里的串口中断
存留备份
四、一些参考目录:
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
定义:
- 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数组读取
- myPort.Read(receiveData, 0, receiveData.Length);//读取数据
- //myPort.DiscardInBuffer();
- for (int i = 0; i < receiveData.Length; i++)
- {
- check[i] = receiveData[i]; //简单的定义了一个byte check[1000]接收数据
- Console.Write(check[i]);
- }
- string strRcv = null;
- for (int i = 0; i < receiveData.Length; i++)
- {
- strRcv += receiveData[i].ToString("X2");//十六进制显示
- }
- /*使用委托,如果需要修改控件*/
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message, "出错提示");
- }
- }
- }
三、串口发送
对应单片机里的串口中断
- cmd = "55";
- bytCmd[0] = Convert.ToByte(cmd.Substring(0, 2), 16);
- myPort.Write(bytCmd, 0, 1);
存留备份
- //port发送
- public void _port_DataSend(string strCommand)
- {
- //处理数字转换
- string sendBuf = strCommand;
- string sendnoNull = sendBuf.Trim();
- string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号
- string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
- string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
- strSendNoComma2.Replace("0X", ""); //去掉0X
- //用' '分开成多个字符串,用strArray装
- string[] strArray = strSendNoComma2.Split(' ');
- int byteBufferLength = strArray.Length;//获取strArray个数
- for (int i = 0; i < strArray.Length; i++)//排除空格数字
- {
- if (strArray[i] == "")
- {
- byteBufferLength--;
- }
- }
- // int temp = 0;
- byte[] byteBuffer = new byte[byteBufferLength];
- int ii = 0;
- for (int i = 0; i < strArray.Length; i++) //对获取的字符做相加运算
- {
- int decNum = 0;
- decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18
- try //防止输错,使其只能输入一个字节的字符
- {
- byteBuffer[ii] = Convert.ToByte(decNum);
- }
- catch (System.Exception ex)
- {
- MessageBox.Show("字节越界,请逐个字节输入!", "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
0
上一篇:光衰代码整理学习
下一篇:STM32的结构和启动模式
相关热门文章
- C# + Socket断线重连
- C#解决EventHandler参数无法传...
- 解决因为sql server 存储过程...
- 水晶报表(使用VS2010配合水晶...
- .Net/C# 开发WinFrom布局详解...
给主人留下些什么吧!~~
评论热议
阅读全文
0 0
- C#串口通信总结
- C#串口通信总结
- C#串口通信总结
- C#串口通信总结
- C#串口通信总结
- C# 串口总结
- C#串口
- c# 串口
- C#串口
- C# 串口
- c# 串口
- C#串口
- C# 串口
- C#初入串口通信(串行通信)总结
- 串口总结
- C#开发串口总结,并提炼串口辅助类到公用类库中
- C#开发串口总结,并提炼串口辅助类到公用类库中
- C#开发串口总结,并提炼串口辅助类到公用类库中
- nucleus 学习 中断
- 解决html表格字体
- nucleus plus学习总结(后续)
- 光衰代码整理学习
- 5-32 哥尼斯堡的“七桥问题”(并查集+度的判断)
- C# 串口总结
- STM32的结构和启动模式
- GIS二次开发牛人博客
- 电源控制和备份区域
- 复位和时钟控制(RCC)
- PCB一些设置记录
- RCC初始化学习
- 通过Ajax方式上传文件
- 按键中断总结
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
rh阴性血是什么血型
rh血型阴性是什么意思
rh血型是什么
rh阴性阳性
新生儿rh溶血症
献血rh阴性
rh阴性血怀孕
rh血型阳性十
孕妇rh阴性血
o型rh+
rh呈阳性是什么意思
人参皂苷rh 2
rh阴性是什么血型
rh阴性a型血
什么是rh血型
a型rh阴性
a型rh+
rh+血型
什么是rh阴性血
a型血rh阴性
o型血rh阴性血
rh抗体阳性是什么意思
o型血rh+
人参皂苷rh2正规药店多少钱卖
人参皂苷rh2哪个牌子好
孕妇rh血型阳性是什么意思
rh d 血型阳性是什么意思
rh血型抗d阳性是什么意思
新生儿rh血型不合溶血病
b型血rh血型阳性是什么意思
rk
rk键盘
www.6677rk
rk是什么牌子
w0rk是什么意思
www.78rk
美的电磁炉rk2106
rk机械键盘
rk机械键盘怎么样
rk键盘怎么样
rl