Chinaunix首页 | 论坛 | 博客
  • 博客访问: 493698
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: C#/.net

2017-01-07 23:28:52

一、串口初始化
定义:
  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

阅读(801) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~