SimLightPdc.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Diagnostics;
  3. using System.Timers;
  4. using MECF.Framework.Simulator.Core.Driver;
  5. namespace MECF.Framework.Simulator.Core.Light
  6. {
  7. class SimLightPdc : SerialPortDeviceSimulator
  8. {
  9. public bool Failed { get; set; }
  10. public bool IsPumpOn { get; set; }
  11. public bool IsOverTemp { get; set; }
  12. public bool IsAtSpeed { get; set; }
  13. Stopwatch _timer = new Stopwatch();
  14. //private System.Timers.Timer _tick;
  15. Random _rd = new Random();
  16. private object _locker = new object();
  17. public string ResultValue { get; set; }
  18. public SimLightPdc(string port)
  19. : base(port, -1, "\r", ' ',false)
  20. {
  21. ResultValue = "";
  22. }
  23. private void _tick_Elapsed(object sender, ElapsedEventArgs e)
  24. {
  25. lock (_locker)
  26. {
  27. if (IsAtSpeed)
  28. {
  29. //if (_timer.Elapsed > TimeSpan.FromSeconds(10))
  30. //{
  31. string text = "0011039806" + _rd.Next(0, 999999).ToString("D6");
  32. int sum = 0;
  33. foreach (var item in text)
  34. {
  35. sum += (int)item;
  36. }
  37. sum = sum % 256;
  38. OnWriteMessage(text + sum.ToString("D3") + "\r");
  39. }
  40. //}
  41. //if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
  42. //{
  43. // _timer.Stop();
  44. // IsAtSpeed = true;
  45. //}
  46. }
  47. }
  48. protected override void ProcessUnsplitMessage(byte[] msg)
  49. {
  50. byte[] message = new byte[] { 0x40, 0x03, 0x01, 0x00, 0x00, 0x44 };
  51. OnWriteMessage(message);
  52. }
  53. protected override void ProcessUnsplitMessage(string msg)
  54. {
  55. //if (!Failed)
  56. {
  57. if (msg == "0010030302=?101\r")
  58. {
  59. //OnWriteMessage("0011030306000000014" + "\r" );
  60. string text = "0011030306" + (Failed ? _rd.Next(0, 999).ToString("D6") : "000000");
  61. int sum = 0;
  62. foreach (var item in text)
  63. {
  64. sum += (int)item;
  65. }
  66. sum = sum % 256;
  67. OnWriteMessage(text + sum.ToString("D3") + "\r");
  68. return;
  69. }
  70. if (msg == "0010039802=?115\r")
  71. {
  72. string text = "0011039806" + _rd.Next(0, 999999).ToString("D6");
  73. int sum = 0;
  74. foreach (var item in text)
  75. {
  76. sum += (int)item;
  77. }
  78. sum = sum % 256;
  79. OnWriteMessage(text + sum.ToString("D3") + "\r");
  80. return;
  81. }
  82. if (msg == "0010034602=?108\r")
  83. {
  84. string text = "0011034606" + _rd.Next(0, 999999).ToString("D6");
  85. int sum = 0;
  86. foreach (var item in text)
  87. {
  88. sum += (int)item;
  89. }
  90. sum = sum % 256;
  91. OnWriteMessage(text + sum.ToString("D3") + "\r");
  92. return;
  93. }
  94. if (msg == "0011001006111111015\r")
  95. {
  96. lock (_locker)
  97. {
  98. if (!IsPumpOn)
  99. {
  100. IsPumpOn = true;
  101. IsAtSpeed = false;
  102. _timer.Restart();
  103. }
  104. }
  105. OnWriteMessage("0011001006111111015" + "\r");
  106. return;
  107. }
  108. if (msg == "0011001006000000009\r")
  109. {
  110. lock (_locker)
  111. {
  112. if (IsPumpOn)
  113. {
  114. IsPumpOn = false;
  115. IsAtSpeed = false;
  116. _timer.Restart();
  117. }
  118. }
  119. OnWriteMessage("0011001006000000009" + "\r");
  120. return;
  121. }
  122. if (msg == "0010001002=?096\r")
  123. {
  124. string text = "0011001006" + (IsPumpOn ? "111111" : "000000");
  125. int sum = 0;
  126. foreach (var item in text)
  127. {
  128. sum += (int)item;
  129. }
  130. sum = sum % 256;
  131. OnWriteMessage(text + sum.ToString("D3") + "\r");
  132. return;
  133. }
  134. if (msg == "0010030502=?103\r")
  135. {
  136. string text = "0011030506" + (IsOverTemp ? "111111" : "000000");
  137. int sum = 0;
  138. foreach (var item in text)
  139. {
  140. sum += (int)item;
  141. }
  142. sum = sum % 256;
  143. OnWriteMessage(text + sum.ToString("D3") + "\r");
  144. return;
  145. }
  146. if (msg == "0010030602=?104\r")
  147. {
  148. string text = "0011030606" + (IsAtSpeed ? "111111" : "000000");
  149. int sum = 0;
  150. foreach (var item in text)
  151. {
  152. sum += (int)item;
  153. }
  154. sum = sum % 256;
  155. OnWriteMessage(text + sum.ToString("D3") + "\r");
  156. return;
  157. }
  158. if (msg == "0010030702=?105\r")
  159. {
  160. string text = "0011030706" + (IsAtSpeed ? "000000" : "111111");
  161. int sum = 0;
  162. foreach (var item in text)
  163. {
  164. sum += (int)item;
  165. }
  166. sum = sum % 256;
  167. OnWriteMessage(text + sum.ToString("D3") + "\r");
  168. return;
  169. }
  170. }
  171. }
  172. }
  173. }