PowerSupplierSocketSimulator.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.ConfigCenter;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.Util;
  6. using CyberX8_Core;
  7. using MECF.Framework.Common.CommonData;
  8. using MECF.Framework.Common.CommonData.PowerSupplier;
  9. using MECF.Framework.Common.Device.PowerSupplier;
  10. using MECF.Framework.Common.Device.Wago;
  11. using MECF.Framework.Common.Net;
  12. using MECF.Framework.Common.Utilities;
  13. using MECF.Framework.Simulator.Core.Driver;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Reflection;
  19. using System.Security.Cryptography.X509Certificates;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Timers;
  24. using System.Xml.Linq;
  25. namespace CyberX8_Simulator.Devices
  26. {
  27. public class PowerSupplierSocketSimulator : SocketDeviceSimulator
  28. {
  29. private const short POWER_CONTROL_ADDRESS = 0x0113;
  30. private const short STEP_PERIOD_ADDRESS = 0x1400;
  31. private const short STEP_PERIOD_START_ADDRESS = 0x1640;
  32. private const short GOLD_CURRENT_SETTING_ADDRESS = 0x6102;
  33. private const short GOLD_STEP_PERIOD_ADDRESS = 0x77D0;
  34. private const short GOLD_STEP_PERIOD_START_ADDRESS = 0x7C50;
  35. /// <summary>
  36. /// 电源状态(00-cv输出,01-cc输出)
  37. /// </summary>
  38. private const short POWER_STATUS_ADDRESS = 0x0200;
  39. Dictionary<int, PowerSupplierData> _powerSupplierDic = new Dictionary<int, PowerSupplierData>();
  40. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  41. private List<PowerSupplierStepPeriodData> _powerSupplierDatas = new List<PowerSupplierStepPeriodData>();
  42. private byte _startStep = 1;
  43. private byte _endStep = 255;
  44. private byte _cycle = 1;
  45. private byte _currentStep = 0;
  46. private int _currentLength = 0;
  47. private DateTime _currentTime = DateTime.Now;
  48. private bool _isGoldPower = false;
  49. private int _currentSetScale;
  50. private System.Timers.Timer _timer;
  51. /// <summary>
  52. /// 记录每次设置的电流值
  53. /// </summary>
  54. private byte[] _powerSupplierSetPoint = new byte[4];
  55. public PowerSupplierSocketSimulator(int port):base(port)
  56. {
  57. PowerSupplierData powerSupplierData1 = new PowerSupplierData();
  58. powerSupplierData1.Current = 0;
  59. powerSupplierData1.Voltage = 56000;
  60. _powerSupplierDic[1] = powerSupplierData1;
  61. PowerSupplierData powerSupplierData2 = new PowerSupplierData();
  62. powerSupplierData2.Current = 0;
  63. powerSupplierData2.Voltage = 2000;
  64. _powerSupplierDic[2] = powerSupplierData2;
  65. PowerSupplierData powerSupplierData3 = new PowerSupplierData();
  66. powerSupplierData3.Current = 0;
  67. powerSupplierData3.Voltage = 3000;
  68. _powerSupplierDic[3] = powerSupplierData3;
  69. PowerSupplierData powerSupplierData4 = new PowerSupplierData();
  70. powerSupplierData4.Current = 0;
  71. powerSupplierData4.Voltage = 4000;
  72. _powerSupplierDic[4] = powerSupplierData4;
  73. _timer = new System.Timers.Timer();
  74. _timer.Interval = 1000;
  75. _timer.Elapsed += Timer_Elapsed;
  76. InitializeParamater(port);
  77. }
  78. private void InitializeParamater(int port)
  79. {
  80. try
  81. {
  82. string oldXmlPath = PathManager.GetCfgDir();
  83. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\PowerSupplierCfg-Simulator.xml";
  84. PowerSupplierDeviceConfigCfg cfg = CustomXmlSerializer.Deserialize<PowerSupplierDeviceConfigCfg>(new FileInfo(newXmlPath));
  85. if (cfg != null)
  86. {
  87. foreach (PowerSupplierDeviceConfig config in cfg.PowerSupplierDeviceConfigs)
  88. {
  89. if(config.Port != port)
  90. {
  91. continue;
  92. }
  93. else
  94. {
  95. foreach (PowerSupplierDevice device in config.Devices)
  96. {
  97. if (device != null)
  98. {
  99. if (config.Type == 1 && device.UnitSetScale > device.UnitScale) //金槽电源
  100. {
  101. _currentSetScale = device.UnitSetScale / device.UnitScale;
  102. }
  103. else
  104. {
  105. _currentSetScale = device.UnitScale / device.UnitSetScale;
  106. }
  107. }
  108. break;
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. catch
  116. {
  117. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, "PowerSupplier", "Load power supplier xml failed");
  118. }
  119. }
  120. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  121. {
  122. if(_currentStep>=_cycle*_endStep)
  123. {
  124. _timer.Stop();
  125. }
  126. if(DateTime.Now.Subtract(_currentTime).TotalSeconds>=_currentLength)
  127. {
  128. _currentStep++;
  129. if (_currentStep >= _cycle * _endStep)
  130. {
  131. _powerSupplierDic[1].Current = 0;
  132. _timer.Stop();
  133. return;
  134. }
  135. _currentTime = DateTime.Now;
  136. _currentLength = (int)(_powerSupplierDatas[_currentStep].Hour * 3600 + _powerSupplierDatas[_currentStep].Minute * 60 +
  137. _powerSupplierDatas[_currentStep].Second);
  138. if (_isGoldPower)
  139. {
  140. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current);
  141. }
  142. else
  143. {
  144. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current * _currentSetScale);
  145. }
  146. }
  147. }
  148. protected override void ProcessUnsplitMessage(byte[] data)
  149. {
  150. short flag = byteTransform.TransInt16(data, 0);
  151. byte channel = data[6];
  152. byte command = data[7];
  153. if(!_powerSupplierDic.ContainsKey(channel))
  154. {
  155. OnWriteMessage(CreateError(flag,channel, command, 0x02));
  156. return;
  157. }
  158. if (command == 0x03)//读取
  159. {
  160. short startAddress = byteTransform.TransInt16(data, 8);
  161. short registerCount = byteTransform.TransInt16(data, 10);
  162. if (startAddress == 0x201)
  163. {
  164. byte[] bytes = new byte[2*registerCount];
  165. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Voltage), 0, bytes, 0, 4);
  166. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Current), 0, bytes, 4, 4);
  167. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  168. return;
  169. }
  170. else if (startAddress == 0x0110)
  171. {
  172. byte[] bytes = new byte[2];
  173. bytes[0] = 0;
  174. bytes[1] = _powerSupplierDic[channel].Enabled?(byte)1:(byte)0;
  175. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  176. return;
  177. }
  178. else if (startAddress == POWER_STATUS_ADDRESS)
  179. {
  180. byte[] bytes = new byte[2];
  181. bytes[0] = 0;
  182. bytes[1] = (byte)_powerSupplierDic[channel].PowerStatus;
  183. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  184. return;
  185. }
  186. else if (startAddress == 0x0101)
  187. {
  188. byte[] bytes = new byte[2];
  189. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].CurrentSetting), 0, bytes, 0, bytes.Length);
  190. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  191. return;
  192. }
  193. else if(startAddress == GOLD_CURRENT_SETTING_ADDRESS)
  194. {
  195. _isGoldPower = true;
  196. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, _powerSupplierSetPoint));
  197. return;
  198. }
  199. else if (startAddress == 0X80)
  200. {
  201. byte[] bytes = new byte[2];
  202. bytes[0] = 0;
  203. bytes[1] = _powerSupplierDic[channel].OutputSwitchControl;
  204. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  205. return;
  206. }
  207. else if (startAddress == 0x0111)
  208. {
  209. byte[] bytes = new byte[2];
  210. bytes[0] = 0;
  211. bytes[1] = (byte)_powerSupplierDic[channel].RunModel;
  212. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  213. return;
  214. }
  215. else if (startAddress == POWER_CONTROL_ADDRESS)
  216. {
  217. byte[] bytes = new byte[2];
  218. bytes[0] = 0;
  219. bytes[1] = (byte)_powerSupplierDic[channel].PowerControl;
  220. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  221. return;
  222. }
  223. else
  224. {
  225. OnWriteMessage(CreateError(flag, channel, command, 03));
  226. }
  227. }
  228. else if (command == 0x06)//设置
  229. {
  230. short startAddress = byteTransform.TransInt16(data, 8);
  231. short value = byteTransform.TransInt16(data, 10);
  232. if(startAddress==0x0110)
  233. {
  234. UpdateChannelEnable(flag, channel, command, startAddress, data[11]);
  235. if (_powerSupplierDic[channel].Enabled)
  236. {
  237. if (_powerSupplierDic[channel].RunModel == 1)
  238. {
  239. if (_isGoldPower)
  240. {
  241. int count = byteTransform.TransInt32(_powerSupplierSetPoint, 0);
  242. _powerSupplierDic[channel].Current = count / _currentSetScale;
  243. }
  244. else
  245. {
  246. _powerSupplierDic[channel].Current = _powerSupplierDic[channel].CurrentSetting * _currentSetScale;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. _powerSupplierDic[channel].Current = 0;
  253. }
  254. }
  255. else if (startAddress == 0x0101)
  256. {
  257. UpdateChannelCurrent(flag,channel, command, startAddress, value);
  258. }
  259. else if (startAddress == POWER_CONTROL_ADDRESS)
  260. {
  261. UpdateChannelPowerControl(flag, channel, command, startAddress, data[11]);
  262. }
  263. else if(startAddress== 0x0111)
  264. {
  265. UpdateChannelRunModel(flag, channel, command, startAddress, data[11]);
  266. }
  267. else
  268. {
  269. byte[] errorByt = CreateError(flag, channel, command, 03);
  270. OnWriteMessage(errorByt);
  271. return;
  272. }
  273. }
  274. else if(command==0x10) //写多个寄存器
  275. {
  276. short startAddress = byteTransform.TransInt16(data, 8);
  277. short length = byteTransform.TransInt16(data, 10);
  278. if (startAddress == STEP_PERIOD_ADDRESS)
  279. {
  280. _powerSupplierDatas.Clear();
  281. int listCount = length / 6;
  282. for(int i=0;i<listCount;i++)
  283. {
  284. PowerSupplierStepPeriodData powerSupplierData = new PowerSupplierStepPeriodData();
  285. powerSupplierData.Voltage = byteTransform.TransInt16(data, 13 + i * 12);
  286. powerSupplierData.Current = byteTransform.TransInt16(data, 15 + i * 12);
  287. powerSupplierData.Hour = byteTransform.TransUInt16(data, 17 + i * 12);
  288. powerSupplierData.Minute = byteTransform.TransUInt16(data, 19 + i * 12);
  289. powerSupplierData.Second = byteTransform.TransUInt16(data, 21 + i * 12);
  290. powerSupplierData.Microsecond = byteTransform.TransUInt16(data, 23 + i * 12);
  291. _powerSupplierDatas.Add(powerSupplierData);
  292. }
  293. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  294. }
  295. else if(startAddress==STEP_PERIOD_START_ADDRESS)
  296. {
  297. _startStep = (byte)byteTransform.TransInt16(data,13);
  298. _endStep = (byte)byteTransform.TransInt16(data, 15);
  299. _cycle = (byte)byteTransform.TransInt16(data, 17);
  300. _currentLength = (_powerSupplierDatas[0].Hour * 3600 + _powerSupplierDatas[0].Minute * 60 +
  301. _powerSupplierDatas[0].Second);
  302. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[0].Current*10);
  303. _currentTime = DateTime.Now;
  304. _currentStep = 0;
  305. _timer.Start();
  306. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  307. }
  308. else if(startAddress == GOLD_CURRENT_SETTING_ADDRESS) //设置电流 32位变量
  309. {
  310. _isGoldPower = true;
  311. byte[] vauleData = new byte[4]; //0,1是高位,2,3是低位
  312. Array.Copy(data, 13, vauleData, 0, 4);
  313. _powerSupplierSetPoint = vauleData;
  314. int count = byteTransform.TransInt32(vauleData, 0);
  315. UpdateChannelGoldCurrent(flag, channel, command, (short)count);
  316. }
  317. else if(startAddress == GOLD_STEP_PERIOD_ADDRESS) //设置步阶数据 32位变量
  318. {
  319. _powerSupplierDatas.Clear();
  320. int listCount = length / 12;
  321. for (int i = 0; i < listCount; i++)
  322. {
  323. PowerSupplierStepPeriodData powerSupplierData = new PowerSupplierStepPeriodData();
  324. powerSupplierData.Voltage = byteTransform.TransInt32(data, 13 + i * 24);
  325. powerSupplierData.Current = byteTransform.TransInt32(data, 17 + i * 24) / _currentSetScale;
  326. powerSupplierData.Hour = byteTransform.TransUInt16(data, 23 + i * 24);
  327. powerSupplierData.Minute = byteTransform.TransUInt16(data, 27 + i * 24);
  328. powerSupplierData.Second = byteTransform.TransUInt16(data, 31 + i * 24);
  329. powerSupplierData.Microsecond = byteTransform.TransUInt16(data, 35 + i * 24);
  330. _powerSupplierDatas.Add(powerSupplierData);
  331. }
  332. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  333. }
  334. else if (startAddress == GOLD_STEP_PERIOD_START_ADDRESS) //启动步阶数据 32位变量
  335. {
  336. _startStep = (byte)byteTransform.TransInt32(data, 13);
  337. _endStep = (byte)byteTransform.TransInt32(data, 17);
  338. _cycle = (byte)byteTransform.TransInt32(data, 21);
  339. _currentLength = (_powerSupplierDatas[0].Hour * 3600 + _powerSupplierDatas[0].Minute * 60 +
  340. _powerSupplierDatas[0].Second);
  341. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[0].Current);
  342. _currentTime = DateTime.Now;
  343. _currentStep = 0;
  344. _timer.Start();
  345. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  346. }
  347. }
  348. else
  349. {
  350. OnWriteMessage(CreateError(flag, channel, command, 0x01));
  351. }
  352. }
  353. private byte[] CreateReadResponse(short flag,byte channel,byte command,short registerCount, byte[] values)
  354. {
  355. byte[] bytes = new byte[3 + values.Length + 6];
  356. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  357. bytes[2] = 0x00;
  358. bytes[3] = 0x00;
  359. short dataLength=(short)(3+ values.Length);
  360. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  361. bytes[6] = channel;
  362. bytes[7] = command;
  363. bytes[8] = (byte)(2 * registerCount);
  364. Array.Copy(values,0,bytes, 9, values.Length);
  365. return bytes;
  366. }
  367. private void UpdateChannelCurrent(short flag,byte channel, byte command, short startAddress, short value)
  368. {
  369. _powerSupplierDic[channel].CurrentSetting = value;
  370. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  371. }
  372. private void UpdateChannelGoldCurrent(short flag, byte channel, byte command, short count)
  373. {
  374. _powerSupplierDic[channel].CurrentSetting = count;
  375. OnWriteMessage(CreateMultiWriteResponse(flag, channel, command, 2));
  376. }
  377. private void UpdateChannelEnabled(short flag,byte channel, byte command, short startAddress, short value)
  378. {
  379. _powerSupplierDic[channel].OutputSwitchControl = (byte)value;
  380. OnWriteMessage(CreateWriteResponse(flag,channel, command, startAddress, value));
  381. }
  382. private void UpdateChannelPowerControl(short flag, byte channel, byte command, short startAddress, short value)
  383. {
  384. _powerSupplierDic[channel].PowerControl = (byte)value;
  385. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  386. }
  387. private void UpdateChannelRunModel(short flag, byte channel, byte command, short startAddress, short value)
  388. {
  389. _powerSupplierDic[channel].RunModel = (byte)value;
  390. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  391. }
  392. private void UpdateChannelEnable(short flag, byte channel, byte command, short startAddress, short value)
  393. {
  394. _powerSupplierDic[channel].Enabled = value == 1;
  395. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  396. }
  397. private byte[] CreateWriteResponse(short flag,byte channel,byte command, short startAddress,short value)
  398. {
  399. byte[] byt = new byte[12];
  400. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  401. byt[2] = 0x00;
  402. byt[3] = 0x00;
  403. byt[4] = 0x00;
  404. byt[5] = 0x06;
  405. byt[6] = channel;
  406. byt[7] = command;
  407. byte[] addressByt= byteTransform.GetBytes(startAddress);
  408. Array.Copy(addressByt, 0, byt, 8, 2);
  409. byte[] valueByt=byteTransform.GetBytes(value);
  410. Array.Copy(valueByt, 0, byt, 10, 2);
  411. return byt;
  412. }
  413. private byte[] CreateMultiWriteResponse(short flag, byte channel, byte command,byte registerCount)
  414. {
  415. byte[] byt = new byte[12];
  416. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  417. byt[2] = 0x00;
  418. byt[3] = 0x00;
  419. byt[4] = 0x00;
  420. byt[5] = 0x06;
  421. byt[6] = channel;
  422. byt[7] = command;
  423. byt[8] = 0x00;
  424. byt[9] = registerCount;
  425. //byte[] addressByt = byteTransform.GetBytes(startAddress);
  426. //Array.Copy(addressByt, 0, byt, 8, 2);
  427. //byte[] valueByt = byteTransform.GetBytes(count);
  428. //Array.Copy(valueByt, 0, byt, 10, 2);
  429. return byt;
  430. }
  431. private byte[] CreateError(short flag,byte channel,byte command,byte error)
  432. {
  433. byte[] byt = new byte[9];
  434. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  435. byt[2] = 0x00;
  436. byt[3] = 0x00;
  437. byt[4] = 0x00;
  438. byt[5] = 0x03;
  439. byt[6]=channel;
  440. byt[7]=(byte)(command|0x80);
  441. byt[8] = error;
  442. return byt;
  443. }
  444. }
  445. internal class PowerSupplierData
  446. {
  447. private short _voltageSetting;
  448. private short _currentSetting;
  449. private byte _outputSwitchControl;
  450. private int _voltage;
  451. private int _current;
  452. private short _powerStatus = (int)PowerStatusEnum.CC;
  453. private short _powerControl=(int)PowerControlEnum.Remote;
  454. private short _runModel = (int)PowerRunModelEnum.Normal;
  455. /// <summary>
  456. /// 可用性
  457. /// </summary>
  458. private bool _enabled;
  459. public short VoltageSetting { get { return _voltageSetting; } set { _voltageSetting = value; } }
  460. public short CurrentSetting { get { return _currentSetting; } set { _currentSetting = value; } }
  461. public byte OutputSwitchControl { get { return _outputSwitchControl; } set { _outputSwitchControl = value; } }
  462. public int Voltage { get { return _voltage; } set { _voltage = value; } }
  463. public int Current { get { return _current; } set { _current = value; } }
  464. public short PowerStatus { get { return _powerStatus; } set { _powerStatus = value; } }
  465. public short PowerControl { get { return _powerControl; } set { _powerControl = value; } }
  466. public short RunModel { get { return _runModel; } set { _runModel = value; } }
  467. public bool Enabled { get { return _enabled; } set { _enabled = value; } }
  468. }
  469. public class PowerSupplierStepPeriodData
  470. {
  471. #region 内部变量
  472. private double _voltage;
  473. private double _current;
  474. private ushort _hour;
  475. private ushort _minute;
  476. private ushort _second;
  477. private ushort _microsecond;
  478. #endregion
  479. #region 属性
  480. public double Voltage { get { return _voltage; } set { _voltage = value; } }
  481. public double Current { get { return _current; } set { _current = value; } }
  482. public ushort Hour { get { return _hour; } set { _hour = value; } }
  483. public ushort Minute { get { return _minute; } set { _minute = value; } }
  484. public ushort Second { get { return _second; } set { _second = value; } }
  485. public ushort Microsecond { get { return _microsecond; } set { _microsecond = value; } }
  486. #endregion
  487. }
  488. }