C# 实现串口发送数据(不用串口控件版)

参考:https://blog.csdn.net/mannix_lei/article/details/79979432

   https://www.cnblogs.com/ElijahZeng/p/7609241.html

1、关于怎么建立工程的,这里就不过多赘述了,先用控件编写个窗体程序如下

  

2、双击窗体跳转到程序页面,直接上代码,代码里有注释,看注释都可以看懂的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;   //声明线程命名空间

namespace TB528IL_APP
{
    public partial class Form1 : Form
    {
        #region 字段/属性/委托  
        /// <summary>  
        /// 串行端口对象  
        /// </summary>  
        private SerialPort sp;

        /// <summary>  
        /// 串口接收数据委托  
        /// </summary>  
        public delegate void ComReceiveDataHandler(string data);

        public ComReceiveDataHandler OnComReceiveDataHandler = null;

        /// <summary>  
        /// 端口名称数组  
        /// </summary>  
        public string[] PortNameArr { get; set; }

        /// <summary>  
        /// 串口通信开启状态  
        /// </summary>  
        public bool PortState { get; set; } = false;

        /// <summary>  
        /// 编码类型  
        /// </summary>  
        public Encoding EncodingType { get; set; } = Encoding.ASCII;
        #endregion



        public Form1()
        {
            InitializeComponent();
            My_COM();
            OpenPort("COM1");
        }

        #region 方法 

        /*********************************************
        函数名: My_COM()
        功  能: 新建串口实例  
        参  数: 无
        返回值: 无 
        ********************************************/
        public void My_COM()
        {
            PortNameArr = SerialPort.GetPortNames();
            sp = new SerialPort();
        }
        /*********************************************
        函数名:OpenPort(string portName, int boudRate = 115200, int dataBit = 8, int stopBit = 1, int timeout = 5000)
        功  能:打开端口,并且初始化默认端口波特率、数据位、停止位、超时时间等参数  
        参  数:<param name="portName">端口名称 
                <param name="boudRate">波特率 
                <param name="dataBit">数据位
                <param name="stopBit">停止位
                <param name="timeout">超时时间
        返回值:无 
        ********************************************/
        public void OpenPort(string portName, int boudRate = 115200, int dataBit = 8, int stopBit = 1, int timeout = 5000)
        {
            try
            {
                sp.PortName = portName;
                sp.BaudRate = boudRate;
                sp.DataBits = dataBit;
                sp.StopBits = (StopBits)stopBit;
                sp.ReadTimeout = timeout;
                sp.Open();
                PortState = true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /*********************************************
        函数名: ClosePort()
        功  能: 关闭端口  
        参  数: 无
        返回值: 无 
        ********************************************/
        public void ClosePort()
        {
            try
            {
                sp.Close();
                PortState = false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /*********************************************
        函数名: SendData(string sendData)
        功  能: 发送字符串数据 
        参  数: string sendData 串口要发送的字符串
        返回值: 无 
        ********************************************/
        public void SendData(string sendData)
        {
            try
            {
                sp.Encoding = EncodingType;
                sp.Write(sendData);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /*********************************************
        函数名: Send_Byte(string send_byte)
        功  能: 把要发送的字符串转换为16进制后发送,例如把字符串"9610"转换为0x25、0x8a发送出去
        参  数: string send_byte 串口要转换为16进制发送的字符串
        返回值: 无 
        ********************************************/
        public void Send_Byte(string send_byte)
        {
            byte[] data = new byte[4];
            int Time_Buff = 0;
            int.TryParse(send_byte, out Time_Buff); //将字符串转换为整形
            data[0] = 0x57;
            data[1] = 0xa1;
            data[2] = (byte)(Time_Buff >> 8);
            data[3] = (byte)Time_Buff;
            // 参数:
            //   buffer: 包含要写入端口的数据的字节数组。
            //   offset: buffer 参数中从零开始的字节偏移量,从此处开始将字节复制到端口。
            //   count : 要写入的字节数。
            sp.Write(data, 0, 4);
        }

        #endregion

        //发送Light_delay按钮
        private void button1_Click(object sender, EventArgs e)
        {
            //获取文本框的值
            string Light_delay = Light_delay_textBox.Text; //linght_delay_textBox为textBox控件的名称
            Send_Byte(Light_delay);
        }

        //发送Camera_delay按钮
        private void button2_Click(object sender, EventArgs e)
        {
            string Camera_delay = Camera_delay_textBox.Text;
            SendData(Camera_delay);
        }

        //发送Light_working_time按钮
        private void button3_Click(object sender, EventArgs e)
        {
            string Light_working_time = Light_working_time_textBox.Text;
            SendData(Light_working_time);
        }
    }
    
}

3、运行结果如下,效果理想

 

  

4、关于串口接收还没完全搞明白,迟点补回来

原文地址:https://www.cnblogs.com/xingboy/p/11052901.html