Data for plots, tables and monitor visualizers is normally sent periodically. If MegunoLink isn’t connected at the time, data is lost but there are no other consequences. However, property information, such a graph titles, table descriptions or initial values for interface panel controls are usually sent infrequently and perhaps only once. If MegunoLink is not connected when this information is sent the user interface may not behave as expected.
There are several solutions to this problem. The most straight forward is sending initial settings from the Arduino setup()
function and have MegunoLink reset the device whenever it connects. However, it is often helpful if the Arduino sketch does not restart every time a connection is opened. If MegunoLink has just been started, however, it may not have the current settings from your Arduino sketch. In this situation, you could either send the configuration information every few seconds or have MegunoLink send a command when it connects. Your Arduino sketch then replies with its current settings. This article explores these different options.
Reset on connection
Newly created serial connections automatically reset compatible Arduino boards whenever you open the serial connection. This is the same behavior used by the Arduino serial monitor and uses the same mechanism that the Arduino IDE employs to trigger the bootloader and load new programs onto your Arduino.
To make sure MegunoLink is configured to reset your board on connection, click the Advanced button in the connection manager visualizer (when the serial connection is closed) to open the serial port settings. Make sure the Disable DTR and Disable RTS are not checked. Note: the board might not reset the first time you connect after changing these settings (depending on the state of the DTR/RTS pins and board design).
With your Arduino sketch resetting every time MegunoLink connects, all the initial settings can go into the Arduino setup
function, which runs whenever the device is reset.
For example, you might want to set title, axis labels on a graph and initial values of an interface panel control:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include "MegunoLink.h" void setup() { Serial.begin(9600); TimePlot Plot; InterfacePanel Panel; Plot.SetTitle(F("Battery Discharge Test Result")); Plot.SetYLabel(F("Battery Voltage [mV]")); Panel.SetNumber(F("DischargeRate"), 0.1); } |
Send configuration periodically
The downside of resetting the device to send settings to MegunoLink is that you may lose any state information (e.g. filter history, motor location, set-point) that has not been saved to the device eeprom. To avoid this, you could simply put the configuration in its own function and send it periodically. Then MegunoLink will receive the updated settings within a short period, whether the device is reset or not when the connection is opened. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include "MegunoLink.h" #include "ArduinoTimer.h" TimePlot Plot; Table Properties; ArduinoTimer SettingsUpdateTimer; void SendSettings() { Plot.SetTitle(F("Battery Discharge Test Result")); Plot.SetYLabel(F("Battery Voltage [mV]")); Properties.SetDescription(F("Discharge current"), F("Battery discharge current in milliamps")); } void setup() { Serial.begin(9600); // Send settings at the start in case MegunoLink is already // connected and listening. SendSettings(); } void loop() { // Periodically send settings in case MegunoLink wasn't connected // at startup. const int SettingsUpdateInterval = 10; // Seconds if (SettingsUpdateTimer.TimePassed_Seconds(SettingsUpdateInterval)) { SendSettings(); } // etc... } |
This approach works best for settings the user is unlikely to change in MegunoLink. That could include things like graph titles or property descriptions in tables. Sending control values to an interface panel may frustrate the user if the the control values they are updating are overwritten by commands from your sketch.
Send on connect
Sending data periodically can overwrite changes the user makes in MegunoLink, a particular problem for interface panels where continually updating control values from your sketch might prevent the user entering new values.
A SerialConnection component on an interface panel visualizer helps solve this problem. The SerialConnection sends a command shortly after MegunoLink opens a connection. So you can have your Arduino sketch send initial settings to MegunoLink by adding:
- a command in your Arduino sketch (using our command handler, for example) to send initial settings to MegunoLink, and
- an interface panel with a SerialConnection component in your MegunoLink project that sends the initial settings command.
Here’s a simple example that configures a time-plot and sends the current plot data update interval to an interface panel control. The settings are updated at startup and when a !SendConfig\r\n
command is received. A SerialConnection on the interface panel sends !SendConfig\r\n
whenever the serial connection is opened. The configuration is also sent in the setup()
function. So updated settings will be sent to MegunoLink if the Arduino is reset while the serial connection is opened.
Select Library Examples → InitializingSettings in MegunoLink to open this example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* ****************************************************************** * Example illustrating how to initialize graph appearance and * interface panel controls with current configuration data when * a serial connection is opened. */ #include "MegunoLink.h" #include "ArduinoTimer.h" #include "CommandHandler.h" // To process serial commands. CommandHandler<> SerialCmds; // For communicating with plot and interface panel visualizers. TimePlot Plot; InterfacePanel Panel; // To periodically send measurements. ArduinoTimer SendMeasurementsTimer; unsigned long UpdateInterval = 100; // Command handler to send settings when the 'SendConfig' // command is received. void Cmd_SendConfiguration(CommandParameter& p) { SendConfiguration(); } // Send current configuration to MegunoLink. void SendConfiguration() { Plot.SetTitle(F("My data")); Plot.SetXLabel(F("Measurement time")); Plot.SetYLabel(F("Voltage")); Panel.SetNumber(F("DataRate"), UpdateInterval); } // Setup communications and commands. void setup() { Serial.begin(9600); SerialCmds.AddCommand(F("SendConfig"), Cmd_SendConfiguration); SerialCmds.AddVariable(F("DataRate"), UpdateInterval); SendConfiguration(); } // Main loop. void loop() { // Receive and dispatch serial commands. SerialCmds.Process(); // Periodically send measurements to MegunoLink. if (SendMeasurementsTimer.TimePassed_Milliseconds(UpdateInterval)) { Plot.SendData(F("Battery"), analogRead(0)); } } |