PowerSupplierGoldModbusDevice.cs 16 KB

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