• 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

    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    【转】关于维生素的那些事
    【转】MaBatis学习---源码分析MyBatis缓存原理
    【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
    【转】Java学习---垃圾回收算法与 JVM 垃圾回收器综述
    Qt 中的对象模型(Object Model)
    The Property System
    Qt--core模块概述
    QtCore概述
    在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
    同一个进程的多个线程堆栈共享状况
  • 原文地址:https://www.cnblogs.com/ch122633/p/7363277.html
Copyright © 2020-2023  润新知