NANO no data from serial port after disconnecting and reconnecting

I have a problem with Arduino Nano.

My sketch is working perfect with the Arduino UNO.

But with the NANO this problem appears: after uploading the sketch OK, then after disconnecting the usb port and reconnecting, no serial data sent.

The device is powered by USB port only, no additionally circuitry is used. I tried another NANO but same situation.

Can anybody please help me?

I did try to add the sketch in `` but too much characters (> 9000), can I do it in an other way?

gritte:
I have a problem with Arduino Nano.

My sketch is working perfect with the Arduino UNO.

But with the NANO this problem appears: after uploading the sketch OK, then after disconnecting the usb port and reconnecting, no serial data sent.

The device is powered by USB port only, no additionally circuitry is used. I tried another NANO but same situation.

Can anybody please help me?

I did try to add the sketch in `` but too much characters (> 9000), can I do it in an other way?

Are you hinting that you did exactly the same connect-disconnect-connect sequence with the Uno and it worked?

Paul

As I can't find my previous issue, I start again. I wanted to answer to the question of the moderator but my PC broke. After restart my issue has disappeared.

So my problem again:

I uploaded my sketch on my UNO first. All is working correctly. After disconnecting and reconnecting the usb port, no problem appeares. The serial port is working correctly.

When data are sent from the PC, LED's show a short flashing. OK.

In this state of development, no additionally circuitry is used. It's for testing the serial port speed only.
The Arduino is powered by the usb port only.

Now I did exactly the same with the NANO. (original type)
Directly after uploading the sketch, everything was OK.

But after disconnecting and reconnecting the usb port, the serial port sent no data when requested by the PC.
When data are sent from the PC, LED's show flickering, I guess half a second. ???

I tried the same with a cheap chinese type, with the same result.
I also changed PC, ports, usb cables, but no success.

So it seems to be a difference between UNO and NANO? How to solve?

I wanted to add my code in `` but more than 9000 characters. How to do this?

Thank you for your efforts.

Bookmark your topics

You can attach files to post code. But for some simple testing, your code should not be 9000 characters :wink:

gritte:
But after disconnecting and reconnecting the usb port, the serial port sent no data when requested by the PC.

After disconnecting and reconnecting, the (internal) handle that the operating system keeps is no longer valid. You're lucky that it did seem to work for the Uno and I do not have an answer as to why this is (maybe because it identifies itself as an Uno and not as a serial port only).

Close and re-open the terminal application is the solution.

Which operating system?

Hi,
I deleted all comments and so reduced the file size fro 13kB to 7kB. Hopefully I did no mistake.
The sketch contains all planned functions for my project, but till jet is only used for testing the serial port.

On PC side I use PureBasic because with this proceccing language I'm not fit for programming diagrams, graphics ect. If it make sense, I send this code also.

here the Arduino sketch:

const int MessPin0 = A0;
const int MessPin1 = A1;
const int MessPin2 = A2;
const int MessPin3 = A3;
const int MessPin4 = A4;
const int MessPin5 = A5;

const int Steuerspannung1P = 13;
const int Steuerspannung1M = 12;

const int Steuerspannung2P = 11;
const int Steuerspannung2M = 10;

const int Steuerspannung3P = 5;
const int Steuerspannung3M = 4;

const int Steuerspannung4P = 3;
const int Steuerspannung4M = 2;

const int PWMSpannung = 6;

const int LedPin1 = 8;
const int LedPin2 = 9;
int Led = 0;
int Aus = 0;

const int TasterPin = 7;
int Taster = 0;

int i = 0;
String Zeichen = "";
String Selektor = "";
String ss = "";
int Selekt = 0;                                   
int Zahl = 0;
String Wert = "";
int stellen = 0;
int Strom = 0;
String Zeit = "";
int warte = 0;

