AppGlobal.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace Fietsclient
  8. {
  9. public class AppGlobal
  10. {
  11. private static AppGlobal _instance;
  12. Thread workerThread;
  13. public static AppGlobal Instance
  14. {
  15. get { return _instance ?? (_instance = new AppGlobal()); }
  16. }
  17. private KettlerBikeComm _bikeComm;
  18. private AppGlobal()
  19. {
  20. _bikeComm = new KettlerBikeComm();
  21. KettlerBikeComm.IncomingDataEvent += HandleBikeData; //initialize event
  22. }
  23. public void startComPort()
  24. {
  25. startComPort("COM3");
  26. }
  27. public void startComPort(string portname)
  28. {
  29. Console.WriteLine(portname);
  30. _bikeComm.initComm(portname);
  31. }
  32. public void startAskingData()
  33. {
  34. workerThread = new Thread(() => workerThreadLoop());
  35. workerThread.Start();
  36. }
  37. private void workerThreadLoop()
  38. {
  39. while(true)
  40. {
  41. Thread.Sleep(1000);
  42. _bikeComm.sendData(KettlerBikeComm.STATUS);
  43. }
  44. }
  45. //event handler
  46. private void HandleBikeData(string[] data)
  47. {
  48. //doe iets ermee...
  49. }
  50. public void closeComPort()
  51. {
  52. if (workerThread != null)
  53. workerThread.Suspend();
  54. _bikeComm.closeComm();
  55. }
  56. }
  57. }