PowerSupplierGoldModbusDevice.cs 16 KB

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