void setup() {                                        
  Serial.begin(57600);
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  
  pinMode(TasterPin, INPUT_PULLUP);

  pinMode(Steuerspannung1P, OUTPUT);
  pinMode(Steuerspannung1M, OUTPUT);
  
  pinMode(Steuerspannung2P, OUTPUT);
  pinMode(Steuerspannung2M, OUTPUT);
  
  pinMode(Steuerspannung3P, OUTPUT);
  pinMode(Steuerspannung3M, OUTPUT);
  
  pinMode(Steuerspannung4P, OUTPUT);
  pinMode(Steuerspannung4M, OUTPUT);

  pinMode(PWMSpannung, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {  
  digitalWrite(LED_BUILTIN, LOW);                        
  if (Serial.available() > 0) {
    digitalWrite(LED_BUILTIN, HIGH);
    
    Zeichen = Serial.read();   
    Zahl = Zeichen.toInt() - 48;
    
    if (Zeichen.equals("77")){
      Serial.print("A" + String(analogRead(MessPin1)) + " ");
      Serial.print("B" + String(analogRead(MessPin2)) + " ");
      Serial.print("C" + String(analogRead(MessPin3)) + " ");
      Serial.print("D" + String(analogRead(MessPin4)) + " ");
      Serial.println();
    }
    
    if (Zeichen.equals("84")){
      Serial.print("P" + String(analogRead(MessPin0)) + " ");
      Serial.print("R" + String(analogRead(MessPin5)) + " ");
      Serial.println();
    }  

    if((Zahl <0) || (Zahl > 9)) Selekt = 0;

    if (Selekt == 1){
      Selektor = Selektor + String(Zahl);
    }
    
    if(Zeichen.equals("83")) {
      Selekt = 1;
      Selektor = "";
    }   
    
    if (Zeichen.equals("43")){ 
      if (Selektor.length() == 4){
        ss = Selektor.charAt(0);
        if (ss.equals("1")){
          digitalWrite(Steuerspannung1P, HIGH);
          digitalWrite(Steuerspannung1M, LOW);
        } else {
          digitalWrite(Steuerspannung1P, LOW);
          digitalWrite(Steuerspannung1M, LOW);          
        }
        ss = Selektor.charAt(1);
        if (ss.equals("2")){
          digitalWrite(Steuerspannung2P, HIGH);
          digitalWrite(Steuerspannung2M, LOW);
        } else {
          digitalWrite(Steuerspannung2P, LOW);
          digitalWrite(Steuerspannung2M, LOW);
        }
        ss = Selektor.charAt(2);
        if (ss.equals("3")){
          digitalWrite(Steuerspannung3P, HIGH);
          digitalWrite(Steuerspannung3M, LOW);
        } else {
          digitalWrite(Steuerspannung3P, LOW);
          digitalWrite(Steuerspannung3M, LOW);
        }
        ss = Selektor.charAt(3);
        if (ss.equals("4")){
          digitalWrite(Steuerspannung4P, HIGH);
          digitalWrite(Steuerspannung4M, LOW);
        } else {
          digitalWrite(Steuerspannung4P, LOW);
          digitalWrite(Steuerspannung4M, LOW);
        }
                              
      }

    }

    if (Zeichen.equals("45")){
      if (Selektor.length() == 4){
        ss = Selektor.charAt(0);
        if (ss.equals("1")){
          digitalWrite(Steuerspannung1P, LOW);
          digitalWrite(Steuerspannung1M, HIGH);
        } else {
          digitalWrite(Steuerspannung1P, LOW);
          digitalWrite(Steuerspannung1M, LOW);          
        }
        ss = Selektor.charAt(1);
        if (ss.equals("2")){
          digitalWrite(Steuerspannung2P, LOW);
          digitalWrite(Steuerspannung2M, HIGH);
        } else {
          digitalWrite(Steuerspannung2P, LOW);
          digitalWrite(Steuerspannung2M, LOW);
        }
        ss = Selektor.charAt(2);
        if (ss.equals("3")){
          digitalWrite(Steuerspannung3P, LOW);
          digitalWrite(Steuerspannung3M, HIGH);
        } else {
          digitalWrite(Steuerspannung3P, LOW);
          digitalWrite(Steuerspannung3M, LOW);
        }
        ss = Selektor.charAt(3);
        if (ss.equals("4")){
          digitalWrite(Steuerspannung4P, LOW);
          digitalWrite(Steuerspannung4M, HIGH);
        } else {
          digitalWrite(Steuerspannung4P, LOW);
          digitalWrite(Steuerspannung4M, LOW);
        }
                              
      } 
      
    }
    
    if(((Zahl <0) || (Zahl > 9)) && (stellen == 1)){
      stellen = 0;
      Strom = map(Wert.toInt(), 0, 100, 0, 255);     
      analogWrite(PWMSpannung, Strom);
    }

    if (stellen == 1){
      Wert = Wert + String(Zahl);
    }
        
    if (Zeichen.equals("75")){
      stellen = 1;
      Wert = "";
    }
    
    if(((Zahl <0) || (Zahl > 9)) && (warte == 1)){
      warte = 0;
      delay(Zeit.toInt());    
    }

    if (warte == 1){
      Zeit = Zeit + String(Zahl);
    }
        
    if (Zeichen.equals("86")){
      warte = 1;
      Zeit = "";
    }
    
    if (Led == 1){
      if (Zahl == 1){
        digitalWrite(LedPin1, HIGH);
      }
      if (Zahl == 2){
        digitalWrite(LedPin2, HIGH);
      } 
      Led = 0;      
    }

    if (Zeichen.equals("76")){
      Led = 1;
    }
    if (Aus == 1){
      if (Zahl == 1){
        digitalWrite(LedPin1, LOW); 
      }
      if (Zahl == 2){
        digitalWrite(LedPin2, LOW); 
      } 
      Aus = 0;      
    }

    if (Zeichen.equals("65")){
      Aus = 1;
    }

    if (Zeichen.equals("69")){
      Zeichen = "";
      Serial.println("X");
    }

    if (Zeichen.equals("67")){
      Serial.println(String(map(Strom, 0, 255, 0, 100)));
    }
    
  }
    
  Taster = digitalRead(TasterPin);                     
  if (Taster == LOW) {
    analogWrite(PWMSpannung, 0);
    Strom = 0;
    digitalWrite(LedPin2, HIGH);
    delay(1000);
    digitalWrite(LedPin2, LOW);         
  }
  
}

Any connection to the serial port needs to be "closed" and "reopened" from the PC's perspective after the port is unplugged and replugged.

If you're saying that, with any serial port, it resumes working without closing and reopening the port, that is wacky behavior - the only way that could happen is if the terminal application is detecting the port's disappearance and reappearance, and silently reconnecting to it (and this behavior could vary between serial adapters, as some serial adapters seem to have a unique identifier, while others do not, and the application could be detecting that the replugged device was definitely the same as the unplugged one, and reconnecting. I think the 16u2 on the Uno does have that identifier, not sure about the FT232RL on official nano, the CH340G on clones does not appear to).

I have also seen a COM port refuse to work until after a reboot when the hardware is unplugged while an application is connected to it, it's best to disconnect the software from the port before unplugging the hardware.

Threads merged.

Hi,
now I tried a Processing script, this is working. But why the Nano (or UNO) needs 3 times receiving data bevore sending the right result?

Send data : "E"; received Data expected "X", but

NULL
NULL
NULL
X

How can I avoid this?

Anyways, I'm not happy to have to use this Processing. If I could use an intuitive language as PureBasic it woulcd be easy to setup diagrams ect., but using Processing ?

import processing.serial.*;

Serial myPort;
String val;

void setup()
{
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 57600);
}

void draw(){
  myPort.write("E");                 //send "E"
  
  if ( myPort.available() > 0){      //receive "X"
    val = myPort.readString();
  }
  
println(val);
delay(1000);
}

Not a processing person

But when you open a serial port and the DTR line is active, most Arduinos reset and start the bootloader. After a short time, the boot loader will hand over control to your sketch.

I suspect that your NULLs come from the fact that the boot loader is running.

Solutions
1)
Do not activate DTR.
2)
Let the Arduino send a message like "I'm here" in setup() and check in your purebasic or processing application for that text before continuing.

