PowerSupplierModbusDevice.cs 15 KB

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