MegunoLink includes two logging visualizers:
- Log to File: all data received on the serial connection is written to the file,
- Message Logger: data sent using matching message commands are written to the file.
The Log to File visualizer is useful for capturing all of the data sent from your Arduino program. The Message Logger gives your Arduino program more control over what data is written to the log file.
Create a new visualizer by selecting Log to File or Message Logger from the Visualizers menu.
Log File Path
Select the path where MegunoLink should write the log file using the Configure Log Path dialog box. Click the Save button in the visualizer window to open the Configure Log File dialog.
The log file path is made up of 3 parts:
- Base Folder
This lets you select a path that will work even if you use the MegunoLink project on computers that use a different file system layout. The key to these flexible paths is the base folder.
The Base Folder resolves to the actual path of the selected folder when the log file is created. You can choose from:
- None: Treat the Folder as an absolute path. The base folder is ignored.
- Project: The log file is saved under the same folder as the MegunoLink project file.
- Dropbox Folder: The log file is saved under the user’s Dropbox folder.
- Dropbox Personal Folder: The log file is saved under the user’s personal Dropbox folder.
- Dropbox Business Folder: The log file is saved under the user’s business Dropbox folder.
- My Documents: The log file is saved under the user’s documents folder.
- Desktop: The log file is saved under the user’s Desktop folder.
The Folder is appended to the base folder, allowing you to select a folder inside the base folder. Use the drop-down menu to include the date and/or time in the folder name.
The Filename sets the log files name. Use the drop-down menu to include the date and/or time in the filename.
Use the Test button to combine all the parts of the log file name to see where the log file will be saved.
Commands sent by your Arduino program to a Message Logger visualizer can control logging. Your Arduino program can start and stop logging if the Allow serial commands to start and stop logging is checked. If Allow serial commands to set filename is checked your Arduino program can change the filename of the log file. Note: commands only apply to the Message Logger visualizer, not to the Log to File visualizer.
Starting and Stopping Logging
To start logging in MegunoLink, check the Enable logging command on the visualizer toolbar. To stop logging uncheck the Enable logging command.
MegunoLink can also start logging automatically whenever a connection is opened. Simply check the Start logging automatically when connection is opened menu item. This can be combined with the Restore on Load option in the Connection Manager to open a connection and automatically start logging when the MegunoLink project is opened.
If you are using the Message Logger you can also start and stop logging from your Arduino program. Ensure the Allow serial commands to start and stop logging option is checked in the Configure Log Path dialog. Then call the
MessageLogger::StopLogging(); commands. For example:
Adding Time/Date to Log
The time and/or date when each line of data is received can be added to the log file by selecting the corresponding options from the time-stamp menu on the visualizer’s toolbar.
The message logger picks out specially formatted messages from the serial data and writes them to a log file.
By using a message logger, your Arduino program can select which data should be written to the log file. You’ll get a cleaner log containing only the data you need. It won’t get cluttered by other messages like commands for plotting data.
The Message Logger supports MegunoLink’s channel tags. If you want to log only a single channel of messages then choose the channel before enabling the logger.
Sending to the Message Logger
The Message Monitor Arduino Library provides a convenient set of methods for sending data to the Message Logger visualizer. The same library works for the Message Monitor visualizer so you can use the Message Monitor to observe the logged events and the Message Logger to create a permanent record.
In addition to the commands shown in the Message Monitor Library Reference, the Message Logger supports additional commands to control logging:
Message Logger Reference
See also: Message Monitor Library Reference.
Changes the log filename to
Filename and begins logging. The
Filename is combined with the Base Folder and Folder to create a path where the log file will be written.
LogTo command will only be accepted by MegunoLink if the Allow serial commands to start and stop logging and the Allow serial commands to set filename options are enabled in the Configure Log Path dialog.
Sends a command to start the Message Logger writing serial messages to the log file.
StartLogging command will only be accepted by MegunoLink if the Allow serial commands to start and stop logging option is enabled in the Configure Log Path dialog.
Sends a command to stop the Message Logger writing serial messages to the log file.
StopLogging command will only be accepted by MegunoLink if the Allow serial commands to start and stop logging option is enabled in the Configure Log Path dialog.
Examples for the Message Logger can be found in Library Examples⇒MegunoLink⇒MessageLogger in MegunoLink. Each example includes an Arduino code file and a MegunoLink project file. The examples include:
- SendCSVData: Write comma separated values to a log file
- LogToFiles: Uses a serial command to start an experiment that writes 10 measurements to each of 5 separate files.
Send CSV Data Example
SendCSVData example demonstrates sending comma separated values to a Message Monitor or Message Logger. The values are separated from the main serial stream making it easier to load them into another program, such as Microsoft Excel.
Message MyCSVMessage("Data"); //"Data" = the taget message channel (remember to select this in megunolink)
// When logging make sure you specifiy a path for the logfile and enable logging.
// Sends the microcontroller time, and the analog value using a comma separated format
// perfect for use with tools like excel and matlab
Log To Files Example
LogToFiles example demonstrates using the Message Logger to log measurements from the Arduino’s analog input. The program uses a serial command to start an experiment that writes 10 measurements to each of 5 files. The Arduino program sends the data to a Message Logger controlling the log filename.
This example can be found under
MegunoLink Examples⇒Message Logger⇒LogToFiles. It includes a companion MegunoLink project that stores the log files in the My Documents\LogToFile\[Date]\ folder.
int FileNumber = 1;
int MeasurementsSaved = 1;
const int MeasurementsPerFile = 10;
const int NumberOfRuns = 5;
void StartNewLogFile(int LogFileNumber)
FileName += LogFileNumber;
FileName += ".txt";
MeasurementsSaved = 0;
auto Value = analogRead(0);
void Cmd_StartExperiment(CommandParameter &p)
ExperimentRunning = true;
FileNumber = 0;
void Cmd_StopExperiment(CommandParameter &p)
ExperimentRunning = false;
if (MeasurementsSaved == MeasurementsPerFile)
if (FileNumber <= NumberOfRuns)
// Start a new experiment run.
// Stop the experiment when all runs have been completed
ExperimentRunning = false;