BreakReceiver.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 GameObject debugSignaler;
  19. public USBSensorConfig(
  20. String port = "COM1",
  21. Int32 baudRate = 9600,
  22. Parity parity = Parity.None,
  23. StopBits stopBits = StopBits.None,
  24. Int32 dataBits = 8,
  25. Handshake handshake = Handshake.None,
  26. Boolean rtsEnable = true,
  27. Boolean debug = false,
  28. GameObject debugSignaler = null
  29. ){
  30. this.serialPort = port;
  31. this.baudRate = baudRate;
  32. this.parity = parity;
  33. this.stopBits = stopBits;
  34. this.dataBits = dataBits;
  35. this.handshake = handshake;
  36. this.rtsEnable = rtsEnable;
  37. this.debug = debug;
  38. this.debugSignaler = debugSignaler;
  39. }
  40. }
  41. public struct USBSensorData
  42. {
  43. public bool isBreaking;
  44. public float timeActive;
  45. }
  46. public class USBReceiver
  47. {
  48. private USBSensorConfig config;
  49. private SerialPort sp = null;
  50. private USBSensorData sensorData;
  51. private Thread serialThread;
  52. private Boolean looping;
  53. private Renderer debugRenderer;
  54. public USBSensorData SensorData => sensorData;
  55. public USBReceiver(USBSensorConfig config){
  56. this.config = config;
  57. }
  58. public void StartListening()
  59. {
  60. /*String[] ports = SerialPort.GetPortNames();
  61. foreach(String port in ports){
  62. Debug.Log(port);
  63. }*/
  64. this.sp = new SerialPort(
  65. this.config.serialPort,
  66. this.config.baudRate,
  67. this.config.parity,
  68. this.config.dataBits,
  69. this.config.stopBits
  70. );
  71. /*this.sp.Handshake = this.config.handshake;
  72. this.sp.RtsEnable = this.config.rtsEnable;*/
  73. //this.sp.DataReceived += new SerialDataReceivedEventHandler(OnDataRecveived); # Does not work in unity projects..
  74. if(this.config.debug){
  75. this.debugRenderer = this.config.debugSignaler.GetComponent<Renderer>();
  76. }
  77. this.sp.Open();
  78. this.looping = true;
  79. this.serialThread = new Thread(CheckSerialPort);
  80. serialThread.Start();
  81. Debug.Log("Started Listening for Serial Port " + this.config.serialPort);
  82. }
  83. public void StopListening(){
  84. if(this.serialThread != null){
  85. Debug.Log("Stopping Thread!");
  86. this.looping = false;
  87. this.serialThread.Join();
  88. this.serialThread.Abort();
  89. }
  90. if(this.sp != null){
  91. this.sp.Close();
  92. }
  93. }
  94. public void Dispose()
  95. {
  96. Debug.Log("Dispose Serial Port Listening");
  97. StopListening();
  98. }
  99. public void OnDataRecveived(object Sender, SerialDataReceivedEventArgs e){
  100. Debug.Log("Received Port Data");
  101. Debug.Log(e.ToString());
  102. }
  103. private void setDebugColor(Color col){
  104. if(this.debugRenderer != null){
  105. this.debugRenderer.material.color = col;
  106. }
  107. }
  108. private void CheckSerialPort(){
  109. while(this.looping){
  110. if(this.sp != null){
  111. String content = sp.ReadLine();
  112. if(content.Contains("BS=")){
  113. String status = content.Split('=')[1];
  114. Int32 statCode = Int32.Parse(status);
  115. if(statCode == 1){
  116. Debug.Log("Breaking");
  117. this.sensorData.isBreaking = true;
  118. }else if(statCode == 0){
  119. this.sensorData.isBreaking = false;
  120. }
  121. }
  122. }else{
  123. Debug.Log("found no Serial Port!");
  124. }
  125. Thread.Sleep(500);
  126. }
  127. }
  128. }
  129. }