Nano serial com issue

Does anyone have a problem with serial communication on the arduino nano A000005 when first power on? Here is what happens.
Fail sequence:

  1. Power on Nano (plug in USB cable).
  2. Connect to Nano USB serial port (this works).
  3. Send first command I.E. :V# to get my program version (nothing comes back).
    Looks like the Nano did not see the command.
  4. Send command again (works).
  5. End program talking to Nano.
  6. Start program new to talk to Nano.
  7. Connect to Nano USB serial port.
  8. Send first command. (Now it works).

So It looks like the Nano is having a problem when first power on seeing the first thing sent to the serial port.

I have tried several Arduino Nano units and they all do the same thing.
However when I use an Arduino UNO this problem does not happen.
Same code is downloaded in all my Arduinos.

Any ideas?
William

What code would that be ?

That is correct.

I put a 2 second delay in my program that connects to the Arduino Nano and it works. 1.5 second delay does not.
So after I connect to the serial port that is connected to the Nano I wait 2 seconds before sending my 1st command and that works for the power up issue.
Had someone test my Arduino code on a Nano IoT and it works fine just like the UNO.

When the Arduino's serial port is opened, the uC is reset using the DTR signal.
After the reset, uC starts the bootloader first and checks if there is a sketch transfer, so it will take some delay before the actual user program is executed.

Perhaps that's why sending a command right after opening the serial port doesn't respond.

Yes, and if the delay is absolutely intolerable for your application, you can eliminate it by uploading using the ICSP port (the six pin connector) and a USBASP programmer or another Arduino programmed to function as one.

Another thing you can (probably should) do, is have the Arduino send a request for the first command at boot time, and have the host wait until it's recognized before sending.

Two way handshaking is always safer than blind transmissions. The situation of the host not being ready to respond is less likely, and easier to troubleshoot and fix.

A really robust system defaults to periodic re-tries if there is a failure to communicate.

It does not matter how long I wait to send the first command after power up.
I have power it on and waited several minutes before connecting and sending the first command, same thing. The only solution I have found is to put a 2 second delay in my program after I open the COM port before sending the first command to the Nano. Anything shorter than 2 seconds does not work.
Also I can press the reset button on the Nano after the devices begins working ok, resent first command after power up, and it works fine.

When are you going to post an example sketch that illustrates the problem ?

I explained that way.

The Nano is automatic reset when you open the serial port.
Of course, it doesn't matter how long it's been since you turned it on.
The Nano auto-resets every time you open the serial port.

Also, if you reset the Nano with the button while the serial port is open from the PC side, there is no operation to "open the serial port", so the Nano is doesn't reset, command has accepted.

If you don't need reset the Nano when you open the COM port, configure to your terminal app not to use the DTR signal when you open the COM port.

DTR is not enabled in my programs that are talking to the Nano.
I am disconnected from the Nano when I press the reset button.
Looks like it is a Nano issue and I will work around it.
The non response does not affect the operation of my program because it is just asking for my Arduino Nano program version that I send back.

Hi,
Can you post your code, or a representative code that demonstrates your query?
Please us code tags.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Since I am a new user I cannot upload the code.
Made a small test program. Is it ok just to put it in my reply?
Could also put it on my web site and provide a link.

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Here is the test code that shows what is happening.
I ran a serial port monitor on the usb com port that is connected to the Nano and it verifies nothing is being sent back from the Nano after my command is sent.

PC program connect to serial port and sends :V#
Nano should send back Ok#
This does work except right after the Nano has been powered on via plugging the USB cable. I can wait several minutes before starting and sending the data from my PC and it fails. Resend the :V# command without disconnecting and it works. Only happens on my Nano devices, UNO and Nano IoT work.

// Nano power on serial port issue

// Send :V# after connect.
// Response Ok# is sent back. This fails after a power up of the Nano.

// Internal LED on D13
#define LED1 13
#define MAXCMDL 20 // max command length

// global variables
char inchr; // input character
int ixchr; // input character counter
char incmd[MAXCMDL]; // input command buffer

void setup()
{
// init variables
ixchr = 0;

// init i/o
Serial.begin(9600); // open serial port (usb)
pinMode(LED1, OUTPUT); // Internal LED
digitalWrite(LED1, 0); // turn off internal LED

} // end setup()

void loop()
{
// check for serial port commands
if (Serial.available() > 0)
{
inchr = Serial.read();
incmd[ixchr] = inchr; // save the character
if (inchr == '#')
{ // terminator character
incmd[ixchr] = '\0';
if (strlen(incmd) > 1) // command must have 3 or more characters
{
// ptrmsg = cmdexe(incmd);
Serial.print("Ok#");
ixchr = 0;
}
}
else
{ // next character
if (++ixchr == sizeof(incmd)) // check overflow
ixchr = 0; // rollover index
}

}

} // end loop()

Did you deliberately ignore the request to use code tags when posting your code ?

Sorry, I am new to Arduino programming and don't know what code tags are.
Please excuse my inexperience.

That is why I posted a link to How to get the best out of this forum

Using code tags when posting code here puts the code in a scrolling box with the ability to select all of it with a single click to enable it to be copied for examination in an editor

Hope this works.

// Nano power on serial port issue

// Send :V# after connect.
// Response Ok# is sent back.  This fails after a power up of the Nano.

// Internal LED on D13
#define LED1 13
#define MAXCMDL 20        // max command length

// global variables
char inchr;                 // input character
int ixchr;                  // input character counter
char incmd[MAXCMDL];        // input command buffer

void setup()
{
  // init variables
  ixchr = 0;

  // init i/o
  Serial.begin(9600);             // open serial port (usb)
  pinMode(LED1, OUTPUT);          // Internal LED
  digitalWrite(LED1, 0);          // turn off internal LED

} // end setup()

void loop()
{
  // check for serial port commands
  if (Serial.available() > 0)
  {
    inchr = Serial.read();
    incmd[ixchr] = inchr;     // save the character
    if (inchr == '#')
    { // terminator character
      incmd[ixchr] = '\0';
      if (strlen(incmd) > 1)  // command must have 3 or more characters
      {
        // ptrmsg = cmdexe(incmd);
        Serial.print("Ok#");
        ixchr = 0;
      }
    }
    else
    { // next character
      if (++ixchr == sizeof(incmd)) // check overflow
        ixchr = 0;    // rollover index
    }

  }

} // end loop()

That's better. See how much easier it is to read and copy ?

As to your problem, it is here

    incmd[ixchr] = inchr;     // save the character
    if (inchr == '#')
    {
      // terminator character
      incmd[ixchr] = '\0';
      if (strlen(incmd) > 1)  // command must have 3 or more characters
      {

You are overwriting the # character in the string with the terminating '\0' so if you only entered V# then all that is in the string now is the V and its length is not greater than 1

That is not a problem.
I don't use the # in the code. It is just the termination character for input.
Besides everything works on the UNO and anytime I use the Nano except on the first connect after powering it on.