C# 串口总结
一、串口初始化
定义:
- 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
C# 串口总结的更多相关文章
- .NET 串口通信
这段时间做了一个和硬件设备通信的小项目,涉及到扫描头.输送线.称重机.贴标机等硬件.和各设备之间通信使用的是串口或网络(Socket)的方式.扫描头和贴标机使用的网络通信,输送线和称重机使用的是串口通 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 0.前言
目 录 前言 前言 刚参加工作,使用过VB.VC开发软件,随着C#的崛起,听说是C++++,公司决定以后开发软件使用C#,凭借在 ...
- 《连载 | 物联网框架ServerSuperIO教程》-4.如开发一套设备驱动,同时支持串口和网络通讯。附:将来支持Windows 10 IOT
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 12.二次开发及应用
目 录 第十二章 二次开发及应用... 2 12.1 项目配制... 3 12.2 引用相关组件... 4 12.3 构建主程序... 5 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 8.总体控制器的设计
目 录 第八章 总体控制器的设计... 2 8.1 总控制器的职能... 2 8.2 组装和释放部件... 3 8.3 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 6.通讯控制器的设计
目 录 第六章 通讯控制器的设计... 2 6.1 控制器接口... 2 6.2 串口控制器... 3 6.3 ...
- Linux下的串口编程及非阻塞模式
本篇介绍了如何在linux系统下向串口发送数据.包括read的阻塞和非阻塞.以及select方法. 打开串口 在Linux系统下,打开串口是通过使用标准的文件打开函数操作的. #include < ...
- 使用Minicom基于串口调试HiKey
虽然通过adb shell调试方便,但是有些时候不得不借助于串口进行调试,比如测试suspend to ram之类的功能时,adb服务被关闭. 同时在minicom中也可以进入shell,进行操作. ...
- UP Board 串口使用心得
前言 原创文章,转载引用务必注明链接. 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的博客: http://www.cnblogs.com/sjqlwy/p/up_s ...
- BluetoothChat用于蓝牙串口通信的修改方法
本人最近在研究嵌入式的串口通信,任务是要写一个手机端的遥控器用来遥控双轮平衡小车.界面只用了一个小时就写好了,重要的问题是如何与板子所带的SPP-CA蓝牙模块进行通信. SPP-CA模块自带代码,在这 ...
随机推荐
- SQL索引优化方法
SQL索引优化方法 以下是代码片段: ROW_NUMBER() OVER(ORDER BY ResumeCreateTime DESC) as [RowID] ,[TopDegree] ,[Degre ...
- 【Linux】grub引导修复
将系统盘挂载上并设置开机从光盘启动(U盘也可以) 进入系统安装引导初始界面,然后选择最后一项Troubleshooting 然后选择第二项Rescue a CentOS system进入系统救援模式选 ...
- ARC093 F Dark Horse——容斥
题目:https://atcoder.jp/contests/arc093/tasks/arc093_d #include<cstdio> #include<cstring> ...
- nRF51822 之 Interrupt
nRF51822的中断使用在官方的例程中好像没有!
- 【Web API]无法添加AttributeRoutes的解决方案
1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttribut ...
- HEAD请求
平时用的最多的无外乎POST GET 很少用的HEAD 这次HEAD请求使用场景:判断资源是否存在
- [python面试题] 什么是单例,单例有什么用,业务场景是什么
单例概念: 单例是一个特殊的类,这个类只能创建一次实例,例子如下: 1.a = Std(name='leo'), b = Std(name='jack'),两者的指向都是name=‘leo’的对象: ...
- CET-6 分频周计划生词筛选(Week 2)
点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...
- Python Challenge 关卡目录及解答过程
第0关:http://www.pythonchallenge.com/pc/def/0.html 线索:试着改变URL的地址-->把图片中得到的数字输入到URL中 2**38 输出: 第1关:h ...
- vue-router(路由)详细教程
vue-router(路由)详细教程:https://blog.csdn.net/wulala_hei/article/details/80488727 vue路由组件传参-页面通信:https:// ...