question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upload & Monitor and Test find wrong serial port for Arduino Leonardo, others

See original GitHub issue

Configuration

Windows 10, also OS X 10.14:

PlatformIO Version (platformio --version): PlatformIO, version 4.1.0b3

Description of problem

“Upload & Monitor” and “Test” will open the wrong serial port on devices such as the Arduino Leonardo or Adafruit M0 & M4 devices that use a software USB interface that re-enumerates after uploading a new program.

Steps to Reproduce

  1. Set up project with code below
  2. Try “Upload & Monitor” and get either wrong serial port, or error.
  3. Try “Test” and after code uploads get wrong serial port, or error.

Actual Results

Depending on configuration upload & monitor or test fail in one of the following ways:

  • You’re prompted for which serial port to use, with a stale list of devices by the time you choose one.
  • The wrong serial port is chosen, if for example you have another serial device besides the device. On OS X this is often the bluetooth serial port, so the test output will never appear.
  • Windows cannot find the file specified if you specify the corresponding monitor_port or test_port

Expected Results

Platformio should wait until the device enumerates again and choose the correct serial port.

If problems with PlatformIO Build System:

The content of platformio.ini:

[env:leonardo]
platform = atmelavr
board = leonardo
framework = arduino
;monitor_port = COM10
;test_port = COM10

Source file to reproduce issue: src/main.cpp

#include <Arduino.h>

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  Serial.println("On");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  Serial.println("Off");
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

test/test_main.cpp

#include <Arduino.h>
#include <unity.h>

// void setUp(void) {
// // set stuff up here
// }

// void tearDown(void) {
// // clean stuff up here
// }

void test_led_builtin_pin_number(void) {
    TEST_ASSERT_EQUAL(13, LED_BUILTIN);
}

void test_led_state_high(void) {
    digitalWrite(LED_BUILTIN, HIGH);
    TEST_ASSERT_EQUAL(HIGH, digitalRead(LED_BUILTIN));
}

void test_led_state_low(void) {
    digitalWrite(LED_BUILTIN, LOW);
    TEST_ASSERT_EQUAL(LOW, digitalRead(LED_BUILTIN));
}

void setup() {
    // NOTE!!! Wait for >2 secs
    // if board doesn't support software reset via Serial.DTR/RTS
    delay(2000);

    UNITY_BEGIN();    // IMPORTANT LINE!
    RUN_TEST(test_led_builtin_pin_number);

    pinMode(LED_BUILTIN, OUTPUT);
}

uint8_t i = 0;
uint8_t max_blinks = 5;

void loop() {
    if (i < max_blinks)
    {
        RUN_TEST(test_led_state_high);
        delay(500);
        RUN_TEST(test_led_state_low);
        delay(500);
        i++;
    }
    else if (i == max_blinks) {
      UNITY_END(); // stop unit testing
    }
}

Additional info

If I configure monitor_port or test_port then I get the “cannot find file specified” error in windows.

If I don’t, then I will get a serial error on upload & monitor because it chooses the leonardo programming serial port which disappears when the leonardo reboots.

Here’s some other people probably hitting the same issue:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
elguiricommented, Dec 15, 2019

If anybody else has this issue prior to a fix, there is a workaround described here… https://community.platformio.org/t/any-way-to-configure-timeout-for-upload-monitor/3812

0reactions
sphereinaboxcommented, Oct 25, 2019

Yeah, Upload alone works fine, and Monitor alone works fine, there’s just the race condition when trying to do one then quickly do the other on these devices that do software USB serial.

I think get_test_port() for example should:

  • If there is a test_port or monitor_port configured, then use the retry & timeout logic instead of immediately trying and failing to open that port after uploading code.
  • If the hardware has USB device IDs for serial, then I don’t think platformio should to be trying to connect with serial ports that don’t match these device IDs. In particular platformio should wait to find the leonardo or whatever instead of connecting to the always-there /dev/cu.Bluetooth-Incoming-Port or maybe I should be able to blacklist /dev/cu.Bluetooth-Incoming-Port

If I were to make a pull request with the changes I propose, which branch should I start from?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[solved]Problem with Serial Communication on Leonardo
This result is very strange: If i do the Serial Monitor (IDE->Tools->Serial Monitor). Its works realy fine: Sending data -> The LED on...
Read more >
Leonardo No Serial Output - Installation & Troubleshooting
I'm trying to get the serial output to work but I'm not receiving anything on my serial port, either through serial monitor or...
Read more >
Leonardo -- Serial Issues - Programming Questions
I currently have a sketch that relies on Serial communication to operate. It works with no issues on the Uno and Mega, but...
Read more >
Leonardo - Unable to upload sketch (error message in post)
Other debugging: Check in device manager what it says for the Leonardo; usually it will say Arduino Leonardo with a COMx; after pressing...
Read more >
Can't print to COM port with Leonardo - Arduino Forum
That means that Windows might have assigned a new port; check the IDE (and device manager if needed). Change the port and try...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found