PowerSupplierSocketSimulator.cs 18 KB

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