Many controls let you send serial commands to an Arduino, or other connected device. The commands are sent when the user does something with the control: clicking on a Button, changing a NumericUpDown control or ticking a CheckBox, for example. Each action raises an event which can send a serial message.
Use the Smart tag or Property editor in the Interface Designer to set the command that is sent to a connected device when an event occurs. You can enter the command message directly, or click on the ellipsis button (…) to open the Message Editor window.
Commands are strings built from three things:
- Text: plain text is sent unchanged to your Arduino
- Escape characters: escape characters start with a backslash (
\) and are translated before being sent
- Expressions: expressions contain references to control properties and are evaluated before being sent. Expressions are surrounded by square brackets (
Command strings can be used in any property that sends messages to connected devices. These properties start with the word
On. Properties which send messages include
OnClickSend for button controls,
OnTextChangedSend for text-box controls and
OnValueChangedSend for numeric up/down controls.
|Hello world!||Sends the text
|Hello\r\nWorld||The escape characters
|SetTemp [Temp.Value]||The expression |
The Message Editor window provides an easy way to edit command messages. Open the Message Editor using the ellipsis button (…) found at the end of any message property.
The Message Editor includes:
- a text area for entering commands
- a drop-down menu which lists all the controls and properties available on the Interface Panel. Selecting one of these properties will insert the correct expression into the text area for you to edit
- a Test button which will evaluate any expressions and show you the message that would be sent. You can display the result in ASCII or raw bytes.
Any backslash characters in a command message (but not within expressions) are treated as the start of an escape character. Escape characters are replaced before the message is sent.
|\xXX||0xXX||Hexadecimal literal value|
Expressions in message commands must be surrounded by square-brackets (
[…]). Just before the message is sent, the content of the square brackets is evaluated, converted to a string and inserted into the message. Expressions are evaluated before escape characters so any escape characters generated by the expression will be processed before the message is sent.
Expressions are processed by DynamicExpresso and support a small subset of the C# language.
Referencing Control Properties
The most common use for expressions is including values from control properties. For example, you might want to include the current value of a NumericUpDown control in a message sent when a Button control is clicked.
The syntax to use for runtime properties is:
Any runtime property of a control can be assessed in this way. You can find a list of the runtime properties of a control on its reference page.
Referencing Control Methods
Some controls (such as the Button) support runtime methods. These methods often take some parameters and return a result. A TextBox control, for example, includes a
FixedLengthText(MaxLength) method which limits the length of the text included in the message.
The syntax to use for calling runtime methods is:
ControlName.MethodName(Parameter1, Parameter2, …)
Any runtime method of a control can be accessed in this way. You can find a list of the runtime methods of a control on its reference page.
The expression engine automatically converts expression results to strings before combining them with any other text in the message. However, you can do this conversion yourself if you need more control over the format. For example, to send the value from a numeric control named
Temp in 4 hexadecimal characters you can use:
Tools is a special object that provides a number of utility functions for generating methods.
||Converts a numeric value to literal byte values (
|Choose(int Index, String s0, String s1, …, String s9)||
||Select from a list of strings by index.
Expressions can include mathematical and logical operators. These can be useful for scaling or transforming control values. For example, the expression
[Temp.Value*10] will multiply the value in a control named
Temp by 10. Results from expressions are automatically converted to strings before they are sent.
|+ - !||Addition, subtraction, logical not|
|* / %||Multiplication, division, modulus (remainder)|
|< > <= >=||Less than, greater than, less than or equal, greater than or equal|
|== !=||Equal, not equal|
|&& ||||Logical And, Logical Or|
Literals are basic values: numbers, strings and logical values.
All numbers are 32-bit integers unless they include a decimal point. Numbers that include a decimal point are treated as doubles. Normal C# type promotion rules apply (so an integer multiplied by a double will be promoted to a double).
By default, all numbers are base-10. You can insert hexadecimal literals using the prefix
To create literals of a particular size, use a cast as in
|Int64||8 bytes||Signed, 64-bit integer|
|Int32||4 bytes||Signed, 32-bit integer|
|Int16||2 bytes||Signed, 16-bit integer|
|sbyte||1 bytes||Signed, 8-bit integer|
|UInt64||8 bytes||Unsigned, 64-bit integer|
|UInt32||4 bytes||Unsigned, 32-bit integer|
|UInt16||2 bytes||Unsigned, 16-bit integer|
|byte||1 bytes||Unsigned, 8-bit integer|
|double||8 bytes||Double precision floating point number|
|float||4 bytes||Single precision floating point number|
|char||1 byte||Single character|
|String||—||Sequence of characters|
String literals are surrounded by double quotes (
"string", for example); character literals are surrounded by single quotes (
'a', for example). Both string and character literals can use escape characters.
|\0||null character (
|\a||alert character (
|\b||backspace character (
|\f||form-feed character (
|\n||newline character (
|\r||carriage return character (
|\t||tab character (
|\v||vertical quote character (