• 大小: 0.02M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-06-18
  • 语言: C#
  • 标签: 串口通信  通信  串口  C#  

资源简介

C#串口通信实例

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;//SerialPort 命名空间
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPort_bilibili
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Encoding = Encoding.GetEncoding(“GB2312“);   //串口编码引入GB2312编码(汉字编码)
            //防止跨线程操作空间异常
            Control.CheckForIllegalCrossThreadCalls = false;   //取消跨线程检查
        }

        //端口号扫描按钮
        private void button1_Click(object sender EventArgs e)
        {
            ReflashPortToComboBox(serialPort1 comboBox_port);
        }

        //自动扫描可用串口并添加到串口号列表上
        private void ReflashPortToComboBox(SerialPort serialPort ComboBox comboBox)
        {                                                               //将可用端口号添加到ComboBox
            if (!serialPort.IsOpen)//串口处于关闭状态
            {
                comboBox.Items.Clear();
                string[] str = SerialPort.GetPortNames();
                if (str == null)
                {
                    MessageBox.Show(“本机没有串口!“ “Error“);
                    return;
                }
                //添加串口
                foreach (string s in str)
                {
                    comboBox.Items.Add(s);
                    Console.WriteLine(s);
                }
            }
            else
            {
                MessageBox.Show(“串口处于打开状态不能刷新串口列表“ “Error“);
            }
        }

        //窗体加载函数
        private void Form1_Load(object sender EventArgs e)
        {
            ReflashPortToComboBox(serialPort1 comboBox_port);//第一次加载时候就预先扫描一次串口号
            button_close.Enabled = false;//关闭串口按钮按钮使能
        }

        //串口打开按钮
        private void button_open_Click(object sender EventArgs e)
        {
            Int32 iBaudRate = Convert.ToInt32(comboBox_BaudRate.SelectedItem.ToString());//获取波特率下拉框里选中的波特率数据从字符串转为int32

            serialPort1.PortName = comboBox_port.SelectedItem.ToString();//串口号
            serialPort1.BaudRate = iBaudRate;//波特率

            try
            {
                serialPort1.Open();
                button_open.Enabled = false;
                button_close.Enabled = true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(“串口打开失败“ + ex “error“);
            }
        }

        private void button_close_Click(object sender EventArgs e)
        {
            try
            {
                serialPort1.Close();
                button_open.Enabled = true;
                button_close.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(“串口关闭失败“ + ex “error“);
            }
        }

        private void button4_Click(object sender EventArgs e)
        {
            textBox_get.Clear();
        }

        private void butt

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-11-20 17:54  SerialPort-winform-bilibili_demo\
     文件        2518  2020-11-20 17:54  SerialPort-winform-bilibili_demo\.gitattributes
     文件        5745  2020-11-20 17:54  SerialPort-winform-bilibili_demo\.gitignore
     文件         184  2020-11-20 17:54  SerialPort-winform-bilibili_demo\App.config
     文件       13583  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Form1.Designer.cs
     文件        5323  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Form1.cs
     文件        5894  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Form1.resx
     文件         509  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Program.cs
     目录           0  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\
     文件        1294  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\AssemblyInfo.cs
     文件        2780  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\Resources.Designer.cs
     文件        5496  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\Resources.resx
     文件        1076  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\Settings.Designer.cs
     文件         242  2020-11-20 17:54  SerialPort-winform-bilibili_demo\Properties\Settings.settings
     文件         262  2020-11-20 17:54  SerialPort-winform-bilibili_demo\README.md
     文件        3544  2020-11-20 17:54  SerialPort-winform-bilibili_demo\SerialPort_bilibili.csproj
     文件        1118  2020-11-20 17:54  SerialPort-winform-bilibili_demo\SerialPort_bilibili.sln

评论

共有 条评论