PowerSupplierModbusDevice.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.Util;
  3. using DocumentFormat.OpenXml.InkML;
  4. using MECF.Framework.Common.CommonData.PowerSupplier;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Net;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.Common.Device.PowerSupplier
  14. {
  15. public class PowerSupplierModbusDevice : JetMessageTcpClient<PowerSupplierTcpModbusMessage, PowerSupplierCommand>
  16. {
  17. #region 常量
  18. private const short CURRENT_SETTING_ADDRESS = 0x0101;
  19. private const short OUTPUT_CONTROL_ADDRESS = 0x0110;
  20. private const short STEP_PERIOD_ADDRESS = 0x1400;
  21. private const short STEP_PERIOD_START_ADDRESS = 0x1640;
  22. private const short VOLTAGE_OUTPUT_ADDRESS = 0x0201;
  23. private const short POWER_CONTROL_ADDRESS = 0x0113;
  24. private const short POWER_RUN_MODEL_ADDRESS = 0x0111;
  25. /// <summary>
  26. /// 电源状态(00-cv输出,01-cc输出)
  27. /// </summary>
  28. private const short POWER_STATUS_ADDRESS = 0x0200;
  29. private const string SET_POINT = "SetPoint";
  30. private const string CURRENT = "Current";
  31. private const string VOLTAGE = "Voltage";
  32. private const string ENABLED = "Enabled";
  33. private const string POWER_STATUS = "PowerStatus";
  34. private const string POWER_CONTROL = "PowerControl";
  35. private const string POWER_RUN_MODEL = "PowerRunModel";
  36. /// <summary>
  37. /// 步阶数据数量
  38. /// </summary>
  39. private const int STEP_PERIOD_LENGTH = 6;
  40. #endregion
  41. #region 内部变量
  42. private string _name;
  43. private string _lastErrorMsg = "";
  44. #endregion
  45. /// <summary>
  46. /// 构造函数
  47. /// </summary>
  48. /// <param name="ip"></param>
  49. /// <param name="port"></param>
  50. public PowerSupplierModbusDevice(string name,string ip, int port) : base(ip, port)
  51. {
  52. ReceiveTimeout = 1000;
  53. SendTimeout = 1000;
  54. ReconnectInterval = 3000;
  55. ConnectTimeout = 1000;
  56. _name = name;
  57. }
  58. /// <summary>
  59. /// 连接
  60. /// </summary>
  61. /// <returns></returns>
  62. public void Start()
  63. {
  64. Connect();
  65. }
  66. /// <summary>
  67. /// 设置通道输出开关控制
  68. /// </summary>
  69. /// <param name="channel"></param>
  70. /// <param name="currentValue"></param>
  71. /// <returns></returns>
  72. public bool SetChannelOutputSwitchControl(byte channel, bool enabled)
  73. {
  74. PowerSupplierCommand command = new PowerSupplierCommand();
  75. command.Channel = channel;
  76. command.CommandCode = 0x06;
  77. command.Address = (ushort)(OUTPUT_CONTROL_ADDRESS);
  78. command.Datas = new ushort[] { enabled ? (ushort)01 : (ushort)00 };
  79. return SetOperation(command);
  80. }
  81. /// <summary>
  82. /// 设置电源控制
  83. /// </summary>
  84. /// <param name="channel"></param>
  85. /// <param name="currentValue"></param>
  86. /// <returns></returns>
  87. public bool SetChannelPowerControl(byte channel,byte remoteControl)
  88. {
  89. PowerSupplierCommand command = new PowerSupplierCommand();
  90. command.Channel = channel;
  91. command.CommandCode = 0x06;
  92. command.Address = (ushort)(POWER_CONTROL_ADDRESS);
  93. command.Datas = new ushort[] { remoteControl };
  94. return SetOperation(command);
  95. }
  96. /// <summary>
  97. /// 设置电源运行模式
  98. /// </summary>
  99. /// <param name="channel"></param>
  100. /// <param name="currentValue"></param>
  101. /// <returns></returns>
  102. public bool SetChannelPowerRunmodelControl(byte channel,byte model)
  103. {
  104. PowerSupplierCommand command = new PowerSupplierCommand();
  105. command.Channel = channel;
  106. command.CommandCode = 0x06;
  107. command.Address = (ushort)(POWER_RUN_MODEL_ADDRESS);
  108. command.Datas = new ushort[] { model };
  109. return SetOperation(command);
  110. }
  111. /// <summary>
  112. /// 设置步阶数据
  113. /// </summary>
  114. /// <param name="channel"></param>
  115. /// <param name="stepDatas"></param>
  116. public bool SetStepPeriod(byte channel,List<PowerSupplierStepPeriodData> stepDatas,int scale)
  117. {
  118. PowerSupplierCommand command = new PowerSupplierCommand();
  119. command.Channel = channel;
  120. command.CommandCode = 0x10;
  121. command.Address = (ushort)STEP_PERIOD_ADDRESS;
  122. command.RegisterCount =(ushort)(STEP_PERIOD_LENGTH * stepDatas.Count);
  123. command.Datas = new ushort[STEP_PERIOD_LENGTH * stepDatas.Count];
  124. for(int i = 0;i<stepDatas.Count;i++)
  125. {
  126. PowerSupplierStepPeriodData data = stepDatas[i];
  127. command.Datas[0 + STEP_PERIOD_LENGTH * i] = (ushort)6000;
  128. command.Datas[1 + STEP_PERIOD_LENGTH * i] = (ushort)Math.Round(stepDatas[i].Current*scale,0);
  129. command.Datas[2 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Hour;
  130. command.Datas[3+STEP_PERIOD_LENGTH*i]= stepDatas[i].Minute;
  131. command.Datas[4+STEP_PERIOD_LENGTH*i]= stepDatas[i].Second;
  132. command.Datas[5 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Microsecond;
  133. }
  134. return SetOperation(command);
  135. }
  136. /// <summary>
  137. /// 启动步阶
  138. /// </summary>
  139. /// <param name="channel"></param>
  140. /// <param name="startStep"></param>
  141. /// <param name="endStep"></param>
  142. /// <param name="cycle"></param>
  143. public bool StartStepPeriod(byte channel,ushort startStep,ushort endStep,ushort cycle)
  144. {
  145. PowerSupplierCommand command = new PowerSupplierCommand();
  146. command.Channel = channel;
  147. command.CommandCode = 0x10;
  148. command.Address = (ushort)STEP_PERIOD_START_ADDRESS;
  149. command.RegisterCount = 3;
  150. command.Datas = new ushort[3] { startStep,endStep,cycle };
  151. return SetOperation(command);
  152. }
  153. /// <summary>
  154. /// 设置电流
  155. /// </summary>
  156. /// <param name="channel"></param>
  157. /// <param name="currentValue"></param>
  158. /// <returns></returns>
  159. public bool SetCurrentValue(byte channel,ushort currentValue)
  160. {
  161. PowerSupplierCommand command = new PowerSupplierCommand();
  162. command.Channel = channel;
  163. command.CommandCode = 0x06;
  164. command.Address = (ushort)(CURRENT_SETTING_ADDRESS);
  165. command.Datas = new ushort[] { currentValue };
  166. return SetOperation(command);
  167. }
  168. /// <summary>
  169. /// 设置电源状态
  170. /// </summary>
  171. /// <param name="channel"></param>
  172. /// <param name="currentValue"></param>
  173. /// <returns></returns>
  174. public bool SetChannelPowerStatus(byte channel, byte powerStatus)
  175. {
  176. PowerSupplierCommand command = new PowerSupplierCommand();
  177. command.Channel = channel;
  178. command.CommandCode = 0x06;
  179. command.Address = (ushort)(POWER_STATUS_ADDRESS);
  180. command.Datas = new ushort[] { powerStatus };
  181. return SetOperation(command);
  182. }
  183. /// <summary>
  184. /// 获取通道输出开关控制
  185. /// </summary>
  186. /// <param name="channel"></param>
  187. /// <returns></returns>
  188. public void GetChannelOutput(byte channel)
  189. {
  190. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  191. applyCommand.Channel = channel;
  192. applyCommand.CommandCode = 0x03;
  193. applyCommand.Address = (ushort)(OUTPUT_CONTROL_ADDRESS);
  194. applyCommand.RegisterCount = 1;
  195. applyCommand.Variables.Add(ENABLED,(0,1));
  196. ApplyDataOperation(applyCommand);
  197. }
  198. /// <summary>
  199. /// 获取通道电源控制
  200. /// </summary>
  201. /// <param name="channel"></param>
  202. /// <returns></returns>
  203. public void GetChannelPowerControl(byte channel)
  204. {
  205. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  206. applyCommand.Channel = channel;
  207. applyCommand.CommandCode = 0x03;
  208. applyCommand.Address = (ushort)(POWER_CONTROL_ADDRESS);
  209. applyCommand.RegisterCount = 1;
  210. applyCommand.Variables.Add(POWER_CONTROL, (0, 1));
  211. ApplyDataOperation(applyCommand);
  212. }
  213. /// <summary>
  214. /// 获取通道电流设置数值
  215. /// </summary>
  216. /// <param name="channel"></param>
  217. /// <returns></returns>
  218. public void GetChannelCurrentSetting(byte channel)
  219. {
  220. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  221. applyCommand.Channel = channel;
  222. applyCommand.CommandCode = 0x03;
  223. applyCommand.Address = (ushort)(CURRENT_SETTING_ADDRESS);
  224. applyCommand.RegisterCount = 1;
  225. applyCommand.Variables.Add(SET_POINT,(0,1));
  226. ApplyDataOperation(applyCommand);
  227. }
  228. /// <summary>
  229. /// 获取电源状态设置数值
  230. /// </summary>
  231. /// <param name="channel"></param>
  232. /// <returns></returns>
  233. public void GetChannelPowerStatus(byte channel)
  234. {
  235. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  236. applyCommand.Channel = channel;
  237. applyCommand.CommandCode = 0x03;
  238. applyCommand.Address = (ushort)(POWER_STATUS_ADDRESS);
  239. applyCommand.RegisterCount = 1;
  240. applyCommand.Variables.Add(POWER_STATUS, (0,1));
  241. ApplyDataOperation(applyCommand);
  242. }
  243. /// <summary>
  244. /// 获取电源运行模式
  245. /// </summary>
  246. /// <param name="channel"></param>
  247. /// <returns></returns>
  248. public void GetChannelPowerRunModel(byte channel)
  249. {
  250. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  251. applyCommand.Channel = channel;
  252. applyCommand.CommandCode = 0x03;
  253. applyCommand.Address = (ushort)(POWER_RUN_MODEL_ADDRESS);
  254. applyCommand.RegisterCount = 1;
  255. applyCommand.Variables.Add(POWER_RUN_MODEL, (0, 1));
  256. ApplyDataOperation(applyCommand);
  257. }
  258. /// <summary>
  259. /// 申请电压和电流数值
  260. /// </summary>
  261. /// <param name="channel"></param>
  262. /// <returns></returns>
  263. public void GetChannelVoltageAndCurrent(byte channel)
  264. {
  265. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  266. applyCommand.Channel = channel;
  267. applyCommand.CommandCode = 0x03;
  268. applyCommand.Address = (ushort)(VOLTAGE_OUTPUT_ADDRESS);
  269. applyCommand.RegisterCount = 4;
  270. applyCommand.Variables.Add(VOLTAGE,(0,2));
  271. applyCommand.Variables.Add(CURRENT,(2,2));
  272. ApplyDataOperation(applyCommand);
  273. }
  274. /// <summary>
  275. /// 设置操作
  276. /// </summary>
  277. /// <param name="command"></param>
  278. /// <returns></returns>
  279. private bool SetOperation(PowerSupplierCommand command)
  280. {
  281. if (Connected)
  282. {
  283. NetResult netResult = SetData(command);
  284. if (!netResult.IsSuccess)
  285. {
  286. WriteErrMsg($"write write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  287. return false;
  288. }
  289. return true;
  290. }
  291. else
  292. {
  293. NetResult netResult = Connect();
  294. if(netResult.IsSuccess)
  295. {
  296. netResult = SetData(command);
  297. if (!netResult.IsSuccess)
  298. {
  299. WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  300. return false;
  301. }
  302. return true;
  303. }
  304. else
  305. {
  306. WriteErrMsg("connect failed");
  307. return false;
  308. }
  309. }
  310. }
  311. /// <summary>
  312. /// 申请数据操作
  313. /// </summary>
  314. /// <param name="command"></param>
  315. private void ApplyDataOperation(PowerSupplierCommand command)
  316. {
  317. if (!Connected)
  318. {
  319. NetResult connectResult = Connect();
  320. if (!connectResult.IsSuccess)
  321. {
  322. WriteErrMsg("connect failed");
  323. return;
  324. }
  325. }
  326. NetResult<PowerSupplierCommand> netResult = ApplyData(command);
  327. if (!netResult.IsSuccess)
  328. {
  329. WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}");
  330. return;
  331. }
  332. if(netResult.Data.Datas!=null)
  333. {
  334. Dictionary<string, (int,int)> dictionary = command.Variables;
  335. List<string> keys = dictionary.Keys.ToList();
  336. foreach(string item in keys)
  337. {
  338. var result = dictionary[item];
  339. if(item==ENABLED)
  340. {
  341. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, ENABLED, netResult.Data.Datas[result.Item1] == 0x01);
  342. }
  343. else
  344. {
  345. if(result.Item2==1)
  346. {
  347. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, netResult.Data.Datas[result.Item1]);
  348. }
  349. else if(result.Item2==2)
  350. {
  351. int value = netResult.Data.Datas[result.Item1] * 0xFFFF + netResult.Data.Datas[result.Item1+1];
  352. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, value);
  353. }
  354. }
  355. }
  356. }
  357. }
  358. /// <summary>
  359. /// 写错误日志
  360. /// </summary>
  361. /// <param name="msg"></param>
  362. private void WriteErrMsg(string msg)
  363. {
  364. if (msg!=_lastErrorMsg)
  365. {
  366. _lastErrorMsg = msg;
  367. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, _name, msg);
  368. }
  369. }
  370. }
  371. }