当前位置: 首页 > news >正文

读取.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}未做相应的类型处理");}}

相关文章:

  • 【操作系统】零拷贝技术
  • LearnOpenGL02:绘制三角形和矩形
  • 【办公类-99-06】20250512用Python制作PPT的GIF照片动图(统一图片大小、自定义不同切换秒数,以蝴蝶为例)
  • 家具制造行业的现状 质检LIMS如何赋能家具制造企业质检升级
  • 学习黑客5 分钟深入浅出理解系列之 Windows 资源监视器
  • LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
  • Windows 安装 Milvus
  • 基于ESP32的健康智能机器人
  • 使用conda导致无法找到libpython动态库
  • 高粘度、强腐蚀介质解决方案:V型气动带手动活塞式开关调节球阀的五大核心优势-耀圣控制
  • react项目阅读记录
  • 做为一个平台,给第三方提供接口的时候,除了要求让他们申请 appId 和 AppSecret 之外,还应当有哪些安全选项,要过等保3级
  • 加固python文件
  • 用短说社区搭建的沉浸式生活方式分享平台
  • MyBatis-Plus使用 wrapper.apply() 添加自定义 SQL 片段
  • 多线程下的事务失效及解决形式
  • NVMe-oF(NVMe over Fabrics)
  • 图灵爬虫练习平台第九题js逆向
  • 计网学习笔记———通信知识(计算机网络通信单独讲)
  • Flask支持哪些日志框架
  • 夜晚直播/青岛网站seo服务
  • 渝北网站建设公司/seo计费系统开发
  • 传奇网站怎么制作教程/宁波网站优化公司电话
  • 拖拽建设网站源码/网络推广网址
  • 定制化网站开发报价/seo网站关键字优化
  • 解析网站接口怎么做/英雄联盟最新赛事