C#实现PC机串口的收发功能(使用ini文件保存客户首次打开选择的参数)

本文介绍了如何使用C#语言来实现PC串口的收发功能,并利用ini文件保存用户首次打开时选择的串口参数。在界面设计中,重点展示了波特率下拉框的配置方法。通过提供的源代码,展示了上位机如何扫描并打开可用的串口号,以及在编程中引用System.IO.Ports命名空间的重要性。
摘要由CSDN通过智能技术生成

界面的布局:

需要注意的是在波特率的下来框中要设置选择的参数列表,可以通过下面截图的方式添加:

实现的源代码如下,注意;:控件的名字编号,要根据自己的界面布局做出修改。这里主要是实现串口控件的使用和基本的逻辑思路。 

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;
using System.Runtime.InteropServices;//DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息。

namespace comport1
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filepath);

        string FileN
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值