Software
Crestron
SIMPL+
Notice that a
do-until
loop was used in the example above. Every time a change
event is triggered for the
data_in
buffer, it is uncertain that only one character has
been inserted. In fact, many characters may have been added since the last change
event. Due to this possibility, continue to pull characters out of the buffer with
getc
until the buffer is empty, which is what the expression (len(data_in) = 0) reveals.
Also notice from the example that the extracted character is stored into an integer.
This is because
getc
returns the ASCII value of the character, which is an integer. On
the next line, the
chr
function is used to convert that value into a one-byte string,
which can be added to temp.
Although this example should work for real-world applications, there is a potential
problem should multiple lines of data come in on the same logic wave. Should this
happen, only the last complete line is stored into
line
and the rest is lost. To account
for this, make
line
into a string array and store each subsequent line into a different
array element. Another possibility is that any code that is needed to further act upon
the data could be built directly into this loop. Thus removing the need to store more
than one line of data.
Once the data has been removed from the buffer and stored in a known format (in
this case, one complete command from the device), the desired data can be extracted.
Using the example above where the data was coming from a CD jukebox, the
following example could be used to extract the artist, track, and album title.
BUFFER_INPUT jukebox[100];
STRING_OUTPUT artist, track, album;
INTEGER startPos;
STRING searchStr[20], tempStr[100];
CHANGE jukebox
{
do
{
tempStr = t chr(getc(jukebox));
if ( right(tempStr,1) = "\r" )
{
searchStr = "Artist=";
startPos = Find(searchStr,tempStr);
if (startPos) { // was the string found?
startPos = st len(searchStr);
artist = mid(tempStr, startpos,
Find(",",tempStr,startpos) - startpos);
searchStr = "Track=";
startpos = Find(searchStr,tempStr) + len(searchStr);
track = mid(tempStr, startpos,
Find("\r",tempStr,startpos) - startpos);
searchStr = "Album=";
startpos = Find(searchStr,tempStr) + len(searchStr);
album = mid(tempStr, startpos,
Find("\r",tempStr,startpos) - startpos);
tempStr = "";
}
}
} until (len(jukebox) = 0);
}
Function Main()
{
48
•
SIMPL+
Programming Guide – DOC. 5789A