The primary purpose of this Arduino project is to guide other developers on how to use Arduino for testers by manipulating the state of specific pins on the Arduino board that are connected to the hardware component. This README also includes instructions for setting up and using the ArduinoHandler
C# class on the PC side to communicate with the Arduino.
- Arduino IDE (version 1.8.15)
- CommandHandler library
- Hardware component
- USB-B cable
- .NET Framework or .NET Core
- C# development environment (e.g., Visual Studio)
- Download and install the Arduino IDE version 1.8.15.
- Open the Arduino IDE after installation.
- Go to
Sketch
>Include Library
>Add ZIP Library...
in the Arduino IDE. - Search for and select the
CommandHandler
library. - Access it again via
Sketch
>Include Library
and click on it.
- Connect your Arduino board to your computer using a USB cable.
- Open the Arduino IDE.
- Select your Arduino board type and port under
Tools
>Board
andTools
>Port
. - Open your project sketch file (.ino) in the Arduino IDE.
- Compile the sketch by clicking on the
Verify
button (checkmark icon). - Once compiled successfully, upload the sketch to your Arduino board by clicking on the
Upload
button (right arrow icon).
Commands sent to the Arduino have arguments separated by commas (,
) and end with a dollar sign ($
). This structure is essential for the Arduino to parse and execute the commands correctly.
To add new commands, use the addCommand
function of the CommandHandler
library. This function takes the command name and a pointer to the function that executes the command.
Syntax:
cmdHdl.addCommand("<CommandName>", <pointerToFunction>);
To read command's arguments, Use the following example:
void exampleWithParams() {
double exampleNumber;
exampleNumber = cmdHdl.readDoubleArg(); // You need to do it for every argument
if(cmdHdl.argOk) {
// Do stuff with it..
}
else {
// No remaining parameters (In this specific example, it also means parmeterless command)
}
}
After executing a command, use sendSuccess()
to indicate success and sendFail()
to indicate failure. These functions communicate the operation status back to the PC.
Example:
void myCustomCommand() {
// Command implementation
// On success
sendSuccess();
// On failure
sendFail();
}
void setup() {
// Adding the custom command
cmdHdl.addCommand("MyCommand", myCustomCommand);
// ...other setup code...
}
void loop() {
cmdHdl.processSerial(Serial);
}
- Include the Class: Ensure the
ArduinoHandler.cs
file is part of your C# project. - Instantiate ArduinoHandler: Replace
"COM3"
with the COM port your Arduino is connected to.
ArduinoHandler arduino = new ArduinoHandler("COM3");
- Open Serial Connection:
arduino.Open();
- Without Parameters:
arduino.SendCommand(ArduinoCommand.EXAMPLECOMMAND);
- With Parameters:
arduino.SendCommand(ArduinoCommand.EXAMPLEWITHPARAMS, "parameter1", "parameter2");
string message = arduino.ReadMessage();
Console.WriteLine(message);
string response = arduino.Query(ArduinoCommand.EXAMPLEPINREAD, "parameter1");
Console.WriteLine(response);
arduino.Close();
arduino.Open();
arduino.SendCommand(ArduinoCommand.TOGGLELED);
arduino.Close();
arduino.Open();
string sensorValue = arduino.Query(ArduinoCommand.READSENSORVALUE);
Console.WriteLine($"Sensor Value: {sensorValue}");
arduino.Close();
The integration of the ArduinoHandler
class into your C# projects to communicate with the Arduino creates abstraction using simple software commands.