C# 串口通信(通过serialPort控件发送及接收数据)

连接串口

首先可以在 工具箱中搜索serialport,将控件拖到你的Winfrom窗口。

界面设计

打开串口

csharp 复制代码
 private void Connect_Click(object sender, EventArgs e)
 {
     serialPort1.PortName = comboBox2.Text;//端口名
     serialPort1.BaudRate = int.Parse(comboBox3.Text);//比特率
     serialPort1.StopBits = StopBits.One;//停止位
     switch (comboBox4.Text)//校验位
     {
         case "None":
             serialPort1.Parity = Parity.None;
             break;
         case "Odd":
             serialPort1.Parity = Parity.Odd;
             break;
         case "Even":
             serialPort1.Parity = Parity.Even;
             break;
         case "Mark":
             serialPort1.Parity = Parity.Mark;
             break;
     }
     serialPort1.DataBits = int.Parse(comboBox5.Text);//数据位
     try
     {
         serialPort1.Open();//打开串口
         bool b = serialPort1.CtsHolding;//CTS状态
		 label8.Text = b ? "1" : "0";
         if (checkBox2.CheckState == CheckState.Checked)//判断是否勾选RTS
         {
             serialPort1.RtsEnable = true;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
    button3.Text=serialPort1.IsOpen ? "关闭串口" : "打开串口";
 }

发送数据

复制代码
liens为全局变量,可以直接在richTextBox里输入,然后再点发送。

通过文件发送

csharp 复制代码
 private void OpenFile_Click(object sender, EventArgs e)
 {
     OpenFileDialog openFileDialog = new OpenFileDialog();
     openFileDialog.InitialDirectory = "d:\\";//设置默认打开路径

     // 设置文件过滤选项
     openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";

     // 设置标题
     openFileDialog.Title = "选择文件";

     // 显示对话框
     DialogResult result = openFileDialog.ShowDialog();

     // 确认用户没有取消操作
     if (result == DialogResult.OK)
     {
         // 获取选中的文件路径
         string filePath = openFileDialog.FileName;
         comboBox1.Items.Add(filePath);
         liens = File.ReadAllText(filePath);//读取文件
         comboBox1.Text = filePath;
         richTextBox1.Text=liens;
     }
 }

发送数据

csharp 复制代码
private void Send_Click(object sender, EventArgs e)
{
	serialPort1.WriteLine(liens);
}

接收数据

csharp 复制代码
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    int len = serialPort1.BytesToRead;//获取可以读取的字节数

    byte[] buff = new byte[len];//创建缓存数据数组

    serialPort1.Read(buff, 0, len);//把数据读取到buff数组

    if (checkBox1.Checked)//是否勾选十六进制显示
    {
        richTextBox1.AppendText(BitConverter.ToString(buff).Replace('-', ' ') + '\n');//转成十六进制显示
    }
    else
    {
        richTextBox1.AppendText(Encoding.ASCII.GetString(data));//转成ASCII码显示
        richTextBox1.AppendText("\n");
    }
}
相关推荐
冷雨夜中漫步2 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
暖馒4 小时前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
m0_736919104 小时前
C++代码风格检查工具
开发语言·c++·算法
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
黎雁·泠崖5 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472466 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ6 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
lly2024067 小时前
C++ 文件和流
开发语言
m0_706653237 小时前
分布式系统安全通信
开发语言·c++·算法
寻寻觅觅☆7 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++