PowerSupplierSocketSimulator.cs 19 KB

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