PowerSupplierModbusDevice.cs 17 KB

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