Program.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace FietsSimulator
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. }
  14. }
  15. class FietsSimulator
  16. {
  17. private SerialPort comport;
  18. private Mode curmode;
  19. private int curvalue;
  20. private int _power;
  21. public int Power
  22. {
  23. get { return _power; }
  24. set {
  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. private enum Mode
  34. {
  35. NONE,
  36. CONSOLE,
  37. DISTANCE,
  38. TIME
  39. }
  40. public FietsSimulator(String addr)
  41. {
  42. this.comport = new SerialPort(addr, 9600);
  43. comport.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
  44. comport.Open();
  45. }
  46. private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
  47. {
  48. String message = comport.ReadLine().Trim();
  49. if (message == "RS")
  50. {
  51. curmode = Mode.NONE;
  52. curvalue = 0;
  53. Power = 25;
  54. SendData("ACK");
  55. }
  56. else if (message == "CU")
  57. {
  58. curmode = Mode.CONSOLE;
  59. SendData("ACK");
  60. }
  61. else if (message.Contains("PW"))
  62. {
  63. if(curmode != Mode.NONE && message.Split().Length == 2)
  64. {
  65. this.Power = Int32.Parse(message.Split(' ')[1]);
  66. }
  67. else
  68. {
  69. SendData("ERROR");
  70. }
  71. }
  72. else
  73. {
  74. SendData("ERROR");
  75. }
  76. }
  77. private void SendData(String message)
  78. {
  79. this.comport.WriteLine(message);
  80. }
  81. }
  82. }