Using variables in messages – Wavetronix Click 500 (programmable controller) (CLK-500) - Developer Guide User Manual
Page 83
82
CHAPTER 8 • SERIAL MESSAGE SUPPORT
The current implementation supports 10 checksums, denoted by the CSUM tag, and refer-
enced by the index inside the bracket following the CSUM tag (e.g. 0-9). This number is an
index into the checksum table.
If any of the control characters are invoked without a size specifier before them, the size is
set to one. Therefore, %CSUM[x] defaults to a one byte checksum value and %c[password]
defaults to a one character string.
Using Variables in Messages
There are two functions that can be used to manage message variables: ClickMsgWriteVar
and ClickMsgReadVar. The ClickMsgWriteVar function sets the value of a variable used in
a message. Its first parameter is a string, which is the variable name, and the second param-
eter is a pointer to the value to be copied into the message variable.
How the second parameter is used depends on the type of the variable. If the variable is a
decimal or a hexadecimal value transferred over the communication link, the address of a
variable containing the value to be set is passed to the function. However if the second pa-
rameter is an array of byte-sized variables, the address of the first byte of the array is passed
to the function.
Warning
If you are using ClcikMsgReadVar and ClickMsgWriteVar to read and write a single
variable, that variable must be an unsigned 32-bit data type. Otherwise a run time
error may occur. The exception to this rule is if you use these functions to read and
write a variable array (%B) with only a one-byte length.
The following code snippet takes data from a ClickUtilTime structure and writes it to Click
message variable space using the function ClickMsgWriteVar. Notice how each variable
needs to be type cast to an unsigned long.
The preceding BuildTimeResponseMsg function is used in the following callback SetTime
function. This message first uses the ClickMsgReadVar function to extract the variables
populated when the associated ClickMsgString is received. The real-time clock is then set
to this date and time, and then the time message is echoed back over the virtual port to the
set time requestor.