KettlerBikeComm.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO.Ports;
  7. using System.Threading;
  8. namespace Fietsclient
  9. {
  10. class KettlerBikeComm
  11. {
  12. // vaste waarden
  13. public static readonly string COMMAND = "CM";
  14. public static readonly string CMD_TIME = "PT";
  15. public static readonly string CMD_DISTANCE = "PD";
  16. public static readonly string CMD_POWER = "PW";
  17. public static readonly string CMD_ENERGY = "PE";
  18. public static readonly string RESET = "RS";
  19. public static readonly string STATUS = "ST";
  20. // private fields
  21. private string _portname;
  22. private int baudrate = 9600;
  23. private string _bufferOut;
  24. private string[] _bufferIn;
  25. // public fields
  26. public enum State { notConnected, connected, reset, command }
  27. public enum ReturnData { ERROR, ACK, RUN, STATUS }
  28. public State state = State.notConnected;
  29. public ReturnData returnData;
  30. private SerialPort ComPort;
  31. // custom events
  32. public delegate void DataDelegate(string[] data);
  33. public static event DataDelegate IncomingDataEvent;
  34. public KettlerBikeComm()
  35. {
  36. }
  37. private static void OnIncomingDataEvent(string[] data)
  38. {
  39. DataDelegate handler = IncomingDataEvent;
  40. if (handler != null) handler(data);
  41. }
  42. public void initComm(string portname)
  43. {
  44. if (ComPort != null)
  45. {
  46. ComPort.Close();
  47. }
  48. _portname = portname;
  49. ComPort = new SerialPort(_portname, this.baudrate);
  50. ComPort.Open();
  51. Console.WriteLine("test");
  52. ComPort.WriteLine(RESET);
  53. Console.Write(ComPort.ReadLine());
  54. Console.WriteLine("end of message");
  55. ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
  56. }
  57. public void closeComm()
  58. {
  59. ComPort.Close();
  60. }
  61. public void sendData(string data)
  62. {
  63. _bufferOut = data;
  64. ComPort.WriteLine(data);
  65. }
  66. private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  67. {
  68. string buffer = ComPort.ReadLine();
  69. switch(buffer) //kijk wat er binnenkomt
  70. {
  71. case "ERROR": //wanneer "Error"
  72. //if (_bufferOut == "RS") sendData("RS"); //gewoon nog een keer proberen...
  73. returnData = ReturnData.ERROR;
  74. break;
  75. case "ACK": // ACK betekent acknowledged.
  76. returnData = ReturnData.ACK;
  77. break;
  78. case "RUN":
  79. returnData = ReturnData.RUN;
  80. break;
  81. default: // alle andere waarden.
  82. returnData = ReturnData.STATUS;
  83. handleBikeValues(buffer);
  84. break;
  85. }
  86. }
  87. private void handleBikeValues(string buffer)
  88. {
  89. buffer = buffer.TrimEnd('\r');
  90. Console.WriteLine(buffer);
  91. _bufferIn = buffer.Split('\t');
  92. OnIncomingDataEvent(_bufferIn);
  93. }
  94. private bool checkBikeState()
  95. {
  96. bool success = false;
  97. switch(state)
  98. {
  99. case State.reset:
  100. setCommandMode();
  101. if(returnData != ReturnData.ERROR)
  102. {
  103. success = true;
  104. }
  105. break;
  106. case State.connected:
  107. setCommandMode();
  108. success = true;
  109. break;
  110. case State.command:
  111. success = true;
  112. break;
  113. case State.notConnected:
  114. Console.WriteLine("ERROR: not connected to bike.");
  115. success = false;
  116. break;
  117. }
  118. return success;
  119. }
  120. public void setCommandMode()
  121. {
  122. sendData(COMMAND);
  123. }
  124. public void setTime()
  125. {
  126. if (!checkBikeState())
  127. return;
  128. sendData(CMD_TIME);
  129. }
  130. public void setDistance()
  131. {
  132. if (!checkBikeState())
  133. return;
  134. sendData(CMD_DISTANCE);
  135. }
  136. public void setPower()
  137. {
  138. if (!checkBikeState())
  139. return;
  140. sendData(CMD_POWER);
  141. }
  142. public void setEnergy()
  143. {
  144. if (!checkBikeState())
  145. return;
  146. sendData(CMD_ENERGY);
  147. }
  148. }
  149. }