Receive.cs 3.3 KB

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