C#中的WinFrom技术实现串口通讯助手(附源码)

实现的功能

1、实现自动加载可用串口。
2、打开串口,并且使用C#状态栏显示串口的状态。
3、实现了串口的接收数据和发送数据功能。
4、串口使用定时器进行定时发送数据。
5、可以打开文件夹,选择文件进行发送,并且将发送文件的内容显示在发送文本框中。
6、可以清空发送和接收文本框中的内容。
7、可以实时计算发送和接收的字节数。
8、实现打开文件夹保存发送和接收的文件内容(目前只支持.txt文件)。
9、实时显示当前时间。

功能演示

1.界面功能介绍

在这里插入图片描述

2.串口接收数据演示

在这里插入图片描述

主要应用思路

1、数据保存和读取。
2、定时器的操作。
3、串口模块的使用。
4、委托和事件解决线程冲突问题。

部分代码展示

namespace MainSender
{
    // 解决线程访问问题
    public delegate void SerialPortEventHandler(Object sender, SerialPortEventArgs e);    // 定义委托
        /// <summary>
        /// 串口初始化设置
        /// </summary>

        public void InitializeSerialSet()
        {
            InitializePorts();   // 初始化串口号
            // 初始化波特率
            comboBox_BandRate.Text = comboBox_BandRate.Items[6].ToString();
            // 初始化校验位
            comboBox_Check.Text = comboBox_Check.Items[0].ToString();
            // 初始化数据位
            comboBox_Data.Text = comboBox_Data.Items[0].ToString();
            // 初始化停止位
            comboBox_Stop.Text = comboBox_Stop.Items[0].ToString();

        }
        /// <summary>
        /// 可用串口扫描,并且显示
        /// </summary>
        public void InitializePorts()
        {
            comboBox_Serial.Items.Clear();   // 清空原来的信息
            // 返回可用串口号,形式:COM3
            string[] arraysPostsNames = SerialPort.GetPortNames();  // 获取所有可用的串口号

            // 检查串口号是否正确
            if (arraysPostsNames.Length > 0)
            {

                Array.Sort(arraysPostsNames);  // 使用默认进行排序,从小到大顺序
                for (int i = 0; i < arraysPostsNames.Length; i++)
                {
                    comboBox_Serial.Items.Add(arraysPostsNames[i]);  // 将所有可用串口加载到串口显示框当中
                }
                comboBox_Serial.Text = arraysPostsNames[0];   // 默认选择第一个串口

                comboBox_Serial.Enabled = true;   // 打开选择框
                // 设置状态栏属性
                toolStripStatus_Port.Text = "串口号:" + comboBox_Serial.Text;  // 设置状态栏的情况                   
                toolStripStatus_Port.ForeColor = Color.Black; // 设置为红色

            }
            else
            {
                toolStripStatus_Port.Text = "没有可用串口";  // 设置状态栏的情况                   
                toolStripStatus_Port.ForeColor = Color.Red; // 设置为红色
                comboBox_Serial.Text = "None";   // 提示没有可用串口
                comboBox_Serial.Enabled = false;   // 禁止打开串口选择框
            }
        }
        /// <summary>
        /// 将文件内容显示在发送数据显示框中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ReadFile(string filepath)
        {
            try
            {
                richTextBox_Send.Text = "";  // 清空显示框

                StreamReader sr = new StreamReader(filepath, Encoding.Default);
                string content;
                while ((content = sr.ReadLine()) != null)   // 按行读取显示在发送数据框中
                {
                    richTextBox_Send.Text += (content.ToString() + "\r\n");   // ReadLine默认不会读取换行符
                }
            }
            catch (Exception)
            {

            }

        }

工程源码

如果你对本工程有兴趣,可以下载工程源码https://download.csdn.net/download/YiBYiH/15878860

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值