TemperatureControllerSerialPortDevice.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Device.TemperatureController;
  5. using MECF.Framework.Simulator.Core.Driver;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.Simulator.Core.Commons
  14. {
  15. public class TemperatureControllerSerialPortDevice : BaseSerialSimulator
  16. {
  17. #region 常量
  18. private const string PORT_NAME = "com12";
  19. private const byte ADD_FLAG = 0x30;
  20. #endregion
  21. #region 内部变量
  22. /// <summary>
  23. /// 控制字字典
  24. /// </summary>
  25. private Dictionary<byte, string> dic = new Dictionary<byte, string>()
  26. {
  27. { 0x05,"ENQ" },
  28. { 0x02,"STX" },
  29. { 0x03,"ETX" },
  30. { 0x06,"ACK" },
  31. { 0x0D,"CR" },
  32. { 0x01,"SOH" }
  33. };
  34. /// <summary>
  35. /// 命令字典
  36. /// </summary>
  37. private Dictionary<byte, string> comdic = new Dictionary<byte, string>()
  38. {
  39. { 0x31,"Target Temp" },
  40. { 0x32,"Internal" },
  41. { 0x33,"External" },
  42. { 0x34,"Alarm" },
  43. { 0x35,"Average" },
  44. { 0x36,"offset" },
  45. { 0x39,"control" },
  46. { 0x41,"PB" },
  47. { 0x42,"ARW" },
  48. { 0x43,"IConstant" },
  49. { 0x44,"DConstant" },
  50. { 0x45,"OutPutRatio" },
  51. { 0x46,"HeatingLimit" },
  52. { 0x47,"CoolingLimit" },
  53. { 0x48,"Saved" }
  54. };
  55. private double[] _reserviorValues = new double[10];
  56. private double[] _heatValues = new double[10];
  57. private double[] _targetValues = new double[10];
  58. private int _controlOperation = 0;
  59. //private byte[] alarm = new byte[12] { 0,0,0,0,1,0,0,0,0,0,0,0};
  60. private byte[] alarm = new byte[3] {0x30,0x33,0x30};
  61. System.Timers.Timer[] _timers;
  62. private int _tcCount = 4;
  63. #endregion
  64. /// <summary>
  65. /// 构造函数
  66. /// </summary>
  67. /// <param name="portName"></param>
  68. /// <param name="type"></param>
  69. public TemperatureControllerSerialPortDevice(string port) : base(port, SerialType.BUFFER)
  70. {
  71. Init(port);
  72. }
  73. /// <summary>
  74. /// 初始化
  75. /// </summary>
  76. /// <param name="port"></param>
  77. private void Init(string port)
  78. {
  79. //加载TC配置
  80. try
  81. {
  82. string oldXmlPath = PathManager.GetCfgDir();
  83. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\SMCCfg.xml";
  84. TemperatureConfig cfg = CustomXmlSerializer.Deserialize<TemperatureConfig>(new FileInfo(newXmlPath));
  85. if (cfg != null)
  86. {
  87. foreach (TemperatureDeviceConfig config in cfg.TemperatureDeviceConfigs)
  88. {
  89. if (int.TryParse(config.Port.Substring(3,2), out int result1) && int.TryParse(port.Substring(3, 2), out int result2))
  90. {
  91. if(result1 + 1 == result2) _tcCount = config.TemperatureDevices.Count;
  92. }
  93. }
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. }
  99. _timers = new System.Timers.Timer[_tcCount];
  100. for(int i = 0; i < _tcCount; i++)
  101. {
  102. _heatValues[i] = 16.5;
  103. _targetValues[i] = 0;
  104. _reserviorValues[i] = 16.6;
  105. _timers[i] = new System.Timers.Timer(50);
  106. switch (i)
  107. {
  108. case 0:
  109. _timers[0].Elapsed += Timer_Elapsed0;
  110. break;
  111. case 1:
  112. _timers[1].Elapsed += Timer_Elapsed1;
  113. break;
  114. case 2:
  115. _timers[2].Elapsed += Timer_Elapsed2;
  116. break;
  117. case 3:
  118. _timers[3].Elapsed += Timer_Elapsed3;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// TC1-1的调温
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void Timer_Elapsed0(object sender, System.Timers.ElapsedEventArgs e)
  131. {
  132. if (Math.Abs(_heatValues[0] - _targetValues[0]) >= 0.2)
  133. {
  134. //内部调温
  135. if (_heatValues[0] < _targetValues[0])
  136. {
  137. _heatValues[0] += 0.1;
  138. }
  139. else
  140. {
  141. _heatValues[0] -= 0.1;
  142. }
  143. //外部调温
  144. if (_reserviorValues[0] < _targetValues[0])
  145. {
  146. _reserviorValues[0] += 0.1;
  147. }
  148. else
  149. {
  150. _reserviorValues[0] -= 0.1;
  151. }
  152. }
  153. else
  154. {
  155. _timers[0].Stop();
  156. }
  157. }
  158. /// <summary>
  159. /// TC1-2的调温
  160. /// </summary>
  161. /// <param name="sender"></param>
  162. /// <param name="e"></param>
  163. private void Timer_Elapsed1(object sender, System.Timers.ElapsedEventArgs e)
  164. {
  165. if (Math.Abs(_heatValues[1] - _targetValues[1]) >= 0.2)
  166. {
  167. //内部调温
  168. if (_heatValues[1] < _targetValues[1])
  169. {
  170. _heatValues[1] += 0.1;
  171. }
  172. else
  173. {
  174. _heatValues[1] -= 0.1;
  175. }
  176. //外部调温
  177. if (_reserviorValues[1] < _targetValues[1])
  178. {
  179. _reserviorValues[1] += 0.1;
  180. }
  181. else
  182. {
  183. _reserviorValues[1] -= 0.1;
  184. }
  185. }
  186. else
  187. {
  188. _timers[1].Stop();
  189. }
  190. }
  191. /// <summary>
  192. /// TC1-3的调温
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void Timer_Elapsed2(object sender, System.Timers.ElapsedEventArgs e)
  197. {
  198. if (Math.Abs(_heatValues[2] - _targetValues[2]) >= 0.2)
  199. {
  200. //内部调温
  201. if (_heatValues[2] < _targetValues[2])
  202. {
  203. _heatValues[2] += 0.1;
  204. }
  205. else
  206. {
  207. _heatValues[2] -= 0.1;
  208. }
  209. //外部调温
  210. if (_reserviorValues[2] < _targetValues[2])
  211. {
  212. _reserviorValues[2] += 0.1;
  213. }
  214. else
  215. {
  216. _reserviorValues[2] -= 0.1;
  217. }
  218. }
  219. else
  220. {
  221. _timers[2].Stop();
  222. }
  223. }
  224. /// <summary>
  225. /// TC1-4的调温
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void Timer_Elapsed3(object sender, System.Timers.ElapsedEventArgs e)
  230. {
  231. if (Math.Abs(_heatValues[3] - _targetValues[3]) >= 0.2)
  232. {
  233. //内部调温
  234. if (_heatValues[3] < _targetValues[3])
  235. {
  236. _heatValues[3] += 0.1;
  237. }
  238. else
  239. {
  240. _heatValues[3] -= 0.1;
  241. }
  242. //外部调温
  243. if (_reserviorValues[3] < _targetValues[3])
  244. {
  245. _reserviorValues[3] += 0.1;
  246. }
  247. else
  248. {
  249. _reserviorValues[3] -= 0.1;
  250. }
  251. }
  252. else
  253. {
  254. _timers[3].Stop();
  255. }
  256. }
  257. protected override string MessageConvert(byte[] byt)
  258. {
  259. string str = "";
  260. for(int i=0;i<byt.Length;i++)
  261. {
  262. byte item = byt[i];
  263. if(dic.ContainsKey(item))
  264. {
  265. str += dic[item];
  266. }
  267. else if(i==1)
  268. {
  269. str += (item - 0x30).ToString("D2");
  270. }
  271. else if(i==3&&comdic.ContainsKey(item))
  272. {
  273. str += comdic[item];
  274. }
  275. else
  276. {
  277. str += item.ToString("X2");
  278. }
  279. str += " ";
  280. }
  281. return str;
  282. }
  283. protected override void ProcessMessageBuffer(byte[] data)
  284. {
  285. if (data[0] == 0x06 && data[2]==0x0D&&data.Length>7)
  286. {
  287. byte[] byt = new byte[data.Length - 3];
  288. Array.Copy(data, 3, byt, 0, byt.Length);
  289. ProcessNormalCommand(byt);
  290. }
  291. else if (data[0] == 0x01)
  292. {
  293. ProcessNormalCommand(data);
  294. }
  295. }
  296. private void ProcessNormalCommand(byte[] data)
  297. {
  298. if (data[0] == 0x01 && data[data.Length-1]==0x0D)
  299. {
  300. if (data.Length == 7)
  301. {
  302. ProcessReadCommand(data);
  303. }
  304. else if (data.Length > 7)
  305. {
  306. ProcessSetCommand(data);
  307. }
  308. }
  309. }
  310. private void ProcessReadCommand(byte[] data)
  311. {
  312. switch(data[3])
  313. {
  314. case 0x31:
  315. ReadTargetValue(data);
  316. break;
  317. case 0x32:
  318. ReadReserviorValue(data);
  319. break;
  320. case 0x33:
  321. ReadHeatValue(data);
  322. break;
  323. case 0x34:
  324. ReadAlarmValue(data);
  325. break;
  326. case 0x39:
  327. ReadControlOperationValue(data);
  328. break;
  329. }
  330. }
  331. private void ProcessSetCommand(byte[] data)
  332. {
  333. switch(data[3])
  334. {
  335. case 0x31:
  336. _targetValues[data[1] - ADD_FLAG - 1] = (data[4] - ADD_FLAG) * 10 + (data[5] - ADD_FLAG) + (data[6]-ADD_FLAG)*0.1;
  337. _timers[data[1] - ADD_FLAG -1].Start();
  338. break;
  339. case 0x39:
  340. _controlOperation = data[7] - ADD_FLAG;
  341. if(_controlOperation == 0)
  342. {
  343. //停止调温
  344. _timers[data[1] - ADD_FLAG - 1].Stop();
  345. }
  346. else
  347. {
  348. //继续调温
  349. _timers[data[1] - ADD_FLAG - 1].Start();
  350. }
  351. break;
  352. }
  353. WriteConfirmData(data[1]);
  354. }
  355. /// <summary>
  356. /// 回复确认
  357. /// </summary>
  358. /// <param name="id"></param>
  359. private void WriteConfirmData(byte id)
  360. {
  361. byte[] confirm = new byte[3];
  362. confirm[0] = 0x06;
  363. confirm[1] = id;
  364. confirm[2] = 0x0d;
  365. WriteBuffer(confirm);
  366. }
  367. private void ReadTargetValue(byte[] data)
  368. {
  369. byte[] byt = new byte[12];
  370. byt[0] = data[0];
  371. byt[1] = data[1];
  372. byt[2] = 0x02;
  373. byt[3] = data[3];
  374. byte[] decadeByte = GetDecadeBytes(_targetValues[data[1] - ADD_FLAG - 1]);
  375. Array.Copy(decadeByte, 0, byt, 4, decadeByte.Length);
  376. byt[5 + decadeByte.Length] = 0;
  377. byt[6 + decadeByte.Length] = 0;
  378. byt[11] = 0x0D;
  379. WriteBuffer(byt);
  380. }
  381. private void ReadReserviorValue(byte[] data)
  382. {
  383. byte[] byt = new byte[12];
  384. byt[0] = data[0];
  385. byt[1] = data[1];
  386. byt[2] = 0x02;
  387. byt[3] = data[3];
  388. byte[] decadeByte = GetDecadeBytes(_reserviorValues[data[1] - ADD_FLAG - 1]);
  389. Array.Copy(decadeByte, 0, byt, 4, decadeByte.Length);
  390. byt[5 + decadeByte.Length] = 0;
  391. byt[6 + decadeByte.Length] = 0;
  392. byt[11] = 0x0D;
  393. WriteBuffer(byt);
  394. }
  395. private void ReadHeatValue(byte[] data)
  396. {
  397. byte[] byt = new byte[12];
  398. byt[0] = data[0];
  399. byt[1] = data[1];
  400. byt[2] = 0x02;
  401. byt[3] = data[3];
  402. byte[] decadeByte = GetDecadeBytes(_heatValues[data[1] - ADD_FLAG - 1]);
  403. Array.Copy(decadeByte, 0, byt, 4, decadeByte.Length);
  404. byt[5 + decadeByte.Length] = 0;
  405. byt[6 + decadeByte.Length] = 0;
  406. byt[11] = 0x0D;
  407. WriteBuffer(byt);
  408. }
  409. private void ReadControlOperationValue(byte[] data)
  410. {
  411. byte[] byt = new byte[12];
  412. byt[0] = data[0];
  413. byt[1] = data[1];
  414. byt[2] = 0x02;
  415. byt[3] = data[3];
  416. byte[] decadeByte = GetKilloBytes(_controlOperation);
  417. Array.Copy(decadeByte, 0, byt, 4, decadeByte.Length);
  418. byt[5 + decadeByte.Length] = 0;
  419. byt[6 + decadeByte.Length] = 0;
  420. byt[11] = 0x0D;
  421. WriteBuffer(byt);
  422. }
  423. private void ReadAlarmValue(byte[] data)
  424. {
  425. byte[] byt = new byte[8+alarm.Length];
  426. byt[0] = data[0];
  427. byt[1] = data[1];
  428. byt[2] = 0x02;
  429. byt[3] = data[3];
  430. Array.Copy(alarm, 0, byt, 4, alarm.Length);
  431. byt[5 + alarm.Length] = 0;
  432. byt[6 + alarm.Length] = 0;
  433. byt[7+alarm.Length] = 0x0D;
  434. WriteBuffer(byt);
  435. }
  436. /// <summary>
  437. /// 获取温度数组(10|1|0.1|0.01)
  438. /// </summary>
  439. /// <param name="temperature"></param>
  440. /// <returns></returns>
  441. private byte[] GetDecadeBytes(double temperature)
  442. {
  443. byte decade = GetSendByteData((byte)Math.Floor(temperature / 10));
  444. byte unit = GetSendByteData((byte)Math.Floor(temperature % 10));
  445. byte digital = GetSendByteData((byte)Math.Floor(temperature * 10 % 10));
  446. return new byte[4] { decade, unit, digital, ADD_FLAG };
  447. }
  448. /// <summary>
  449. /// 获取千级数组(1000|100|10|1)
  450. /// </summary>
  451. /// <param name="temperature"></param>
  452. /// <returns></returns>
  453. private byte[] GetKilloBytes(double temperature)
  454. {
  455. byte kilo = GetSendByteData((byte)Math.Floor(temperature / 1000));
  456. byte hundred = GetSendByteData((byte)Math.Floor(temperature % 1000/100));
  457. byte decade = GetSendByteData((byte)Math.Floor(temperature % 100 / 10));
  458. byte unit = GetSendByteData((byte)Math.Floor(temperature % 10));
  459. return new byte[4] { kilo, hundred, decade, unit };
  460. }
  461. /// <summary>
  462. /// 获取发送位数据
  463. /// </summary>
  464. /// <param name="originalData"></param>
  465. /// <returns></returns>
  466. private byte GetSendByteData(byte originalData)
  467. {
  468. return (byte)(ADD_FLAG + originalData);
  469. }
  470. }
  471. }