Serial in Mega takes long to open, then gets stuck if data is coming

Not sure if this has being discussed before or if it is "normal" behavior. Or wondering if I am doing some wrong.

I have been working for a while in a fast arduino server similar to what matlab provides via the usb to serial interface, but much faster and more open, so some the scientists I work with but are not familiar with C++ can control sensors and different hardware from Matlab. One of the researches I work with wanted to use an alpha version to test in a project, and we found didn't communicate in an old basic laptop he brought. Otherwise works in over a dozen of much powerful computers I tried. I found that in this computer it takes nearly 10 seconds to open the serial port. After debugging for a while, I simplified to some as simple as:

void setup() {
Serial.begin(115200);
Serial.print("O");
}
void loop() {
}

Once the port is opened in the PC side, it is assigned a handle with flying colors (no errors). The Arduino Mega resets immediately (as expected) but takes around 9 seconds to finally start and be ready to send the O back. I even tap with a different computer with RealTerm on the TX pin.
I also moved this simple example code to a much powerful computer and still takes around a second same deal.
As this is computer variable, I was suspecting in the USB to Serial converter controlled by the PC driver, but not sure.
What is even more problematic is that if the PC side starts sending data to the arduino because thinks the port is happy opened, but still is not, now the arduino gets stuck until you stop plus another 10 secs before the port finally opens.
I found that because my PC side was doing just that, so the protocol sends a header after the port is opened, that never makes' it, so tries again, and again... In the faster computers this was not a problem because the server side had a 1 sec delay after opening the serial, but puting 10 sec now thinking in the slows computers would be bad.

SO what I did so far is to send a char after Serial.begin() on the server side as shown in my simple example above ('O'), and wait on the PC client whatever it takes before proceeding. Then flush the O and start protocol header....

Still wondering if I got some wrong, or if is a better driver fix or something, as 9 secs looks like an insane amount of time to open a serial port. In fact, I have being writing serial programs like this for ages, and I can't remember some this ugly.

Note that in the fastest computer still will take 1 sec to open, and if I send data before that second, the opening keeps getting postponed... Interestingly enough I can see the data that comes from the PC via the USB to serial converter to hit the ATMEL MEGA, while the same MEGA can't start. Almost looks like if it is resetting during that time, but it is not (checking with oscilloscope). So I only can think of the bootloader that is failing to come up in the MEGA until data at serial RX stops, no idea why.

Anyway, any feedback will for sure be appreciated!

Thank you.

never particularly noticed this delay before but ran your test program on a UNO and pressing the reset button there is an appreciable delay (about a second) before the 0 appears on the serial monitor
then ran the program on an Arduino Due and an ESP32 - no noticeable delay on either

when running programs (in C++, C#, Java) on a PC which connect to COM ports I do a COM port scan then open each port in turn and send a prompt, e.g. ? character. The target code is programmed to respond with a set message - if received I know I am connected to the correct target.
if no message received within a set time (couple of seconds) close port and move to next

in projects where Matlab was used we tended to communicate over Ethernet - mainly due to the large volumes of data acquired per second,

Remove the boot loader from the Arduino when you upload your test code (use an ISP programmer).
Having the boot loader is creating a delay as the board listens for a potential code upload for a while before getting started.

That being said, the boot loader is very convenient, so may be you could install Optiboot and keep the benefits of a boot loader but with faster start time.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.