C#开启关闭串口,发送数据

来源:互联网 发布:英文文献阅读软件 编辑:程序博客网 时间:2024/06/10 08:03
using System.IO.Ports;private SerialPort LEDPt = new SerialPort();byte[] sendByte = new byte[sendLength];Array.Copy(by1, 0, sendByte, 0, count1);Array.Copy(msg, 0, sendByte, count1, msg.Length);Array.Copy(by2, 0, sendByte, count1 + msg.Length, count2);//封装好数据,复制到数组sendByte中if (!LEDPt.IsOpen) //若串口没有打开{   try  {     LEDPt.PortName = SysConfig.comNum; //设置端口号     LEDPt.BaudRate = int.Parse(SysConfig.btl); //设置波特率     LEDPt.Open(); //打开串口     if (LEDPt.IsOpen)     {        LEDPt.Write(sendByte, 0, sendByte.Length); //将封装好的数据通过串口发送出去     }   }  catch  {      ......  }}else{  LEDPt.Close();}