AppGlobal.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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("COM4");
  26. }
  27. public void startComPort(string portname)
  28. {
  29. _bikeComm.initComm(portname);
  30. }
  31. public void startAskingData()
  32. {
  33. workerThread = new Thread(() => workerThreadLoop());
  34. workerThread.Start();
  35. }
  36. private void workerThreadLoop()
  37. {
  38. while(true)
  39. {
  40. Thread.Sleep(1000);
  41. _bikeComm.sendData(KettlerBikeComm.STATUS);
  42. }
  43. }
  44. //event handler
  45. private void HandleBikeData(string[] data)
  46. {
  47. //doe iets ermee...
  48. }
  49. public void closeComPort()
  50. {
  51. if (workerThread != null)
  52. workerThread.Suspend();
  53. _bikeComm.closeComm();
  54. }
  55. }
  56. }