BreakReceiver.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using UnityEngine;
  2. using System;
  3. using System.IO.Ports;
  4. using System.Threading;
  5. namespace Sensors.USB
  6. {
  7. [Serializable]
  8. public struct USBSensorConfig
  9. {
  10. public String serialPort;
  11. public Int32 baudRate;
  12. public Parity parity;
  13. public StopBits stopBits;
  14. public Int32 dataBits;
  15. public Handshake handshake;
  16. public Boolean rtsEnable;
  17. public Boolean debug;
  18. public USBSensorConfig(
  19. String port = "COM1",
  20. Int32 baudRate = 9600,
  21. Parity parity = Parity.None,
  22. StopBits stopBits = StopBits.None,
  23. Int32 dataBits = 8,
  24. Handshake handshake = Handshake.None,
  25. Boolean rtsEnable = true,
  26. Boolean debug = false
  27. ){
  28. this.serialPort = port;
  29. this.baudRate = baudRate;
  30. this.parity = parity;
  31. this.stopBits = stopBits;
  32. this.dataBits = dataBits;
  33. this.handshake = handshake;
  34. this.rtsEnable = rtsEnable;
  35. this.debug = debug;
  36. }
  37. }
  38. public struct USBSensorData
  39. {
  40. public bool isBreaking;
  41. public float timeActive;
  42. }
  43. public class USBReceiver
  44. {
  45. private USBSensorConfig config;
  46. private SerialPort sp = null;
  47. private USBSensorData sensorData;
  48. private Thread serialThread;
  49. private Boolean looping;
  50. public USBSensorData SensorData => sensorData;
  51. public USBReceiver(USBSensorConfig config){
  52. this.config = config;
  53. }
  54. public void StartListening()
  55. {
  56. /*String[] ports = SerialPort.GetPortNames();
  57. foreach(String port in ports){
  58. Debug.Log(port);
  59. }*/
  60. this.sp = new SerialPort(
  61. this.config.serialPort,
  62. this.config.baudRate,
  63. this.config.parity,
  64. this.config.dataBits,
  65. this.config.stopBits
  66. );
  67. /*this.sp.Handshake = this.config.handshake;
  68. this.sp.RtsEnable = this.config.rtsEnable;*/
  69. this.sp.Open();
  70. this.looping = true;
  71. this.serialThread = new Thread(CheckSerialPort);
  72. serialThread.Start();
  73. Debug.Log("Started Listening for Serial Port " + this.config.serialPort);
  74. }
  75. public void StopListening(){
  76. if(this.serialThread != null){
  77. Debug.Log("Stopping Thread!");
  78. this.looping = false;
  79. this.serialThread.Join();
  80. this.serialThread.Abort();
  81. }
  82. if(this.sp != null){
  83. this.sp.Close();
  84. }
  85. }
  86. public void Dispose()
  87. {
  88. Debug.Log("Dispose Serial Port Listening");
  89. StopListening();
  90. }
  91. private void CheckSerialPort(){
  92. while(this.looping){
  93. if(this.sp != null){
  94. String content = sp.ReadLine();
  95. if(content.Contains("BS=")){
  96. String status = content.Split('=')[1];
  97. Int32 statCode = Int32.Parse(status);
  98. if(statCode == 1){
  99. Debug.Log("Breaking");
  100. this.sensorData.isBreaking = true;
  101. }else if(statCode == 0){
  102. this.sensorData.isBreaking = false;
  103. }
  104. }
  105. }else{
  106. Debug.Log("found no Serial Port!");
  107. }
  108. Thread.Sleep(500);
  109. }
  110. }
  111. }
  112. }