读取.ini后缀类型的马达配置文件并进行赋值
文章速览
- Test.ini配置文件示例
- 读取
- 文件读取并转化为字典形式
- 2、对相应的轴参数进行赋值
- 轴配置参数类
- 利用反射对创建的配置进行赋值
一个赞,专属于你的足迹!
Test.ini配置文件示例
[ParaAxis0]
PulseUnit=10000
EndSpeed=5
DstpTime=0.2
HOMELOWSPEED=10
HOMEHIGHSPEED=50
HOMEACC=0.1
HOMEDEC=0.1
HOMEMODE=1
HOMEPOS=0
EMGValid=0
EMGHighValid=0
EmgMsgMapIoType=6
EmgMsgMapIoIndex=0
EmgMsgMapIoFilter=0
DstpMsgMapIoType=6
DstpMsgMapIoIndex=0
DstpMsgMapIoFilter=0
DstpValid=0
DstpHighValid=0
basic_export=1
home_export=1
encoder_export=1
el_export=1
sl_export=1
inp_export=1
alm_export=1
slow_export=1
emg_export=1
TAcc=0.3
TDec=0.2
StartSpeed=5
Speed=500
SCurve=0.5
SlValid=1
SlSource=0
SlPlimitValue=307
SlNlimitValue=-307
SlStopMode=1
adv_export=1
[ParaAxis1]
PulseUnit=10000
EndSpeed=0.5
DstpTime=0.2
HOMELOWSPEED=1
HOMEHIGHSPEED=2
HOMEACC=0.1
HOMEDEC=0.1
HOMEMODE=26
HOMEPOS=0
EMGValid=0
EMGHighValid=0
EmgMsgMapIoType=6
EmgMsgMapIoIndex=0
EmgMsgMapIoFilter=0
DstpMsgMapIoType=6
DstpMsgMapIoIndex=0
DstpMsgMapIoFilter=0
DstpValid=0
DstpHighValid=0
basic_export=1
home_export=1
encoder_export=1
el_export=1
sl_export=1
inp_export=1
alm_export=1
slow_export=1
emg_export=1
TAcc=0.3
TDec=0.2
StartSpeed=0.1
Speed=0.5
SCurve=0.5
SlValid=0
SlSource=0
SlPlimitValue=1000
SlNlimitValue=0
SlStopMode=1
adv_export=1
[ParaAxis2]
PulseUnit=16384
EndSpeed=0
DstpTime=0.2
HOMELOWSPEED=50
HOMEHIGHSPEED=200
HOMEACC=0.1
HOMEDEC=0.1
HOMEMODE=6
HOMEPOS=0
EMGValid=0
EMGHighValid=0
EmgMsgMapIoType=6
EmgMsgMapIoIndex=0
EmgMsgMapIoFilter=0
DstpMsgMapIoType=6
DstpMsgMapIoIndex=0
DstpMsgMapIoFilter=0
DstpValid=0
DstpHighValid=0
basic_export=1
home_export=1
encoder_export=1
el_export=1
sl_export=1
inp_export=1
alm_export=1
slow_export=1
emg_export=1
TAcc=0.1
TDec=0.1
StartSpeed=20
Speed=200
SCurve=0.5
SlValid=0
SlSource=0
SlPlimitValue=1000
SlNlimitValue=0
SlStopMode=1
adv_export=1
[ParaLiner0]
VSTARTSPEED=0
VENDSPEED=0
VDECSTOPTIME=0
VSPEED=0
VACC=0
VDEC=0
VSCURVE=0
[ParaLiner1]
VSTARTSPEED=0
VENDSPEED=0
VDECSTOPTIME=0
VSPEED=0
VACC=0
VDEC=0
VSCURVE=0
[ParaLiner2]
VSTARTSPEED=0
VENDSPEED=0
VDECSTOPTIME=0
VSPEED=0
VACC=0
VDEC=0
VSCURVE=0
[ParaLiner3]
VSTARTSPEED=0
VENDSPEED=0
VDECSTOPTIME=0
VSPEED=0
VACC=0
VDEC=0
VSCURVE=0
读取
文件读取并转化为字典形式
/// <summary>/// 读取ini配置文件内容/// </summary>/// <param name="filePath"></param>/// <returns></returns>private Dictionary<string, Dictionary<string, string>> ReadIniFile(){var iniContent = new Dictionary<string,Dictionary<string, string>>();var filePath = GetLsINIConfig();try{string[] lines = File.ReadAllLines(filePath, Encoding.Default);string currentSection = "";foreach (string line in lines){string trimmedLine = line.Trim();//跳过注释行 空行if (trimmedLine.StartsWith(";") || trimmedLine == ""){continue;}// 判断是否为节标题if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]")){currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);iniContent[currentSection] = new Dictionary<string, string>();}else if (!string.IsNullOrEmpty(currentSection)){// 解析键值对int equalIndex = trimmedLine.IndexOf('=');if (equalIndex > 0){string key = trimmedLine.Substring(0, equalIndex).Trim();string value = trimmedLine.Substring(equalIndex + 1).Trim();iniContent[currentSection][key] = value;}}}}catch (Exception ex){Logger.Error(ex, $"读取 INI 文件 {filePath} 时出错");}return iniContent;}
2、对相应的轴参数进行赋值
根据需求,创建对应的轴参数字典,同时根据配置文件需要使用到的参数创建配置类
这样就可以利用反射获取配置类中的属性名称,再到读取的配置文件中查找相应的值
轴配置参数类
public class AxisConfigModel{/// <summary>/// 脉冲当量/// </summary>public ushort PulseUnit { get; set; } = 1000;/// <summary>/// S段时间/// </summary>public double SCurve { get; set; } = 0.2;//---------------位移--------------------/// <summary>/// 轴速度/// </summary>public double Speed { get; set; } = 0.5;/// <summary>/// 加速度/// </summary>public double TAcc { get; set; } = 0.1;/// <summary>/// 减速度/// </summary>public double TDec { get; set; } = 0.1;/// <summary>/// 起始速度/// </summary>public double StartSpeed { get; set; } = 0.1;/// <summary>/// 中止速度/// </summary>public double EndSpeed { get; set; } = 0.5;}
利用反射对创建的配置进行赋值
/// <summary>/// 获取轴的配置文件值/// </summary>/// <param name="axis"></param>/// <returns></returns>private AxisConfigModel GetAxisValue(Dictionary<string, string> _axisConfigs){var ret = new AxisConfigModel();//获取对象的类型信息Type type = ret.GetType();//获取类中定义的所有公共属性PropertyInfo[] propertyInfos = type.GetProperties();foreach (PropertyInfo property in propertyInfos){string propertyName = property.Name;//设置值SetPropertyValue(ret, property, _axisConfigs[propertyName]);}return ret;}/// <summary>/// 获取指定相应类型的值/// </summary>/// <param name="property"></param>/// <param name="configValue"></param>/// <returns></returns>private void SetPropertyValue(AxisConfigModel AxisConfigModel, PropertyInfo property,string configValue){if (property.PropertyType == typeof(ushort)){//值转换不判断,出错直接crash上报var value = ushort.Parse(configValue);property.SetValue(AxisConfigModel, value);}else if (property.PropertyType == typeof(double)){//值转换不判断,出错直接crash上报var value = double.Parse(configValue);property.SetValue(AxisConfigModel, value);}else{throw new Exception($"属性{property.Name},类型{property.PropertyType}未做相应的类型处理");}}