The Message
class provides methods to send data and commands to message monitor and message logger visualizers. Message monitors write data to a text window while message loggers write data to a disk file. Message
provides fine control over the data written. Refer to the raw monitor and logging visualizers for visualizers that write all data received to a text window or log file respectively.
To use message functions:
- Add
#include "MegunoLink.h"
to the top of your Arduino program - Create a
Message
variable. - Call methods on the
Message
variable such asSendData
.
Data sent with the Message
class will be received by both message monitor and message logger visualizers. Use message channels to send separate data to different visualizer windows.
Message Library Example
This simple example sends the millis()
time and two ADC measurements. The comma-separated data starts with the Begin()
and finishes with the End()
method. Between these two calls, standard Serial.print(…)
methods add content to the message that will be sent. The numbers are all separated by commas and could be easily pasted into Excel.
Every 100 measurements it sends a separator using the Send(…)
method. For single values with labels, you don’t need to use the Begin()
and End()
methods.
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 |
#include "MegunoLink.h" Message MyCSVMessage; int MessagesSent = 0; void setup() { Serial.begin(9600); } void loop() { MyCSVMessage.Begin(); Serial.print(millis()); Serial.print(","); Serial.print(analogRead(A0)); Serial.print(","); Serial.print(analogRead(A1)); MyCSVMessage.End(); ++MessagesSent; if (MessagesSent % 100 == 0) { MyCSVMessage.Send("*** sent", MessagesSent); } delay(100); } |
Methods
Message
methods are:
- Message(MessageDestination — optional, ChannelName — optional, SerialPort — optional)
- Send(Value)
- Send(Label, Value)
- Begin()
- End()
- Clear() — message monitor only
- LogTo(Filename) — message logger only
- StartLogging() — message logger only
- StopLogging() — message logger only
- Flush() — message logger only
Detailed Descriptions
The parameters for each method are described below. Optional parameters are surrounded by []
‘s. Text parameters support in memory strings (const char*
) and flash memory strings (F(…)
) where available.
Message constructor
Creates a Message
variable, which can be used to send data and control message monitor and message logger visualizers.
Message([MessageDestination], [ChannelName], [Destination])
Name | Type | Required? | Description |
---|---|---|---|
MessageDestination |
MessageDestination |
No | Applies to message monitor visualizer only. One of Message::Text , Message::Speak , or Message::TextAndSpeak , which tells MegunoLink whether to add the message data to the text buffer or speak it using the text-to-speech engine, or both. Defaults to Message::Text . |
ChannelName |
Text |
No | sets the channel used for all methods called on the variable. If missing, data is sent to the default channel |
Destination |
Print |
No | the stream used for all methods called on the variable. If missing, data is sent to the standard Serial port |
1 2 3 4 5 |
#include "MegunoLink.h" Message Msg; // messages are shown in the text buffer of the default channel, sent through Serial. Message Msg(Message::Speak); // messages are spoken on the default channel, sent through Serial. Message Msg(F("Log"), Serial2); // messages are shown in the text buffer of the channel named "Log", sent through Serial2. |
Sending values
Sends a value, with an optional label, to the Message Monitor.
Send(Value)
Send(Label, Value)
Name | Type | Required? | Description |
---|---|---|---|
Value |
TValue |
No | The value to send. Supports any type that can be used in Serial.print(…) . |
Label |
Text |
No | A label for the value. |
1 2 3 4 5 6 |
Message Msg; int Counter = 10; Msg.Send("Hello World!"); Msg.Send(Counter); Msg.Send("Counter", Counter); // written as "Counter: 10" |
Start block
Starts a message block. This method sends the first part of the message command. Content sent to the serial stream before the End() method is called will all be written to the message monitor/ message logger.
Begin()
This method takes no parameters.
1 2 3 4 5 6 7 8 9 10 11 |
Message Msg; int Counter1 = 10; int Counter2 = 20; // Message Monitor will display "Counter 1 = 10, Counter 2 = 20" Msg.Begin(); Serial.print("Counter 1 = "); Serial.print(Counter1); Serial.print(", Counter 2 = "); Serial.print(Counter2); Msg.End(); |
End block
Completes a message block. Any content sent between the Begin()
and End
command will be send to the Message Monitor.
End()
This method takes no parameters.
See start block above for an example.
Clear message buffer
Sends a command to clear the message monitor text window. No effect on the message logger.
Clear()
This method takes no parameters.
1 2 |
Message Msg; Msg.Clear(); |
Set log filename
Changes the log filename and begins logging. The 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. Only applies to the message logger visualizer.
LogTo(Filename)
Name | Type | Required? | Description |
---|---|---|---|
Filename |
Text |
Yes | The name of the file to write the log to. Filename is combined with the BaseFolder and Folder set in the message logger visualizer to set the path where the log file will be written. |
1 2 3 4 5 6 7 8 9 10 |
Message Msg; for(int ExperimentNumber = 1; ExperimentNumber < 5; ++ExperimentNumber) { String ExperimentLogFilename = String("Experiment ") + ExperimentNumber; Msg.LogTo(ExperimentLogFilename); RunExperiment(); } |
Start Logging
Starts writing the serial stream to the selected log file. Only applies to the message logger visualizer. The 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.
StartLogging()
This method takes no parameters.
1 2 3 4 5 6 7 8 |
Message Msg; Msg.StartLogging(); Serial.print("Counter 1 = "); Serial.print(Counter1); Serial.print(", Counter 2 = "); Serial.print(Counter2); Msg.StopLogging(); |
Stop Logging
Stops writing the serial stream to the selected log file. Only applies to the message logger visualizer. The 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.
StopLogging()
This method takes no parameters.
See start logging above for an example.
Flush log
Writes any log content buffered in memory to the log file. Only applies to the message logger visualizer. The Flush
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.
Flush()
This method takes no parameters.
1 2 3 4 5 6 7 8 |
Message Msg; Msg.StartLogging(); Serial.print("Counter 1 = "); Serial.print(Counter1); Serial.print(", Counter 2 = "); Serial.print(Counter2); Msg.Flush(); |