I have the rather simple need to send "text commands" to an Arduino board from a C# program. For instance I want to send the command "DimLight10to50%" which will trigger Arduino to do PWM dimming for LED #10 (I will have to simulate PWM as there aren't enough PWM pins). That's really all I need including getting some acknowledgment. An added benefit would be to control pins directly as this could be beneficial for some other applications but then again I can easily implement this with my own text commands/translations.
I did some research and the most promising candidate seems to be CmdMessenger. However, I just wanted to get some input from the forum if there is a better alternative or something I am missing for my application. FYI, I am using an Arduino Nano.
I don't know C# but the examples in Serial Input Basics should be appropriate on the Arduino side. The technique in the 3rd example will be the most reliable.
You will save yourself a lot of trouble if you just send simple commands. For example to dim a light you could send <10, 128> to tell the Uno to set the PWM value on pin 10 to 128 (which is 50%).
On the C# side, keep in mind that the user interface runs in one thread and serial I/O happens in another thread, so the thread that gets any response can not directly write to the GUI.
Well, I downloaded CmdMessenger and compiled one of the simple example programs. It uses 46% of the dynamic memory of my Nano. I guess I just have to write my own
I used example 5 as it's based on example 3 and allows me to send more commands (I didn't look at example 3 but it's probably very similar). The memory footprint is MUCH smaller compared to CmdMessenger, especially after removing all the logging. I am sure CmdMessenger has it's purposes, is much more flexible, removes some of the parsing tasks, etc. However, for my simple use example 5 does everything I need. Thanks!!
Just use a simple SerialPort object on the C# side. You can write to or read from it pretty simple. On the Arduino, just use the built-in serial interface as well and read the commands from the buffer. It's pretty simple and straight-forward. And it works without any external library or software.
Keep in mind, that C# will use \r\n as newline characters, so you have to catch both of them. One way is to simply ignore the \r altogether on the Arduino side and react to the \n only.