KettlerBikeComm.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /// <summary>
  11. /// Dit is de communicatieklasse met de simulator of de fiets.
  12. /// </summary>
  13. class KettlerBikeComm
  14. {
  15. // vaste waarden
  16. public static readonly string COMMAND = "CM";
  17. public static readonly string CMD_TIME = "PT";
  18. public static readonly string CMD_DISTANCE = "PD";
  19. public static readonly string CMD_POWER = "PW";
  20. public static readonly string CMD_ENERGY = "PE";
  21. public static readonly string RESET = "RS";
  22. public static readonly string STATUS = "ST";
  23. // private fields
  24. private string _portname;
  25. private int baudrate = 9600;
  26. private string _bufferOut;
  27. private string[] _bufferIn;
  28. // public fields
  29. public enum State { notConnected, connected, reset, command }
  30. public enum ReturnData { ERROR, ACK, RUN, STATUS }
  31. public State state = State.notConnected;
  32. public ReturnData returnData;
  33. private SerialPort ComPort;
  34. // custom events
  35. public delegate void DataDelegate(string[] data);
  36. public static event DataDelegate IncomingDataEvent;
  37. public delegate void DebugDelegate(string debugData);
  38. public static event DebugDelegate IncomingDebugLineEvent;
  39. public KettlerBikeComm()
  40. {
  41. }
  42. private static void OnIncomingDataEvent(string[] data)
  43. {
  44. DataDelegate handler = IncomingDataEvent;
  45. if (handler != null) handler(data);
  46. }
  47. private static void OnIncomingDebugLineEvent(string debugData)
  48. {
  49. DebugDelegate handler = IncomingDebugLineEvent;
  50. if (handler != null) handler(debugData);
  51. }
  52. public void initComm(string portname)
  53. {
  54. if (ComPort != null)
  55. {
  56. ComPort.Close();
  57. }
  58. _portname = portname;
  59. try
  60. {
  61. ComPort = new SerialPort(_portname, this.baudrate);
  62. ComPort.Open();
  63. ComPort.WriteLine(RESET);
  64. ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
  65. }
  66. catch (UnauthorizedAccessException)
  67. {
  68. OnIncomingDebugLineEvent("ERROR: UnauthorizedAccessException throwed");
  69. }
  70. catch (InvalidOperationException)
  71. {
  72. OnIncomingDebugLineEvent("ERROR: InvalidOperationException throwed");
  73. }
  74. finally
  75. {
  76. try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
  77. }
  78. }
  79. public void closeComm()
  80. {
  81. ComPort.Close();
  82. }
  83. public void sendData(string data)
  84. {
  85. _bufferOut = data;
  86. ComPort.WriteLine(data);
  87. }
  88. private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  89. {
  90. string buffer = ComPort.ReadLine();
  91. switch(buffer) //kijk wat er binnenkomt
  92. {
  93. case "ERROR": //wanneer "Error"
  94. returnData = ReturnData.ERROR;
  95. handleError();
  96. break;
  97. case "ACK": // ACK betekent acknowledged.
  98. returnData = ReturnData.ACK;
  99. break;
  100. case "RUN":
  101. returnData = ReturnData.RUN;
  102. break;
  103. default: // alle andere waarden.
  104. returnData = ReturnData.STATUS;
  105. handleBikeValues(buffer);
  106. break;
  107. }
  108. }
  109. int trycount = 0;
  110. private void handleError()
  111. {
  112. if (_bufferOut == "RS" && trycount < 3)
  113. {
  114. sendData("RS"); //gewoon nog een keer proberen tot 3 keer toe, net zolang totdat hij werkt.
  115. trycount++;
  116. }
  117. }
  118. private void handleBikeValues(string buffer)
  119. {
  120. buffer = buffer.TrimEnd('\r');
  121. Console.WriteLine(buffer);
  122. _bufferIn = buffer.Split('\t');
  123. OnIncomingDataEvent(_bufferIn);
  124. }
  125. private bool checkBikeState()
  126. {
  127. bool success = false;
  128. switch(state)
  129. {
  130. case State.reset:
  131. setCommandMode();
  132. if(returnData != ReturnData.ERROR)
  133. {
  134. success = true;
  135. }
  136. break;
  137. case State.connected:
  138. setCommandMode();
  139. success = true;
  140. break;
  141. case State.command:
  142. success = true;
  143. break;
  144. case State.notConnected:
  145. Console.WriteLine("ERROR: not connected to bike.");
  146. success = false;
  147. break;
  148. }
  149. return success;
  150. }
  151. public void setCommandMode()
  152. {
  153. sendData(COMMAND);
  154. }
  155. public void setTime()
  156. {
  157. if (!checkBikeState())
  158. return;
  159. sendData(CMD_TIME);
  160. }
  161. public void setDistance()
  162. {
  163. if (!checkBikeState())
  164. return;
  165. sendData(CMD_DISTANCE);
  166. }
  167. public void setPower()
  168. {
  169. if (!checkBikeState())
  170. return;
  171. sendData(CMD_POWER);
  172. }
  173. public void setEnergy()
  174. {
  175. if (!checkBikeState())
  176. return;
  177. sendData(CMD_ENERGY);
  178. }
  179. }
  180. }