Arduino Nano3 crashes when trying to connect a PC through USB

I have a little issue with Arduino Nano 3. My project uses 5V power to run arduino and connected devices, such as temperature sensor, LCD display, etc... I set up in my code to send data to serial in every 50ms. I have a desktop app that reads the serial data. Nothing else. Just reading.

And here comes the problem:

Scenario 1:
USB plugged in and Reset Arduino: The desktop app can read the data. When loosing the connectivity and reconnect with USB, there is no problem. The data comes again.

Scenario 2:
USB not plugged in. Arduino is running from external power unit (5V). When I plug the USB and connect it to my computer, it seems that Arduino is got frozen when I try to read data from the serial port. (USB)
If I reset the Arduino in this state, everything turning back to normal again.

:o :confused:

Scenario 2.1:
When I use the arduino board from external unit and plugging in to PC and my software would like to read data, Arduino is freezing down. However, when I open ArduinoIDE and open the serial monitor, then it resets the board automatically and everything will work again. This is very very irritative.

I tried to make the same logic in my code but it's not really helped. What else should I do?

When the board crashing, the green light goes off.

While working normally, the red TX LED is blinking fast.

I tried it with a C# application based on the Arduino IDE:

serialPort.Open();
serialPort.BaudRate = 1200;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DtrEnable = false;
serialPort.Close();
serialPort.Dispose();

Based on this, which is in Arduino IDE, serial monitor component:

public static boolean touchForCDCReset(String iname) throws SerialException {
SerialPort serialPort = new SerialPort(iname);
try {
serialPort.openPort();
serialPort.setParams(1200, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setDTR(false);
serialPort.closePort();
return true;

Funny fact:

Some times it works, some times does not!

The question here is how could I do this all without resetting? Any idea, any suggestion? The problem is not with the code within the Arduino.

This is what I'm using in my Sketch:

void serialWrite(String text)
{
if (Serial)
Serial.println(text);
}

Two important things missing.

A. The sketch you say is OK.

B. A schematic for your setup.

You may want to READ THIS also.

Bob.

Here is the code I'm trying to deal with:

void serialSetup()
{
  //Serial.begin(250000);
  Serial.begin(115200);
  //Serial.begin(9600);
  delay(100);

  serialOK = false;

  unsigned long ellapsed = millis();
  Serial.setTimeout(250);
  
  if (Serial)
    serialOK = true;
}

void serialWrite(String text)
{
    if (Serial)
      Serial.println(text);
}

void Login()
{
  if (Serial)
  {
    serialWrite("HELLO:"+serialID);
    serialWrite(MAXTEMP);
  }
}

unsigned long prevTime = millis();

void Communicate()
{
  /*if (Serial.available() > 0)
        {          
          String data = Serial.readStringUntil("\n");
          data.trim();
          if (data == "HELLO:AmProtector")
            AmProtectorLogin();
        }*/
  
  //sends data in every 100ms only!
  if (((unsigned long)(millis() - prevTime)) > 50)
  {
        Login();

      if (! initDone)
        serialWrite(
          "INIT:" + (String)leftAvg + "|" + (String)rightAvg + 
  "|" + String((int)round((potMeterLastVal_L / (float) 1023) * 100)) + "|" + String((int)round((potMeterLastVal_R / (float) 1023) * 100)) + 
  "|" + (String)tempL +"|" + (String)tempR + 
  "|" + (String)fanSpeedL +"|" + (String)fanSpeedR + 
  "|" + ((overDriveProtectionPROTECT_L) ? "1" : "0") + "|" + ((overDriveProtectionPROTECT_R) ? "1" : "0") +
  "|" + ((overTempProtection_L) ? "1" : "0") + "|" + ((overTempProtection_R) ? "1" : "0") +
  "|" + ((mainProtection_L) ? "1" : "0") + "|" + ((mainProtection_R) ? "1" : "0")
          /*"INIT:" + String((int)round((potMeterLastVal_L / (float) 1023) * 100)) + "|" + 
        String((int)round((potMeterLastVal_R / (float) 1023) * 100))*/);
      else
        serialSendStatus();

      prevTime = millis();
  } 
}

void serialSendStatus()
{
  serialWrite("LVLS:" + (String)leftAvg + "|" + (String)rightAvg + 
  "|" + String((int)round((potMeterLastVal_L / (float) 1023) * 100)) + "|" + String((int)round((potMeterLastVal_R / (float) 1023) * 100)) + 
  "|" + (String)tempL +"|" + (String)tempR + 
  "|" + (String)fanSpeedL +"|" + (String)fanSpeedR + 
  "|" + ((overDriveProtectionPROTECT_L) ? "1" : "0") + "|" + ((overDriveProtectionPROTECT_R) ? "1" : "0") +
  "|" + ((overTempProtection_L) ? "1" : "0") + "|" + ((overTempProtection_R) ? "1" : "0") +
  "|" + ((mainProtection_L) ? "1" : "0") + "|" + ((mainProtection_R) ? "1" : "0")
  );
}

setup()
{
 serialSetup();
}

loop()
{
 Communicate();
}

And here is the schematics:

(MOD EDIT)

Just a comment on this code

serialPort.Open();
                serialPort.BaudRate = 1200;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                serialPort.Parity = Parity.None;
                serialPort.DtrEnable = false;
                serialPort.Close();
                serialPort.Dispose();

Usually, one would first configure a serial port and next open it. See examples in Serial Port class.

If you change that, I suspect (99% sure) that the Nano will not reset.

The fact that the Arduino does it different has its reason; e.g. Arduino wants the board to reset and hence asserts the DTR and later removes it.