Required serial delay after reset?

I'm trying to interface with a piece of software that uses JSSC for its comms, but running into an issue where the software is sending requests immediately after the port is opened. As the Arduino is resetting when the port is opened, this is causing problems as its going into code loading mode when it receives the first request.

We can introduce a delay before sending the first request, but I need it to be as short as possible and i can't find anywhere that formally states how long the wait time is upon startup. As it's a mega 2560, I tried looking through the atmega16u2 serial firmware, but couldn't see the delay amount defined.

Can anyone confirm exactly how long I need to wait after reset before sending the first request?

We can introduce a delay before sending the first request, but I need it to be as short as possible and i can't find anywhere that formally states how long the wait time is upon startup.

If the code has to invoke a bunch of constructors, it will take longer to be ready than if it doesn't.

If the code is running on a Leonardo that has to wait for Serial1 to be ready, it will take longer to be ready than if it doesn't.

So, there is no way for the PC software to know how long the Arduino is going to take to reset.

The best thing is for the PC to wait for the Arduino to say "Yo, dude, I'm ready for you to start shoveling data my way" (or something like that).

The Arduino would say that as soon as it was ready.

PaulS:
The best thing is for the PC to wait for the Arduino to say "Yo, dude, I'm ready for you to start shoveling data my way" (or something like that).

Agree 100%.

I have used that technique with JRuby and JSSC.
However my Arduino just says "Arduino is Ready" :slight_smile:

...R

PaulS:
The best thing is for the PC to wait for the Arduino to say "Yo, dude, I'm ready for you to start shoveling data my way" (or something like that).

The Arduino would say that as soon as it was ready.

Unfortunately I have no ability to change the serial protocol at all to do this. I can alter how/when things are sent (to a degree) but not what is being sent.

I am able to limit this to just mega 2560s though, so there's not a worry about how different devices will behave. If there's not a standard spec answer though I might just go with trial and error to see how short I can get this.

If all you can do is wait for some time after the PC program opens the serial port then try 10 seconds. If that does not work try 20 seconds.

I can't understand how you can change the PC program so it will wait N seconds and yet you cannot change it to use a more sophisticated and sensible system.

By the way, the problem you describe is not a JSSC problem. It is with how the program uses JSSC.

...R

Robin2:
I can't understand how you can change the PC program so it will wait N seconds and yet you cannot change it to use a more sophisticated and sensible system.

The system has to remain talking to other devices, not just the Arduino, and they have their code locked. A small delay can be introduced on the host software and it will still work with those devices, but I can't change the protocol as it will then cause them to fail.

Given its a known characteristic of the Arduino serial system, I was just surprised that there wasn't a formal time for this. IE 'if you wait at least 800ms then you can be sure all Arduino will be out of code loading mode'

By the way, the problem you describe is not a JSSC problem. It is with how the program uses JSSC.

Whilst that is true, switching to JSSC has triggered the problem. Previously rxtx had been used, but as that is no longer supported the choice was made to switch. For whatever reason rxtx has enough of a delay in its own code that this isn't an issue.

Given its a known characteristic of the Arduino serial system

What is?

Your question is like asking how long does it take to run a mile? Well, are you Jesse Owen with the cops chasing you, or are you my great-grandmother pushing a walker on a nice sunny day?

Come on? How long?

PaulS:
What is?

The period of time after a reset that the system will look for new code coming in. My understanding is that there is a limited period of time after a reset that the firmware will look to see if new code is being sent from the Arduino IDE. If you send anything to the serial line during that period it assumed it is a program download.

Your question is like asking how long does it take to run a mile? Well, are you Jesse Owen with the cops chasing you, or are you my great-grandmother pushing a walker on a nice sunny day?

Come on? How long?

Again I may be wrong here, but I had assumed the time the bootloader is watching for the new code upload was a fixed time, but maybe it isn't. Documentation pages such as http://playground.arduino.cc/Main/ArduinoReset only say 'a few seconds', I was just wanting to know whether there was anything more concrete than that.

Again I may be wrong here, but I had assumed the time the bootloader is watching for the new code upload was a fixed time

It is. But, once it has decided that no new code is coming, and starts running the current code the time until the serial port is ready to receive data is not. That time depends on what the installed sketch is doing.

I mean, suppose that the first instruction in your setup() is Serial.begin(), and there are no classes being instantiated, and the first instruction in my setup() is delay(12000000) and there are 47 instances being created and each of those constructors contains a call to delay(1000).

Which one is going to be ready to receive data first?

PaulS:
It is.

Is it known what that time is? That's what I'm chasing.

But, once it has decided that no new code is coming, and starts running the current code the time until the serial port is ready to receive data is not. That time depends on what the installed sketch is doing.

I mean, suppose that the first instruction in your setup() is Serial.begin(), and there are no classes being instantiated, and the first instruction in my setup() is delay(12000000) and there are 47 instances being created and each of those constructors contains a call to delay(1000).

Yep, that all makes sense, but the Serial.begin() is one of the first things I'm doing in setup(). The interface program is currently sending the first serial request within about 30ms of the port being opened and it certainly appears that this is causing it to fall into code download mode, regardless of what my sketch is doing .

Is it known what that time is?

I'm sure it is. Not by me, though.

one of the first things I'm doing in setup().

So, you are only concerned about your sketches?

PaulS:
So, you are only concerned about your sketches?

What I meant is that I think I've done everything that's possible within the sketch. Any other delay that's required will be required because of the bootloader.

noisymime:
IE 'if you wait at least 800ms then you can be sure all Arduino will be out of code loading mode'

I suggested you try 10 seconds - did you ?

Why not do some tests with the Serial Monitor and a short Arduino program that prints "I'm ready" as soon as it can?

The system has to remain talking to other devices, not just the Arduino, and they have their code locked. A small delay can be introduced on the host software and it will still work with those devices, but I can't change the protocol as it will then cause them to fail.

I would be very suprised if there isn't a program work-around for all that. Think how the program would have been designed if the requirements of the Arduino had been known at the outset.

By the way, it is possible to modify an Uno so that it doesn't reset when the Serial port is opened. Or you could communicate with it using a USB-TTL cable connected to the Rx and Tx pins and GND.

...R

For what it's worth in case anyone else ever needs the info, the delay time seems to be somewhere between 850ms and 920ms. 900ms will usually work, but is right on the borderline and misses about 1/10 depending on your timing accuracy, 920ms seems to be 'safe'.