PowerSupplierModbusDevice.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 CURRENT_SETTING_ADDRESS = 0x0101;
  20. private const short OUTPUT_CONTROL_ADDRESS = 0x0110;
  21. private const short STEP_PERIOD_ADDRESS = 0x1400;
  22. private const short STEP_PERIOD_START_ADDRESS = 0x1640;
  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 = 6;
  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 PowerSupplierModbusDevice(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,int voltageUnitSetScale)
  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);
  136. command.Datas[1 + STEP_PERIOD_LENGTH * i] = (ushort)Math.Round(stepDatas[i].Current * scale, 0);
  137. command.Datas[2 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Hour;
  138. command.Datas[3 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Minute;
  139. command.Datas[4 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Second;
  140. command.Datas[5 + STEP_PERIOD_LENGTH * i] = stepDatas[i].Microsecond;
  141. }
  142. return SetOperation(command);
  143. }
  144. /// <summary>
  145. /// 启动步阶
  146. /// </summary>
  147. /// <param name="channel"></param>
  148. /// <param name="startStep"></param>
  149. /// <param name="endStep"></param>
  150. /// <param name="cycle"></param>
  151. public bool StartStepPeriod(byte channel, ushort startStep, ushort endStep, ushort cycle)
  152. {
  153. PowerSupplierCommand command = new PowerSupplierCommand();
  154. command.Channel = channel;
  155. command.CommandCode = 0x10;
  156. command.Address = (ushort)STEP_PERIOD_START_ADDRESS;
  157. command.RegisterCount = 3;
  158. command.Datas = new ushort[3] { startStep, endStep, cycle };
  159. return SetOperation(command);
  160. }
  161. /// <summary>
  162. /// 设置电流
  163. /// </summary>
  164. /// <param name="channel"></param>
  165. /// <param name="currentValue"></param>
  166. /// <returns></returns>
  167. public bool SetCurrentValue(byte channel, int currentValue)
  168. {
  169. PowerSupplierCommand command = new PowerSupplierCommand();
  170. command.Channel = channel;
  171. command.CommandCode = 0x06;
  172. command.Address = (ushort)(CURRENT_SETTING_ADDRESS);
  173. command.Datas = new ushort[] { (ushort)currentValue };
  174. return SetOperation(command);
  175. }
  176. /// <summary>
  177. /// 设置电源状态
  178. /// </summary>
  179. /// <param name="channel"></param>
  180. /// <param name="currentValue"></param>
  181. /// <returns></returns>
  182. public bool SetChannelPowerStatus(byte channel, byte powerStatus)
  183. {
  184. PowerSupplierCommand command = new PowerSupplierCommand();
  185. command.Channel = channel;
  186. command.CommandCode = 0x06;
  187. command.Address = (ushort)(POWER_STATUS_ADDRESS);
  188. command.Datas = new ushort[] { powerStatus };
  189. return SetOperation(command);
  190. }
  191. /// <summary>
  192. /// 获取通道输出开关控制
  193. /// </summary>
  194. /// <param name="channel"></param>
  195. /// <returns></returns>
  196. public void GetChannelOutput(byte channel)
  197. {
  198. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  199. applyCommand.Channel = channel;
  200. applyCommand.CommandCode = 0x03;
  201. applyCommand.Address = (ushort)(OUTPUT_CONTROL_ADDRESS);
  202. applyCommand.RegisterCount = 1;
  203. applyCommand.Variables.Add(ENABLED, (0, 1));
  204. ApplyDataOperation(applyCommand);
  205. }
  206. /// <summary>
  207. /// 获取通道电源控制
  208. /// </summary>
  209. /// <param name="channel"></param>
  210. /// <returns></returns>
  211. public void GetChannelPowerControl(byte channel)
  212. {
  213. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  214. applyCommand.Channel = channel;
  215. applyCommand.CommandCode = 0x03;
  216. applyCommand.Address = (ushort)(POWER_CONTROL_ADDRESS);
  217. applyCommand.RegisterCount = 1;
  218. applyCommand.Variables.Add(POWER_CONTROL, (0, 1));
  219. ApplyDataOperation(applyCommand);
  220. }
  221. /// <summary>
  222. /// 获取通道电流设置数值
  223. /// </summary>
  224. /// <param name="channel"></param>
  225. /// <returns></returns>
  226. public void GetChannelCurrentSetting(byte channel)
  227. {
  228. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  229. applyCommand.Channel = channel;
  230. applyCommand.CommandCode = 0x03;
  231. applyCommand.Address = (ushort)(CURRENT_SETTING_ADDRESS);
  232. applyCommand.RegisterCount = 1;
  233. applyCommand.Variables.Add(SET_POINT, (0, 1));
  234. ApplyDataOperation(applyCommand);
  235. }
  236. /// <summary>
  237. /// 获取电源状态设置数值
  238. /// </summary>
  239. /// <param name="channel"></param>
  240. /// <returns></returns>
  241. public void GetChannelPowerStatus(byte channel)
  242. {
  243. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  244. applyCommand.Channel = channel;
  245. applyCommand.CommandCode = 0x03;
  246. applyCommand.Address = (ushort)(POWER_STATUS_ADDRESS);
  247. applyCommand.RegisterCount = 1;
  248. applyCommand.Variables.Add(POWER_STATUS, (0, 1));
  249. ApplyDataOperation(applyCommand);
  250. }
  251. /// <summary>
  252. /// 获取电源运行模式
  253. /// </summary>
  254. /// <param name="channel"></param>
  255. /// <returns></returns>
  256. public void GetChannelPowerRunModel(byte channel)
  257. {
  258. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  259. applyCommand.Channel = channel;
  260. applyCommand.CommandCode = 0x03;
  261. applyCommand.Address = (ushort)(POWER_RUN_MODEL_ADDRESS);
  262. applyCommand.RegisterCount = 1;
  263. applyCommand.Variables.Add(POWER_RUN_MODEL, (0, 1));
  264. ApplyDataOperation(applyCommand);
  265. }
  266. /// <summary>
  267. /// 申请电压和电流数值
  268. /// </summary>
  269. /// <param name="channel"></param>
  270. /// <returns></returns>
  271. public void GetChannelVoltageAndCurrent(byte channel)
  272. {
  273. PowerSupplierCommand applyCommand = new PowerSupplierCommand();
  274. applyCommand.Channel = channel;
  275. applyCommand.CommandCode = 0x03;
  276. applyCommand.Address = (ushort)(VOLTAGE_OUTPUT_ADDRESS);
  277. applyCommand.RegisterCount = 4;
  278. applyCommand.Variables.Add(VOLTAGE, (0, 2));
  279. applyCommand.Variables.Add(CURRENT, (2, 2));
  280. ApplyDataOperation(applyCommand);
  281. }
  282. /// <summary>
  283. /// 设置操作
  284. /// </summary>
  285. /// <param name="command"></param>
  286. /// <returns></returns>
  287. private bool SetOperation(PowerSupplierCommand command)
  288. {
  289. if (SC.ContainsItem("Log.EnablePowerSupplierLog"))
  290. {
  291. LogEnabled = SC.GetValue<bool>("Log.EnablePowerSupplierLog");
  292. }
  293. if (Connected)
  294. {
  295. NetResult netResult = SetData(command);
  296. if (!netResult.IsSuccess)
  297. {
  298. WriteErrMsg($"write write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  299. return false;
  300. }
  301. return true;
  302. }
  303. else
  304. {
  305. NetResult netResult = Connect();
  306. if (netResult.IsSuccess)
  307. {
  308. netResult = SetData(command);
  309. if (!netResult.IsSuccess)
  310. {
  311. WriteErrMsg($"write {command.Address.ToString("X2")} value {command.Datas[0]} failed,{netResult.Message}");
  312. return false;
  313. }
  314. return true;
  315. }
  316. else
  317. {
  318. WriteErrMsg("connect failed");
  319. return false;
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// 申请数据操作
  325. /// </summary>
  326. /// <param name="command"></param>
  327. private void ApplyDataOperation(PowerSupplierCommand command)
  328. {
  329. if (SC.ContainsItem("Log.EnablePowerSupplierLog"))
  330. {
  331. LogEnabled = SC.GetValue<bool>("Log.EnablePowerSupplierLog");
  332. }
  333. if (!Connected)
  334. {
  335. NetResult connectResult = Connect();
  336. if (!connectResult.IsSuccess)
  337. {
  338. WriteErrMsg("connect failed");
  339. return;
  340. }
  341. }
  342. NetResult<PowerSupplierCommand> netResult = ApplyData(command);
  343. if (!netResult.IsSuccess)
  344. {
  345. WriteErrMsg($"apply {command.Address.ToString("X2")} failed,{netResult.Message}");
  346. return;
  347. }
  348. if (netResult.Data.Datas != null)
  349. {
  350. Dictionary<string, (int, int)> dictionary = command.Variables;
  351. List<string> keys = dictionary.Keys.ToList();
  352. foreach (string item in keys)
  353. {
  354. var result = dictionary[item];
  355. if (item == ENABLED)
  356. {
  357. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, ENABLED, netResult.Data.Datas[result.Item1] == 0x01);
  358. }
  359. else
  360. {
  361. if (result.Item2 == 1)
  362. {
  363. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, netResult.Data.Datas[result.Item1]);
  364. }
  365. else if (result.Item2 == 2)
  366. {
  367. int value = netResult.Data.Datas[result.Item1] * 0xFFFF + netResult.Data.Datas[result.Item1 + 1];
  368. PowerSupplierDeviceConfigManager.Instance.UpdateModuleVariable(_name, command.Channel, item, value);
  369. }
  370. }
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// 写错误日志
  376. /// </summary>
  377. /// <param name="msg"></param>
  378. private void WriteErrMsg(string msg)
  379. {
  380. if (msg != _lastErrorMsg)
  381. {
  382. _lastErrorMsg = msg;
  383. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, _name, msg);
  384. }
  385. }
  386. }
  387. }