# Nano serial com issue

**URL:** <https://forum.arduino.cc/t/nano-serial-com-issue/892431>\
**Category:** General Guidance\
**Created:** [August 4, 2021, 6:24pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431 "2021-08-04T18:24:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 4, 2021, 6:24pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/1 "2021-08-04T18:24:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 4, 2021, 6:34pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/2 "2021-08-04T18:34:30Z")

</div>

> [@mho-observatory](#):
>
> Same code is downloaded in all my Arduinos.

What code would that be ?

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 4, 2021, 9:37pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/3 "2021-08-04T21:37:56Z")

</div>

That is correct.

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 4, 2021, 11:51pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/4 "2021-08-04T23:51:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 5, 2021, 12:02am UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/5 "2021-08-05T00:02:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 5, 2021, 12:49am UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/6 "2021-08-05T00:49:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 10:41am UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/7 "2021-08-05T10:41:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 5, 2021, 10:50am UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/8 "2021-08-05T10:50:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrisknightley](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/chrisknightley/32/637257_2.png) [@chrisknightley](https://forum.arduino.cc/u/chrisknightley)\
**Post date:** [August 5, 2021, 10:53am UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/9 "2021-08-05T10:53:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 12:28pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/10 "2021-08-05T12:28:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [August 5, 2021, 12:56pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/11 "2021-08-05T12:56:46Z")

</div>

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

Thanks.. Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 2:49pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/12 "2021-08-05T14:49:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 5, 2021, 2:50pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/13 "2021-08-05T14:50:59Z")

</div>

> [@mho-observatory](#):
>
> Is it ok just to put it in my reply?

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

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 3:52pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/14 "2021-08-05T15:52:57Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 5, 2021, 3:56pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/15 "2021-08-05T15:56:28Z")

</div>

> [@mho-observatory](#):
>
> Here is the test code

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

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 5:43pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/16 "2021-08-05T17:43:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 5, 2021, 5:57pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/17 "2021-08-05T17:57:44Z")

</div>

> [@mho-observatory](#):
>
> don't know what code tags are

That is why I posted a link to [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code)

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

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 7:42pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/18 "2021-08-05T19:42:49Z")

</div>

Hope this works.

```cpp
// 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()

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 5, 2021, 8:55pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/19 "2021-08-05T20:55:11Z")

</div>

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

As to your problem, it is here

```cpp
    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

---

<div class="post-metadata">

**Author:** ![mho-observatory](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mho-observatory](https://forum.arduino.cc/u/mho-observatory)\
**Post date:** [August 5, 2021, 10:26pm UTC](https://forum.arduino.cc/t/nano-serial-com-issue/892431/20 "2021-08-05T22:26:55Z")

</div>

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.

[Next page](https://forum.arduino.cc/t/nano-serial-com-issue/892431.md?page=2)
