Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria


Introduction 

In this post it will be shown how to propperly work with the Command Line library. 

The library consists on adding a command line utility to the Arduino.

You can type customizable commands to the Arduino board through a Serial port to execute functions. For example, you can add a command “set-ip 192.168.1.10” to set the PLC IP address. Of course, you MUST implement the function which sets the IP address.


Requirements

Command Line library:        CmdLine library - Github Repository

Ethernet or 20 I/Os PLC:      Ethernet PLC             20 I/Os PLC     

Industrial Shields boards:   Industrial Shields Boards


Usage

When you open the Serial Monitor it appears a ‘>’ symbol which indicates that a command can be typed.

When you type ‘help’ (and press Enter), the printHelpCmd() function is executed and the list of available commands is printed.

When you type ‘setOutput HIGH’ or ‘setOutput LOW’, the setOutputCmd() function is executed. It uses the argument to set the output HIGH or LOW. If the argument is not ‘HIGH’ nor ‘LOW’, the setOutputCmd() function does nothing.

When you type ‘getInput’, the getInptuCmd() function is executed. It reads the I0.0 input and prints ‘I0.0 is HIGH’ or ‘I0.0 is LOW’ depending on the value of the digital input.


Example

You MUST define a set of commands with the associated functions to be executed: it is defined as an array of commands (cmd_t). In the example it is called ‘commands’. This variable MUST be used to initialize the CmdLine library, with CmdLine.begin(commands, sizeof(commands)).

The commands functions MUST accept a ‘const char *’ argument: a string representing the additional arguments passed to the command. You can use Arduino functions to treat these arguments, as IPAddress to get IP addresses, strcmp for comparing strings, atoi for converting strings to numbers, etc.

Here there is a simple example where we show this:

#include <CmdLine.h>
 
 // Create the command line and use the Serial port to introduce commands
 CmdLine cmdline(Serial);
 
 // Define the commands and associate them a function. The function is called when the
 // command is typed.
 // i.e. The "help" command calls the printHelpCmd function
 // IMPORTANT: functions MUST be declared before the commands definition, MUST accept a
 // 'const char *' argument and MUST return nothing (void)
 // The function argument is an optional argument typed after the command
 // i.e. > setOutput HIGH // 'arg' is "HIGH"
 void printHelpCmd(const char *arg);
 void setOutputCmd(const char *arg);
 void getInputCmd(const char *arg);
 
 const cmd_t commands[] = {
   {"help", printHelpCmd},
   {"setOutput", setOutputCmd},
   {"getInput", getInputCmd},
 };
 
 void setup() {
   // Start the Serial port to be used as a commandline
   Serial.begin(9600L);
   while (!Serial);
 
   // Start the cmdline to show the prompt and begin processing commands
   cmdline.begin(commands, sizeof(commands));
 }
 
 void loop() {
   // Update cmdline often to process commands written in the Serial port
   cmdline.update();
 }
 
 void printHelpCmd(const char *arg) {
   Serial.println("List of commands:");
   Serial.println("help                    print this help");
   Serial.println("setOutput HIGH|LOW      set Q0.0 to HIGH or LOW");
   Serial.println("getInput                get I0.0 digital value");
 }
 
 void setOutputCmd(const char *arg) {
   if (strcmp(arg, "HIGH") == 0) {
     digitalWrite(Q0_0, HIGH);
     Serial.println("Q0.0 set to HIGH");
   } else if (strcmp(arg, "LOW") == 0) {
     digitalWrite(Q0_0, LOW);
     Serial.println("Q0.0 set to LOW");
   }
 }
 
 void getInputCmd(const char *arg) {
   int value = digitalRead(I0_0);
   if (value == HIGH) {
     Serial.println("I0.0 is HIGH");
   } else {
     Serial.println("I0.0 is LOW");
   }
 }




Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send