Hi sterretje,

many thanks for your hint, Nano and Processing are working together now.

The problem remains for Nano and PureBasic.

Code on NANO side:

String Zeichen = "";

void setup() {
  Serial.begin(57600);
  Serial.println("I'm ready");
}

void loop() {
  if (Serial.available()) {
    Zeichen = Serial.read();   
    if (Zeichen.equals("69")) Serial.println("X"); 
  }
}

Code on PureBasic Side:

Declare.s Kommando(Befehl.s)

Befehl.s = "E"

Ergebnis.s = Kommando(Befehl)
Debug Ergebnis


Procedure.s Kommando(Befehl.s)
  *puffer = AllocateMemory(64)
  If OpenSerialPort(0, "COM6", 57600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 64, 64)
    If IsSerialPort(0) > 0
      Erfolg.i = WriteSerialPortString(0, Befehl)
      If Erfolg > 0
        Delay(20)
       length.i = AvailableSerialPortInput(0)
       If length > 0
         AnzahlBytes.i = ReadSerialPortData(0, *puffer, length)
         Werte.s = PeekS(*puffer, AnzahlBytes, #PB_Ascii)
         ;Debug "Messwert: " + Werte + "  Anzahl Bytes: " + Str(AnzahlBytes)
        Else
          Debug "Keine Bytes vom Arduino gesendet"
        EndIf
      Else
        Debug "konnte Daten an Arduino nicht senden"
      EndIf
    Else
      Debug "Serieller Port ungültig"
    EndIf
 
  Else
    Debug "Serielle Schnittstelle funktioniert nicht"
  EndIf
  CloseSerialPort(0)
  FreeMemory(*puffer)
  ProcedureReturn Werte
EndProcedure