KettlerBikeComm.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. public 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. try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
  70. }
  71. catch (InvalidOperationException)
  72. {
  73. OnIncomingDebugLineEvent("ERROR: InvalidOperationException throwed");
  74. try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
  75. }
  76. }
  77. public void closeComm()
  78. {
  79. ComPort.Close();
  80. }
  81. public void sendData(string data)
  82. {
  83. _bufferOut = data;
  84. ComPort.WriteLine(data);
  85. }
  86. private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  87. {
  88. string buffer = ComPort.ReadLine();
  89. switch(buffer) //kijk wat er binnenkomt
  90. {
  91. case "ERROR": //wanneer "Error"
  92. returnData = ReturnData.ERROR;
  93. handleError();
  94. break;
  95. case "ACK": // ACK betekent acknowledged.
  96. returnData = ReturnData.ACK;
  97. break;
  98. case "RUN":
  99. returnData = ReturnData.RUN;
  100. break;
  101. default: // alle andere waarden.
  102. returnData = ReturnData.STATUS;
  103. handleBikeValues(buffer);
  104. break;
  105. }
  106. }
  107. int trycount = 0;
  108. private void handleError()
  109. {
  110. if (_bufferOut == "RS" && trycount < 3)
  111. {
  112. sendData("RS"); //gewoon nog een keer proberen tot 3 keer toe, net zolang totdat hij werkt.
  113. trycount++;
  114. }
  115. }
  116. private void handleBikeValues(string buffer)
  117. {
  118. buffer = buffer.TrimEnd('\r');
  119. Console.WriteLine(buffer);
  120. _bufferIn = buffer.Split('\t');
  121. OnIncomingDataEvent(_bufferIn);
  122. }
  123. private bool checkBikeState()
  124. {
  125. bool success = false;
  126. switch(state)
  127. {
  128. case State.reset:
  129. setCommandMode();
  130. if(returnData != ReturnData.ERROR)
  131. {
  132. success = true;
  133. }
  134. break;
  135. case State.connected:
  136. setCommandMode();
  137. success = true;
  138. break;
  139. case State.command:
  140. success = true;
  141. break;
  142. case State.notConnected:
  143. Console.WriteLine("ERROR: not connected to bike.");
  144. success = false;
  145. break;
  146. }
  147. return success;
  148. }
  149. public void setCommandMode()
  150. {
  151. sendData(COMMAND);
  152. }
  153. public void setTime()
  154. {
  155. if (!checkBikeState())
  156. return;
  157. sendData(CMD_TIME);
  158. }
  159. public void setDistance()
  160. {
  161. if (!checkBikeState())
  162. return;
  163. sendData(CMD_DISTANCE);
  164. }
  165. public void setPower()
  166. {
  167. if (!checkBikeState())
  168. return;
  169. sendData(CMD_POWER);
  170. }
  171. public void setEnergy()
  172. {
  173. if (!checkBikeState())
  174. return;
  175. sendData(CMD_ENERGY);
  176. }
  177. }
  178. }