Serial read - SerialSend.exe

Hi, I made the following arduino code to ‘read’ the serial commands and execute actions with it. In this example, the code should enable and disable a LED by sending the commands “on” or “off” via Serial.

Now, when using the Arduino IDE and inbuilt Serial monitor, the LED goes on (and stays on) when the ‘on’ command is being sent, exactly how we wanted it. Also the off functionality works as expected.

However, when running this command through CMD with the program called 'SerialSend.exe': c:\serialsend.exe /baudrate 9600 /devnum 14 /closedelay 500 “on” The behaviour is nót as expected: I can see the Arduino receive the command (looking at the RX light), but the LED on pin 13 doesn’t turn on. Or actually, it seems to turn on for a minimalistic amount of time and then turn off again immediately. Due to this I expect some troubles due to the COM port being closed. I’ve already tried delaying it up to multiple thousands of milliseconds (/closedelay 5000), but the LED-on-time remains the same minimalistic little bit, while it should just stay on forever untill we tell it to turn off.

Does any of you have any idea what could go wrong using the SerialSend.exe program?

Thanks in advance!

See the Arduino code below (credits to: https://github.com/basilfx/Arduino-CommandLine ):

// NOTE: Read the README.md file first!

#include “CommandLine.h”

// Keep track of number of bytes processed.
int count = 0;

int ledPin = 13;

// CommandLine instance.
CommandLine commandLine(Serial, “> “);

/**
* Setup serial port and add commands.
*/
void setup()
{

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Attach pre and post command handlers.
commandLine.attachPre(handlePre);
commandLine.attachPost(handlePost);

// Add commands.
commandLine.add(“count”, handleCount);
commandLine.add(“help”, handleHelp);
commandLine.add(“on”, LEDon);
commandLine.add(“off”, LEDoff);

Serial.println(“Ready2Go”);
}

void LEDon(){
Serial.println(“Test, LED should go on now”);
digitalWrite(ledPin, HIGH);
}

void LEDoff(){
Serial.println(“Test, LED should go off now”);
digitalWrite(ledPin, LOW);
}

/**
* Read-eval-print-loop.
*/
void loop()
{
commandLine.update();
}

/**
* Handle the count command. The command has one additional argument that can be the integer to set the count to.
*
* @param tokens The rest of the input command.
*/
void handleCount(char* tokens)
{
Serial.print(“Number of input bytes processed: “);
Serial.println(count);
}

/**
* Handle pre-command callback.
*
* @param tokens The the input command.
*/
void handlePre(char* tokens)
{
count = count + strlen(tokens);
}

/**
* Remove the count command.
*
* @param tokens The the input command.
* @param success True if some command was executed.
*/
void handlePost(char* tokens, bool success)
{
if (!success)
{
Serial.println(“Unknown command. Type ‘help’ for help.”);
}
}

/**
* Print some help.
*
* @param tokens The rest of the input command.
*/
void handleHelp(char* tokens)
{
Serial.println(“Use the commands ‘help’ or ‘count’.”);
}

Does your PC program open the Serial port for each message and then close it?

The Arduino resets when the PC program opens the serial port so the PC program needs to open the serial port, allow time for the Arduino to reset before sending the first message and then keep the serial port open until it is finished with the PC.

Have a look at how it is done in this Simple Python - Arduino demo

…R

Hi Robin,

Thanks for your reply.
I’ve already disabled the auto-reset using a capacitor. The LED’s on the arduino tell me it’s not resetting anymore.
However, the current behaviour is like this:

When using Arduino IDE Serial Monitor:
Everything works as expected, strings send and are read, LED13 behaves according to the commands.

When using SerialSend.exe:
Rx light tells me that communication exists, but no further action on LED13 is seen. Kinda behavious like when a ‘unknown’ command would be send. It does receive, but doesn’t seem to receive the ‘correct’ command.

Might this have something to do with the NL/CR/CF characters not being set/used correctly by the SerialSend program?

Thanks in advance!

I can't think of anything else to suggest. I have no knowledge of the CommandLine library or of SerialSend.exe.

...R

Where did you download SerialSend.exe from ?

SerialSend.exe was downloaded from: https://batchloaf.wordpress.com/serialsend/

It's a small windows application which can send a Serial message from CMD and/or .bat file. For example by executing the command:

SerialSend.exe /baudrate 9600 /devnum 10 "Hello world!"

It will send "Hello world!" to the device on COM10 with a baudrate of 9600.

Extra information is also available via the link above.

Anyone with any clue about the NL/CR/CF (New line, Carriage Return, Carriage Feed) setup/usage?

Thanks in advance!

Anyone with any clue about the NL/CR/CF (New line, Carriage Return, Carriage Feed) setup/usage?

I have downloaded and had a play with SerialSend.exe

Using it with a very simple sketch like this

#include <SoftwareSerial.h>

SoftwareSerial PC(10, 9);

void setup()
{
  Serial.begin(115200);
  PC.begin(38400);
  Serial.println("starting up");
}

void loop()
{
  if (PC.available())
  {
    Serial.write(PC.read());
  }
}

it works as expected

Using

serialsend  "\n123\n\n\nABC" /devnum 6 /hex

results in the Arduino printing

123


ABC

Note the fact that the “starting up” message does not appear and the fact that there is a newline before the 123 and 3 between the 123 and the ABC as expected

My conclusion is that the Arduino does not reboot on receipt of data from SerialSend, at least not using SoftwareSerial, and that newlines work when the /hex parameter is used in the SendSerial command line. If you have disabled the Arduino reboot then it should, I believe, behave in the same way as using SoftwareSerial

Hmm, that’s good news, thanks for the effort!
I’ll give this a try when I’m @home again!

How would you suggest handling the received string(s) to decide if a LED would go on or off?

Would a simple ‘if-statement’ or even better, ‘switch-case’ work with this?

Something along the lines of:

if(pc.read == "LED1ON"){
digitalWrite(LED1,HIGH);
}

or even better:

switch (PC.read) {
  case "LED1ON":
    digitalWrite(LED1,HIGH);
    break;

  case "LED1OFF":
    digitalWrite(LED1,LOW);
    break;

  case "LED2ON":
    digitalWrite(LED2,HIGH);
    break;

  case "LED2OFF":
    digitalWrite(LED2,LOW);
    break;

}

In the end I will use this Arduino and SerialSend.exe in combination with a software called ‘dslrBooth’. This software has some kind of API, which sends out strings through for example a .bat file with serialsend.exe commands in it.
If interested, one can find more info here: https://support.dslrbooth.com/hc/en-us/articles/360000637854-Triggers-API

Thanks in advance for all your kind help!

A small example for you

#include <SoftwareSerial.h>

SoftwareSerial PC(10, 9);

const byte ledPin = 13;

void setup()
{
  Serial.begin(115200);
  PC.begin(38400);
  pinMode(ledPin, OUTPUT);
  Serial.println("starting up");
}

void loop()
{
  if (PC.available())
  {
    static byte ledState = LOW;
    char inChar = PC.read();
    if (inChar == 'a')
    {
      ledState = HIGH;
    }
    else if (inChar == 'b')
    {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

Use with either

SerialSend /devnum 6 "a"

or

SerialSend /devnum 6 "b"

Change the port number to suit your system

I have done some more tests, this time using the hardware serial port of a Nano being fed data from SerialSend. At its default baud rate of 34800 the results are variable and the Nano often misses characters or does not receive them. Reducing the baud rate to 9600 seems to cure the problem

UKHeliBob:
A small example for you

#include <SoftwareSerial.h>

SoftwareSerial PC(10, 9);

const byte ledPin = 13;

void setup()
{
  Serial.begin(115200);
  PC.begin(38400);
  pinMode(ledPin, OUTPUT);
  Serial.println(“starting up”);
}

void loop()
{
  if (PC.available())
  {
    static byte ledState = LOW;
    char inChar = PC.read();
    if (inChar == ‘a’)
    {
      ledState = HIGH;
    }
    else if (inChar == ‘b’)
    {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}




Use with either


SerialSend /devnum 6 “a”



or


SerialSend /devnum 6 “b”




Change the port number to suit your system

Hmm, Would this also work with a string/command consisting out of a full word instead of a single character?
This is relevant as that’s the way dslrBooth outputs it’s api, for instance:
“session_start”
or
“countdown”

Would this also work with a string/command consisting out of a full word instead of a single character?

It would work if you changed the part of the program that received the Serial data so that the individual characters received are put into an array and correctly terminated to turn it into a C style string (lowercase s) and you would also need to change the way that the comparison is done to use the strcmp() function that allows you to compare strings.

Have a look at Serial input basics - updated for details