![Crestron SIMPL+ Скачать руководство пользователя страница 42](http://html1.mh-extra.com/html/crestron/simpl/simpl_programming-manual_2684744042.webp)
Software
Crestron
SIMPL+
Print(“5 raised to the power of 3 = %d\n”,power(5,3));
x = power(y,z);
As a second example, we shall build a function which appends a simple checksum
byte onto the end of a string. As was mentioned earlier in this manual, checksums are
used by many devices to provide a basic form of error checking. In this example, the
checksum is formed by simply adding up the values of each byte in the command
string and then appending the equivalent ASCII character of the result onto the string
itself. If the checksum value is larger than a single byte (255 decimal), we simply
ignore the overflow and use the lower 8-bits of the result.
DIGITAL_INPUT control_device1, control_device2;
STRING_OUTPUT device1_out, device2_out;
STRING device1_cmd[20], device2_cmd[20], tempCmd[20];
STRING_FUNCTION appendChecksum(STRING command)
{
INTGEGER checksum, i; // define local variables
checksum = 0;
// initialize variable
for (i = 1 to len(command)) // calculate the sum
checksum = ch byte(command,i);
return(c chr(checksum)); //append the byte
}
PUSH vcr_play
{
vcr_out = appendChecksum(“PLAY”);
}
PUSH vcr_stop
{
vcr_out = appendChecksum(“STOP”);
}
In this example, the system function,
byte
, is used inside the function to get the
numeric value of each byte in the string. After the checksum has been calculated, the
chr
function is used to append the corresponding ASCII character to the end of the
command string. Realize that this example is useful for just one (very simple) type of
checksum.
Function Libraries
You are likely to find that the longer you program in SIMPL+ the more you will
need to repeat code you have already written. For example, a function that converts
the temperature from Celsius to Fahrenheit might come in handy in more than one
job.
Clearly, code that has many applications is best placed inside of a function.
Remember, however, that unlike system functions, which are globally available,
user-defined functions are only available inside of the SIMPL+ program in which
they exist. Thus is you need to use a user-defined function in more than one SIMPL+
program, you must copy and paste it from one program to another. While this
technique works, it can lead to problems when, for example, you find a bug in the
function and fix it one program but forget to change it elsewhere.
38
•
SIMPL+
Programming Guide – DOC. 5789A