c#串口

#官方文档地址链接:
官方技术文档链接
#一、引用C#类
using System.IO.Ports;
c#串口_第1张图片
#二、创建(new)一个串口对象

//创建一个串口号为“COM4”的对象

SerialPort mySerialPort=new SeriaPort("COM4")

#三、设置波特率,数据位,校验位,停止位

        //波特率 BaudRate 获取或设置串行波特率。
        mySerialPort.BaudRate = 38400;

        //奇偶校验位 无奇偶校验位
        mySerialPort.Parity = Parity.None;

        //停止位 一个停止位
        mySerialPort.StopBits = StopBits.One;

        //数据位 8位数据位
        mySerialPort.DataBits = 8;

        //设置串口协议
        mySerialPort.Handshake = Handshake.None;

        //是否启用串口发送 启用串口发送
        mySerialPort.RtsEnable = true;

        //开启串口
        mySerialPort.Open();

#四、串口发送

//串口发送“Hello word”
mySerialPort.Write(“Hello Word”);

//发送字节数据:
//创建一个字节对象 里面的值为(0x0x,0x02)
Byte[] bj = new byte[] {0x01,0x02};
//发送字节数据
//参数1:数据源
//参数2:从哪个索引开始发送(发送数据起始位)
//参数3:一共发送多少个数据(发送数据结束位)
 mySerialPort.Write(bj,0,bj.Length);

#五、串口接收
1、创建一个指定大小的字节对象,用来接收接收缓冲区的数据。

//创建一个字节大小为1024*1024大小字节的对象 A
byte[] A = new byte[1024 * 1024];

2、获取接收缓冲区大小(用来判断是否有数据)

//获取接收缓冲区大小
int jie_shou_chang_du = mySerialPort.BytesToRead;

3、判断接收缓冲区是否有数据,如果有就读取数据

if(jie_shou_chang_du >0)
{
   for (i = 0; i < jie_shou_chang_du; i++)
   {
      //将数据按字节依次读取到A中
      A[i] = (byte)mySerialPort.ReadByte();
   }
   //清空接收缓冲区
   //mySerialPort.DiscardInBuffer();
   
}

#五、关闭串口

//关闭串口
mySerialPort.Close();

#六、完整代码(只能参考,复制无用)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        int bz=0;
        byte[] A = new byte[1024 * 1024];

        //创建一个串口对象
        //string COM = "COM1";//串口号

        SerialPort mySerialPort = new SerialPort("COM4");

        //定义端口类
        private SerialPort ComDevice = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //C#串口参考链接
            //https://docs.microsoft.com/zh-cn/dotnet/api/system.io.ports.serialport.bytestoread?view=netframework-4.6.1


            int bo_te_lv = 38400;//波特率

            int shu_ju_wei = 8;//数据位

            //获取或设置串行波特率。


            //波特率 BaudRate 获取或设置串行波特率。
            mySerialPort.BaudRate = bo_te_lv;

            //奇偶校验位 无奇偶校验位
            mySerialPort.Parity = Parity.None;

            //停止位 一个停止位
            mySerialPort.StopBits = StopBits.One;

            //数据位 8位数据位
            mySerialPort.DataBits = shu_ju_wei;

            //设置串口协议
            mySerialPort.Handshake = Handshake.None;

            //是否启用串口发送 启用串口发送
            mySerialPort.RtsEnable = true;

            //开启串口
            mySerialPort.Open();
            this.label3.Text = "打开";
            bz = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            mySerialPort.Close();
            this.label3.Text = "串口关闭";
            bz = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int i;
            this.label8.Text = bz.ToString();
            if (bz == 1)
            {
                string s1 = "";
                //获取接收缓冲区长度
                int jie_shou_chang_du = mySerialPort.BytesToRead;
                //判断是否有串口数据
                if (jie_shou_chang_du > 0)
                {
                    for (i = 0; i < jie_shou_chang_du; i++)
                    {
                        A[i] = (byte)mySerialPort.ReadByte();
                        s1 = s1 + A[i].ToString();
                    }

                    if (A[0] == 0xFF && A[1] == 0xFD)
                    {
                        switch (A[2])
                        {
                            //光电传感器
                            case 0x00:
                                {
                                    break;
                                }
                            //温度传感器
                            case 0x01:
                                {
                                    //显示温度
                                    this.label10.Text = A[4] + "." + A[5];
                                    //判断温度 
                                    if (A[4].ToString() == this.textBox3.Text)
                                    {
                                        //发送数据
                                        Byte[] bj = new byte[] {0xFA, 0xFB, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
                                        mySerialPort.Write(bj,0,bj.Length);
                                    }
                                    else
                                    {
                                        //发送数据
                                        Byte[] bj = new byte[] { 0xFA, 0xFB, 0x06, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
                                        mySerialPort.Write(bj.ToString());
                                    }
                                    break;
                                }
                            //火焰传感器
                            case 0x03:
                                {
                                    break;
                                }
                            //空气质量
                            case 0x04:
                                {
                                    break;
                                }
                            //可燃气体
                            case 0x05:
                                {
                                    break;
                                }
                            //人体传感器
                            case 0x06:
                                {
                                    if (A[4] == 0x00)
                                    {
                                        this.textBox2.Text = "有人" + A[4];
                                        //发送数据
                                        Byte[] bj = new byte[] { 0xFA, 0xFB, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };

                                        mySerialPort.Write(bj,0,bj.Length);
                                    }
                                    else
                                    {
                                        //发送数据
                                        Byte[] bj = new byte[] { 0xFA, 0xFB, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                                        mySerialPort.Write(bj, 0, bj.Length);
                                    }
                                    break;
                                }
                        }
                    }
                    //this.textBox2.Text = s1;
                    //清空接收数据缓冲区
                    mySerialPort.DiscardInBuffer();
                }
            }
            else
            {
                this.textBox2.Text = "串口无数据";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string fa_shong = this.textBox1.Text;
            mySerialPort.Write(fa_shong);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i;
            byte[] A = new byte[1024 * 1024];

            //获取接收缓冲区长度
            int jie_shou_chang_du = mySerialPort.BytesToRead;

            //判断是否有串口数据
            if (jie_shou_chang_du > 0)
            {
                for (i = 0; i < jie_shou_chang_du; i++)
                {
                    A[i] = (byte)mySerialPort.ReadByte();
                }
                this.textBox2.Text = A[0].ToString();
                mySerialPort.DiscardInBuffer();
            }
            else
            {
                this.textBox2.Text = "串口无数据";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            mySerialPort.DiscardInBuffer();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            this.Hide();
            frm2.ShowDialog();
            Application.ExitThread();
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

        }
    }
}

你可能感兴趣的:(C#)