Receive.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 
  2. using System;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using CommandMessenger;
  6. using CommandMessenger.Transport.Serial;
  7. namespace VTExperiment1
  8. {
  9. // This is the list of recognized commands. These can be commands that can either be sent or received.
  10. // In order to receive, attach a callback function to these events
  11. //
  12. // Default commands
  13. // Note that commands work both directions:
  14. // - All commands can be sent
  15. // - Commands that have callbacks attached can be received
  16. //
  17. // This means that both sides should have an identical command list:
  18. // one side can either send it or receive it (sometimes both)
  19. // Commands
  20. enum Command
  21. {
  22. SetLed,
  23. SetMotor,// Command to request led to be set in specific state
  24. };
  25. public class Receive
  26. {
  27. public bool RunLoop { get; set; }
  28. private SerialTransport _serialTransport;
  29. private CmdMessenger _cmdMessenger;
  30. private bool _ledState;
  31. public int portID;
  32. // Setup function
  33. public void Setup()
  34. {
  35. _ledState = false;
  36. // Create Serial Port object
  37. _serialTransport = new SerialTransport();
  38. _serialTransport.CurrentSerialSettings.PortName = "COM" + portID; // Set com port
  39. _serialTransport.CurrentSerialSettings.BaudRate = 115200; // Set baud rate
  40. _serialTransport.CurrentSerialSettings.DtrEnable = false; // For some boards (e.g. Sparkfun Pro Micro) DtrEnable may need to be true.
  41. // Initialize the command messenger with the Serial Port transport layer
  42. _cmdMessenger = new CmdMessenger(_serialTransport, BoardType.Bit16);
  43. // Attach the callbacks to the Command Messenger
  44. AttachCommandCallBacks();
  45. // Start listening
  46. _cmdMessenger.Connect();
  47. }
  48. // Loop function
  49. public void Loop()
  50. {
  51. // Create command
  52. var command = new SendCommand((int)Command.SetLed, _ledState);
  53. // Send command
  54. _cmdMessenger.SendCommand(command);
  55. Console.Write("Turning led ");
  56. Console.WriteLine(_ledState ? "on" : "off");
  57. // Wait for 1 second and repeat
  58. Thread.Sleep(100);
  59. _ledState = !_ledState; // Toggle led state
  60. }
  61. public void toggleLed(bool b)
  62. {
  63. var command = new SendCommand((int)Command.SetLed, b);
  64. _cmdMessenger.SendCommand(command);
  65. Console.Write("Turning led ");
  66. Console.WriteLine(b ? "on" : "off");
  67. }
  68. public void setMotor(int number, int intensity)
  69. {
  70. var command = new SendCommand((int)Command.SetMotor, number +"");
  71. command.AddArgument(intensity);
  72. _cmdMessenger.SendCommand(command);
  73. }
  74. public void setMotor(int number, int intensity, int duration)
  75. {
  76. var command = new SendCommand((int)Command.SetMotor, number + "");
  77. command.AddArgument(intensity);
  78. command.AddArgument(duration);
  79. _cmdMessenger.SendCommand(command);
  80. }
  81. // Exit function
  82. public void Exit()
  83. {
  84. // We will never exit the application
  85. }
  86. /// Attach command call backs.
  87. private void AttachCommandCallBacks()
  88. {
  89. // No callbacks are currently needed
  90. }
  91. }
  92. }