Custom Messages
Custom messages can be sent through the scripting API to send messages that do not conform to the standard Sym3messages structures. The data field is defined as an ASCII array.
Sending
You can send custom messages using the function SendMessage
Receiving
To receive a message, set the option ‘Use Custom Message Protocol’ in the ‘Communication’ section of the Device properties and define a handler script to process any message received.
void $HandlerScript(String device, array data);
Parameters
Name | Type | Description |
---|---|---|
device | String | The name of the device that received this message |
data | Array | ASCII array containing the message received |
Return Value
None
Example
function MC_MessageProcessor1(device ,data) {
LogDebug("Received message from '" + device + "' data='" + ArrayToString(data) + "'");
}
Useful functions
Here are two functions that can be used to convert a string to an array of ASCII values and back to a string:
// Convert a string to an array of ASCII value
// example: "ABC" will return [65, 66, 67]
function StringToArray(str) {
var arr = str.split('');
var result = new Array();
for (var i = 0; i < arr.length; i++) {
var ascii = arr[i].charCodeAt(0);
result.push(ascii);
}
return result;
}
// Convert an array of ASCII value to a string
// example: [65, 66, 67] will return "ABC"
function ArrayToString(data) {
var result = "";
for (var i = 0; i < data.length; i++) {
var ascii = data[i];
result = result + String.fromCharCode(ascii);
}
return result;
}