DataHandler.cs 5.5 KB

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