arduino / arduino-ide

Arduino IDE 2.x
https://www.arduino.cc/en/software
GNU Affero General Public License v3.0
2.07k stars 353 forks source link

DTR gets disabled when uploading on Windows #2417

Open RandyParedis opened 1 month ago

RandyParedis commented 1 month ago

Describe the problem

Hi there,

My apologies if this is a duplicate issue, but I have been unable to find anything that solved my problem.

I have an official Arduino Nano Every that I connect to my computer via USB (on COM3). The sketch I have assumes there is an external process that communicates with the Arduino over USB (Baudrate is 115200, Parity none and 8 data bits). However, I noticed that Serial.available() will always yield 0.

So, I did some digging and I realized that the code only started to work after I opened and closed the Serial monitor. When looking into the device state (by running mode COM3 /state on Windows), I was able to discern that I can only receive an input when DTR is ON. When I use the Upload button (or "Upload using Programmer") in the IDE, DTR is always OFF afterwards. Opening and closing the Serial monitor appears to have the same effect as calling mode COM3 BAUD=115200 PARITY=n DATA=8 DTR=on.

The external process I use for communicationis an executable someone else has written, so I do not have access to that source code. But the issue persists when using set /p x="999" <nul >\\.\COM3 to force an input. It is not an option for any hardware modifications on my end.

To reproduce

  1. Connect an Arduino Nano Every over USB. My Arduino is connected on the COM3 port, but I will use COMx in the steps below.
  2. Use the IDE to create a sketch with following code:
    
    void setup() {
    Serial.begin(115200);
    Serial.setTimeout(10);
    pinMode(LED_BUILTIN, OUTPUT);
    }

void loop() { if(Serial.available() > 0) { String signal = Serial.readString(); signal.trim();

// Startup command
if(signal == "999") {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  return;
}

digitalWrite(LED_BUILTIN, LOW);

} }


3. Upload the sketch using the Upload button, or "Upload Using Programmer".
4. Send a signal to the Arduino. I use the command prompt: `set /p x="999" <nul >\\.\COMx`. Nothing happens.
5. Open DTR on the device by calling `mode COMx BAUD=115200 PARITY=n DATA=8 DTR=on` from the command prompt.
6. If we now execute (4) again, the led turns on! We can turn it off by calling `set /p x="0" <nul >\\.\COMx`
7. If we repeat steps (3) and (4) again, nothing happens once more as DTR has been disabled again.

### Expected behavior

I would expect that either DTR is opened automatically, or that it would be reset to the previous state after upload. Alternatively, having a setting that allows a user to select which DTR state is preferred on the port can be the best of both worlds.

### Arduino IDE version

2.3.3-nightly-20240416

### Operating system

Windows

### Operating system version

10 and 11

### Additional context

I was able to find the following thread that helped me identify the issue:
  https://forum.arduino.cc/t/sketch-only-works-after-serial-monitor-is-activated-solved/880297

However, constantly needing to updating the DTR circuit line before the `Serial` connection starts to work is incredibly obnoxious and feels counter-intuitive. My biggest issue is that I have to do this after **every** upload, which makes debugging code (the Arduino Nano Every does not support the IDE's debugger) quite a hassle.

### Issue checklist

- [X] I searched for previous reports in [the issue tracker](https://github.com/arduino/arduino-ide/issues?q=)
- [X] I verified the problem still occurs when using the latest [nightly build](https://www.arduino.cc/en/software#nightly-builds)
- [X] My report contains all necessary details