Use the SerialConnection
component to automatically send a message to your Arduino sketch when MegunoLink connects. This is commonly used to send configuration information to your Arduino program from controls on an interface panel or to instruct your Arduino sketch to begin sending data to MegunoLink.
To send a message automatically when you open a serial connection:
-
- create an interface panel and add controls for the user to enter the parameters you wish to send,
- add a
SerialConnection
component to the interface panel, - set the
SerialConnection
‘sOnConnectSend
property to the message MegunoLink should send when the connection is opened, - set the
Delay
property, if necessary, to provide time for your Arduino program to get ready (some devices may be reset when a connection is opened).
Set up the Serial Connection Component
Add a SerialConnection
component to your interface panel by dragging it from the toolbox onto the design surface in the interface panel designer.
The SerialConnection
components lives in the component tray at the bottom of the design surface. Unlike buttons and textboxes, it is not visible to the user at run-time.
Create a Message
Select the SerialConnection
component to view its properties. Set the OnConnectSend
to the message that MegunoLink should send when a connection to the device is opened. A typical message could include a command name and parameters obtained from controls on the interface panel, such as: !SetParameters [numOnTime.Value] [numOffTime.Value]\r\n
.
Type the message directly into the property grid or click the ellipsis () to open the message editor window.
Implement a command handler in your Arduino sketch to take an appropriate action when the message is received.
Set a Delay
If your command is not received reliably by your Arduino device it may not be ready to process commands as soon as the connection is opened. Many devices are reset, for example, when a serial connection is opened and are not ready to process commands until the bootloader times out and the setup()
function is complete.
Set the Delay
property on the SerialConnection
component using the property editor in the interface panel designer. MegunoLink will wait until the delay has passed after the connection has been opened before sending the message. A delay of 1000 – 3000 ms (1 – 3 seconds) is usually sufficient.