在使用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)); //核心代码