小言_互联网的博客

联为C#6——串口通信、动态链接库dll

319人阅读  评论(0)

串口通信

串口通讯只支持一个字节一个字节传输,及byte传输,不支持汉字进行传输,一个汉字有两个字节,所以不能进行传输。com口是点对点传输。8bit表示一个字节,两个16进制数占一个字节,一个字节占两位

串口参数设置




打开串口


接收数据


用委托进行显示



按16进制显示(将字节一个一个从 ASCII 转换为16进制)

发送数据


将字符串转换成16进制

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 serialPortTest
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private delegate void showTextRecv(string strRecv);
        private showTextRecv showMyText;

        private void Form1_Load(object sender, EventArgs e)
        {
   
            try
            {
   
                string[] portlist = System.IO.Ports.SerialPort.GetPortNames();   //获取电脑上的所有端口号
                for (int i = 0; i < portlist.Length; i++)
                {
   
                    this.comboBox1.Items.Add(portlist[i]);        //循环添加端口号到combox中
                }

            }
            catch (Exception)
            {
   

                throw;
            }
           
            showMyText = new showTextRecv(runShowText);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
   
            this.serialPort1.PortName = this.comboBox1.Text;    //将更改的com口随时赋值到serialport中
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
   
            if (this.comboBox3.Text == "无校验")
            {
   
                this.serialPort1.Parity = Parity.None;
            }
            else if(this.comboBox3.Text == "奇校验")
            {
   
                this.serialPort1.Parity = Parity.Odd;
            }
            else if (this.comboBox3.Text == "偶校验")
            {
   
                this.serialPort1.Parity = Parity.Even;
            }
            else if (this.comboBox3.Text == "0校验")
            {
   
                this.serialPort1.Parity = Parity.Space;
            }
            else if (this.comboBox3.Text == "1校验")
            {
   
                this.serialPort1.Parity = Parity.Mark;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
   
            this.serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
        }
        bool m_bOpen = false;
        private void button1_Click(object sender, EventArgs e)
        {
   
            m_bOpen = !m_bOpen;
            if (m_bOpen)
            {
   
                try
                {
   
                    this.serialPort1.Open();
                }
                catch (Exception em)
                {
   

                    MessageBox.Show(em.ToString());
                    m_bOpen = false;
                    return;
                }
                this.button1.Text = "关闭串口";
            }
            else
            {
   
                this.button1.Text = "打开串口";
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
   
            byte[] recvData = new byte[this.serialPort1.BytesToRead];   //以读取到的数据长度开辟一个接收空间
            this.serialPort1.Read(recvData, 0, recvData.Length);   //从第0位开始读到最后一位

        }
        private void runShowText(string strRecv)
        {
   
            this.textBox1.AppendText(strRecv + "\r\n");
        }

        #region  接收数据
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
   
            byte[] recvData = null;
            if (checkBox1.Checked)
            {
   
                StringBuilder strRecv = new StringBuilder();
                for (int i = 0; i < strRecv.Length; i++)
                {
   
                    strRecv.AppendFormat("{0:X2}" + " ", recvData[i]);     //将字节一个一个从ASCII转换为16进制

                }
                this.textBox1.Invoke(showMyText, strRecv.ToString());
            }
            else
            {
   
                string strRecv = System.Text.Encoding.Default.GetString(recvData);
                this.textBox1.Invoke(showMyText, strRecv);
            }
        }
        #endregion

        #region  发送数据
        private void button2_Click(object sender, EventArgs e)
        {
   
            byte[] sendData = null;
            if (checkBox2.Checked)
            {
   
                sendData = strToHexByte(this.textBox2.Text);
            }
            else
            {
   
                sendData = System.Text.Encoding.Default.GetBytes(this.textBox2.Text);
            }
            this.serialPort1.Write(sendData, 0, sendData.Length);
        }
        #endregion

        #region  将字符串转换为16进制
        private byte[] strToHexByte(string hexString)
        {
   
            hexString = hexString.Replace(" ", "");    //去掉空格
            if (hexString.Length % 2 != 0)
            {
   
                hexString += " ";
            }
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
   
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""));
            }
            return returnBytes;
        }
        #endregion
    }
}

动态链接库dll

目标框架要保持一致




转载:https://blog.csdn.net/qq_42832272/article/details/115920220
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场