code in Arduino to connect/disconnect serial communication

Support forum for MegunoLink
Post Reply
Posts: 3
Joined: Wed Mar 04, 2015 12:31 pm

Wed May 27, 2015 6:36 pm


I noticed that with the last MLP version, upon connecting (in any window, e.g., Time Plot, Monitor, Interface Panel or Connection Manager) after disconnecting the serial communication, the arduino sketch restarts from scratch as if it has just been loaded.
This somewhat confusing feature can nevertheless be useful as an error handling tool.
With this in mind, I'm seeking the (arduino) code to implement such a function: doing the following
1. on error (some faulty condition), disconnect (i.e., press the disconnect button)
2. re-connect (i.e., press the connect button).
in order to restart the sketch anew.
Am I missing anything obvious?
Are there such commands in Arduino (like restart, connect, disconnect)?

Also, some times the communication with the serial port is lost, and everything hangs out in a locked state; the only ways I found to recover communication is to either
a. physically disconnect and reconnect the serial communication cable, or
b. do the same using Windows Device Manager to Disable and next Enable the COMM port to which the arduino is connected.
Since both actions require manual intervention, I'd like to use a function in the Arduino code to do that automatically and remotely.

I'll appreciate any input on either (or both) matter(s).

Site Admin
Posts: 16
Joined: Mon May 26, 2014 10:53 am

Fri May 29, 2015 10:07 am

Hi Claude,

The reset on connect behavior is a recent change to better support boards, such as the Leonardo, which require this for serial communications to work properly. You can switch to the old behavior by clicking the [Advanced] button in the connection manager (the serial port must be disconnected for this). Then check the box labeled Disable DTR. Now, the Arduino won't reset when you connect. Unfortunately this one work for boards that require the reset signal to setup the serial port (Leonardo, Yun, etc). Its fine for the Uno and Mega boards though.

It would be interesting to use MegunoLink Pro for error handling, but I wonder if you're familiar with the Watchdog timer on the Arduino? The watchdog timer automatically resets your sketch unless you tell it everything is working normally. Typically, you initialize the watchdog in your setup function then call the watch dog reset function each time through the loop. You can see an example here: ... -watchdog/

We have run into the communication problem with the serial port too. Sometimes it is caused by large loads on the Arduino (servo, relays). Adding a large capacitor (220uF) across the power supply can help then. In other cases we find no explanation. Some serial ports are more reliable than others. we've had the most trouble with the Uno . We found you can get good results by using an external USB board instead of the USB port on the Arduino and found these are pretty good: ... Wg5Ec-qqkp. You can connect them to TX0 and RX0 You connect the TX pin on the external port to the RXpin on the header; and the RX pin on the external board to the TX pin on the header. This gives reliable comms, but you need to disconnect it for programming unfortunately. If you find a better solution, please post and let everyone know!
Post Reply