FietsSimulator.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8. using System.Timers;
  9. using System.Diagnostics;
  10. namespace FietsSimulator
  11. {
  12. class FietsSimulator
  13. {
  14. private SerialPort comport;
  15. private Mode curmode;
  16. private int _power, _heartbeat, rpm, speed, distance, energy;
  17. private long maxtime;
  18. private Stopwatch stopwatch;
  19. private Random r = new Random();
  20. public int Power
  21. {
  22. get { return _power; }
  23. set
  24. {
  25. if (value >= 25 && value <= 400)
  26. _power = value;
  27. if (value < 25)
  28. _power = 25;
  29. if (value > 400)
  30. _power = 400;
  31. }
  32. }
  33. public int Heartbeat
  34. {
  35. get { return r.Next(60, 160); }
  36. }
  37. private enum Mode
  38. {
  39. NONE,
  40. CONSOLE,
  41. DISTANCE,
  42. TIME
  43. }
  44. public FietsSimulator(String addr)
  45. {
  46. this.comport = new SerialPort(addr, 9600);
  47. comport.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
  48. comport.Open();
  49. stopwatch= new Stopwatch();
  50. stopwatch.Stop();
  51. System.Timers.Timer aTimer = new System.Timers.Timer();
  52. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  53. aTimer.Interval = 1000;
  54. aTimer.Enabled = true;
  55. }
  56. private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
  57. {
  58. string message = comport.ReadLine().Trim();
  59. Console.WriteLine(message);
  60. if (message == "RS")
  61. {
  62. curmode = Mode.NONE;
  63. rpm = speed = distance = energy = 0;
  64. Power = 25;
  65. stopwatch.Stop();
  66. SendData("ACK");
  67. }
  68. else if (message == "CU")
  69. {
  70. curmode = Mode.CONSOLE;
  71. SendData("ACK");
  72. }else if (message.Contains("PD"))
  73. {
  74. if (curmode == Mode.CONSOLE && message.Split().Length == 2)
  75. {
  76. distance = Int32.Parse(message.Split(' ')[1]);
  77. curmode = Mode.DISTANCE;
  78. stopwatch.Reset();
  79. stopwatch.Start();
  80. rpm = 100;
  81. speed = 10;
  82. }
  83. else
  84. {
  85. SendData("ERROR");
  86. }
  87. }
  88. else if (message.Contains("PT"))
  89. {
  90. if (curmode == Mode.CONSOLE && message.Split().Length == 2)
  91. {
  92. string[] time = message.Split(' ')[1].Split(':');
  93. maxtime = Int32.Parse(time[0]) * 60000 + Int32.Parse(time[1]) * 1000;
  94. curmode = Mode.TIME;
  95. stopwatch.Reset();
  96. stopwatch.Start();
  97. rpm = 100;
  98. speed = 10;
  99. }
  100. else
  101. {
  102. SendData("ERROR");
  103. }
  104. }
  105. else if (message.Contains("PW"))
  106. {
  107. if (curmode != Mode.NONE && message.Split().Length == 2)
  108. {
  109. this.Power = Int32.Parse(message.Split(' ')[1]);
  110. }
  111. else
  112. {
  113. SendData("ERROR");
  114. }
  115. }
  116. else if (message == "ST" && curmode != Mode.NONE)
  117. {
  118. SendStatus();
  119. }
  120. else
  121. {
  122. SendData("ERROR");
  123. }
  124. }
  125. private void SendData(string message)
  126. {
  127. Console.WriteLine("RETURN:" + message);
  128. this.comport.WriteLine(message);
  129. }
  130. private void SendStatus()
  131. {
  132. SendData(Heartbeat.ToString() + "\t"+rpm +"\t"+speed*10+"\t"+distance+"\t" + Power.ToString() + "\t600\t"+getTimeElapsed()+"\t200\r");
  133. }
  134. private string getTimeElapsed()
  135. {
  136. if(curmode == Mode.DISTANCE)
  137. {
  138. long seconds = (stopwatch.ElapsedMilliseconds / 1000) % 60;
  139. long minutes = ((stopwatch.ElapsedMilliseconds - seconds) / 1000) / 60;
  140. return minutes + ":" + seconds;
  141. }else if(curmode == Mode.TIME)
  142. {
  143. long seconds = ((maxtime - stopwatch.ElapsedMilliseconds) / 1000) % 60;
  144. long minutes = (((maxtime -stopwatch.ElapsedMilliseconds) - seconds) / 1000) / 60;
  145. return minutes + ":" + seconds;
  146. }
  147. else
  148. {
  149. return "00:00";
  150. }
  151. }
  152. private void OnTimedEvent(object source, ElapsedEventArgs e)
  153. {
  154. if(curmode == Mode.DISTANCE)
  155. {
  156. distance -= 1;
  157. }
  158. else if (curmode == Mode.TIME)
  159. {
  160. distance += 1;
  161. }
  162. }
  163. }
  164. }