C# INI文件读取串口参数,string转枚举

在使用ini读取串口配置时,ini文件读取的数据类型为string ,而串口的参数设置校验位和停止位为枚举类型,因类型不匹配,读取出来的字符串不能直接使用,需要作进一步处理,将字符串转换为枚举类型

ini读取字符串并转换为枚举类型示例代码如下:

cs 复制代码
  string _PortName = ReadIni.IniReadValue("ServerPort", "PortName", ReadIni.FileSysIni);读取ini
  string _BaudRate = ReadIni.IniReadValue("ServerPort", "BaudRate", ReadIni.FileSysIni);
  string _Parity = ReadIni.IniReadValue("ServerPort", "Parity", ReadIni.FileSysIni);
  string _DataBits = ReadIni.IniReadValue("ServerPort", "DataBits", ReadIni.FileSysIni);
  string _StopBits = ReadIni.IniReadValue("ServerPort", "StopBits", ReadIni.FileSysIni);
  SerialPort = new SerialPort(_PortName,int.Parse(_BaudRate), (Parity)Enum.Parse(typeof(Parity), _Parity), 8, (StopBits)Enum.Parse(typeof(StopBits), _StopBits)); //核心代码
相关推荐
用户37215742613521 小时前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船1 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
huaqianzkh2 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261353 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
公子小六3 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技3 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13143 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#
Coder_Shenshen4 天前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
寂寞的船4 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
1-1=04 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore