ActuBoardInterface.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ActuBoardAPI
  8. {
  9. public class ActuBoardInterface : IActuBoard
  10. {
  11. public event Action<bool> OnConnectionChanged;
  12. public event Action<String> OnMessageReceived;
  13. // status event
  14. public event Action<StatusCode> OnStatusUpdate;
  15. private char EOL = '\r';
  16. private int _baud = 115200;
  17. private int _com = -1;
  18. private SerialPort _io;
  19. /**
  20. * connect to ActuBoard
  21. */
  22. public int Connect()
  23. {
  24. // COM Port not set
  25. if (_com <= 0)
  26. {
  27. OnStatusUpdate?.Invoke(StatusCode.ComPortInvalid);
  28. return -1;
  29. }
  30. // start SerialPort connection
  31. Console.WriteLine("Connecting to port " + _com);
  32. _io = new SerialPort("COM" + _com)
  33. {
  34. BaudRate = _baud
  35. };
  36. OnStatusUpdate?.Invoke(StatusCode.ActuBoardIsConnecting);
  37. _io.Open();
  38. // if could not open SerialPort set error
  39. if (!_io.IsOpen)
  40. {
  41. //OnStatusUpdate(this, StatusCode.ActuBoardNotConnected);
  42. OnStatusUpdate?.Invoke(StatusCode.ActuBoardNotConnected);
  43. return -2;
  44. }
  45. OnConnectionChanged?.Invoke(true);
  46. //OnConnectionChanged?.Invoke(true); // default .NET
  47. OnStatusUpdate?.Invoke(StatusCode.ActuBoardConnected); // Unity .NET
  48. Console.WriteLine("Connected!");
  49. _io.DataReceived += _io_DataReceived;
  50. return 1;
  51. }
  52. private void _io_DataReceived(object sender, SerialDataReceivedEventArgs e)
  53. {
  54. int count = _io.BytesToRead;
  55. byte[] msg = new byte[count];
  56. _io.Read(msg, 0, count);
  57. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  58. String smsg = enc.GetString(msg);
  59. if (smsg.Contains('\a'))
  60. OnStatusUpdate?.Invoke(StatusCode.HardwareCommandError);
  61. else
  62. OnStatusUpdate?.Invoke(StatusCode.NoError);
  63. OnMessageReceived?.Invoke(smsg);
  64. }
  65. public int Disconnect()
  66. {
  67. if (_io == null)
  68. return -1;
  69. if (!_io.IsOpen)
  70. return -2;
  71. _io.Close();
  72. OnConnectionChanged?.Invoke(false);
  73. return 1;
  74. }
  75. public bool IsConnected()
  76. {
  77. if (_io != null)
  78. return _io.IsOpen;
  79. else
  80. return false;
  81. }
  82. /**
  83. * enable or disable output of _every_ ActuBoard Hub at once
  84. * (e.g., useful as emergency switch)
  85. */
  86. public void EnableOutput(bool enable)
  87. {
  88. if (enable)
  89. WriteCommand("O01" + EOL);
  90. else
  91. WriteCommand("O00" + EOL);
  92. }
  93. public int EnableBlinking(int cid)
  94. {
  95. WriteCommand("G" + Helper.IntToHex(cid) + "01" + EOL);
  96. return 0;
  97. }
  98. public int DisableBlinking(int cid)
  99. {
  100. WriteCommand("G" + Helper.IntToHex(cid) + "00" + EOL);
  101. return 0;
  102. }
  103. public int EnableBlinking(int[] cid)
  104. {
  105. // currently not needed, implemented soon
  106. throw new NotImplementedException();
  107. }
  108. public int DisableBlinking(int[] cid)
  109. {
  110. // currently not needed, implemented soon
  111. throw new NotImplementedException();
  112. }
  113. public void SendCustomCommand(string cmd)
  114. {
  115. WriteCommand(cmd + EOL);
  116. }
  117. public int SetChannel(int cid, int pwm)
  118. {
  119. SetChannel(Helper.IntToHex(cid), Helper.IntToHex(pwm));
  120. return 0;
  121. }
  122. public int SetChannel(string cid, string pwm)
  123. {
  124. WriteCommand("S" + cid + pwm + EOL);
  125. return 0;
  126. }
  127. public int GetChannel(int cid)
  128. {
  129. WriteCommand("R" + cid.ToString("X2") + EOL);
  130. return 0;
  131. }
  132. public int ListDevices()
  133. {
  134. WriteCommand("L" + EOL);
  135. return 0;
  136. }
  137. public int SetBlinkingCycle(int cycle)
  138. {
  139. WriteCommand("i" + Helper.IntToHex(cycle) + EOL);
  140. return 0;
  141. }
  142. public int SetBlinkingInterval(int interval)
  143. {
  144. WriteCommand("I" + Helper.IntToHex(interval) + EOL);
  145. return 0;
  146. }
  147. public int SetChannels(int[] cid, int[] pwm)
  148. {
  149. if (cid.Length > 0 && cid.Length == pwm.Length)
  150. {
  151. String channels = Helper.IntArrayToNumHexString(cid);
  152. String pwms = Helper.IntArrayToNumHexString(pwm, false);
  153. WriteCommand("n" + channels + pwms + EOL);
  154. return 0;
  155. }
  156. return -1;
  157. }
  158. public int SetChannels(int[] cid, int pwm)
  159. {
  160. if (cid.Length > 0)
  161. {
  162. String channels = Helper.IntArrayToNumHexString(cid);
  163. WriteCommand("m" + channels + Helper.IntToHex(pwm) + EOL);
  164. return 0;
  165. }
  166. return -1;
  167. }
  168. public void ToggleDebug()
  169. {
  170. WriteCommand("D" + EOL);
  171. }
  172. public int SetComPort(int comport)
  173. {
  174. if (comport > 0)
  175. _com = comport;
  176. else
  177. return -1;
  178. return _com;
  179. }
  180. private void WriteCommand(String command)
  181. {
  182. if (_io != null && _io.IsOpen)
  183. {
  184. Console.WriteLine("CMD: " + command);
  185. _io.Write(command);
  186. }
  187. else
  188. {
  189. Console.WriteLine("Could not send command (IO is null or closed): " + command);
  190. }
  191. }
  192. }
  193. }