C# 串口总结

一、串口初始化
定义:

  1. using System.IO.Ports;
  2. SerialPort myPort = new SerialPort()

初始化:

  1.         //port初始化
  2.         public void _port_Init(string comName)
  3.         {
  4.             myPort.PortName = comName;
  5.             myPort.BaudRate = 9600;
  6.             myPort.DataBits = 8;
  7.             myPort.Parity = Parity.None;
  8.             myPort.ReadTimeout = 1000;
  9.             myPort.Open();
  10.             myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);
  11.         }


二、串口接收事件

  1.         //接收事件
  2.         void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  3.         {
  4.             if (myPort.IsOpen)
  5.             {
  6.                 try
  7.                 {
  8.                     byte[] receiveData = new byte[myPort.BytesToRead];//用receiveData数组读取
  9.                     myPort.Read(receiveData, 0, receiveData.Length);//读取数据
  10.                     //myPort.DiscardInBuffer();
  11.                     for (int i = 0; i < receiveData.Length; i++)
  12.                     {
  13.                         check[i] = receiveData[i];                             //简单的定义了一个byte check[1000]接收数据
  14.                         Console.Write(check[i]);
  15.                     }
  16.                     string strRcv = null;
  17.                     for (int i = 0; i < receiveData.Length; i++)
  18.                     {
  19.                         strRcv += receiveData[i].ToString("X2");//十六进制显示
  20.                     }
  21.                     /*使用委托,如果需要修改控件*/
  22.                 }
  23.                 catch (System.Exception ex)
  24.                 {
  25.                     MessageBox.Show(ex.Message, "出错提示");
  26.                 }
  27.             }
  28.         }


三、串口发
对应单片机里的串口中断

  1.             cmd = "55";
  2.             bytCmd[0] = Convert.ToByte(cmd.Substring(0, 2), 16);
  3.             myPort.Write(bytCmd, 0, 1);


存留备份

  1. //port发送
  2.         public void _port_DataSend(string strCommand)
  3.         {
  4.             //处理数字转换
  5.             string sendBuf = strCommand;
  6.             string sendnoNull = sendBuf.Trim();
  7.             string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号
  8.             string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
  9.             string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
  10.             strSendNoComma2.Replace("0X", ""); //去掉0X
  11.             //用' '分开成多个字符串,用strArray装
  12.             string[] strArray = strSendNoComma2.Split(' ');
  13.             int byteBufferLength = strArray.Length;//获取strArray个数
  14.             for (int i = 0; i < strArray.Length; i++)//排除空格数字
  15.             {
  16.                 if (strArray[i] == "")
  17.                 {
  18.                     byteBufferLength--;
  19.                 }
  20.             }
  21.             // int temp = 0;
  22.             byte[] byteBuffer = new byte[byteBufferLength];
  23.             int ii = 0;
  24.             for (int i = 0; i < strArray.Length; i++) //对获取的字符做相加运算
  25.             {
  26.                 int decNum = 0;
  27.                 decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18
  28.                 try //防止输错,使其只能输入一个字节的字符
  29.                 {
  30.                     byteBuffer[ii] = Convert.ToByte(decNum);
  31.                 }
  32.                 catch (System.Exception ex)
  33.                 {
  34.                     MessageBox.Show("字节越界,请逐个字节输入!", "Error");
  35.                     //tmSend.Enabled = false;
  36.                     return;
  37.                 }
  38.                 ii++;
  39.             }
  40.             myPort.Write(byteBuffer, 0, byteBuffer.Length);
  41.         }


四、一些参考目录
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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C#开发串口,全部注释了;串行接口(串口)是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件。串口通信(Serial Communications)的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通信的端口,这些参数必须匹配。 1. 波特率:这是一个衡量符号传输速率的参数。指的是信号被调制以后在单位时间内的变化,即单位时间内载波参数变化的次数,如每秒钟传送960个字符,而每个字符格式包含10位(1个起始位,1个停止位,8个数据位),这时的波特率为960Bd,比特率为10位*960个/秒=9600bps。 2. 数据位:这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据往往不会是8位的,标准的值是6、7和8位。标准的ASCII码是0~127(7位),扩展的ASCII码是0~255(8位)。 3. 停止位:用于表示单个包的最后几位。典型的值为1,1.5和2位。由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。 4. 校验位:在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高和低。当然没有校验位也是可以的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值