DataHandler.cs 5.9 KB

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