PowerSupplierModbusDevice.cs 16 KB

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