User`s guide
Binary and fixed field protocols:
But first, we should talk a little more about binary protocols. The protocols so far
have been primarily ASCII protocols. The only binary information is the carriage
return that marks the end of the line of data. What if, instead of “D3” + chr(13)
to read the 3
rd
channel with an ASCII string response, we had to send the binary
value 15 to trigger a read, and a word representation of the channel, for a total of
3 bytes, and then read a 5 byte response, the command byte (15) plus a 4 digit
double word representation of the number? Well, we’d have to do a few things
differently. We’d still construct our query as a string, but we’d use some different
functions:
private string dataout = chr(15) + chra(From.Word(Channel))
This looks similar to our previous example when we did “D” plus the channel
number in ASCII string form, except this time we use the From.Word() function.
The From. functions are a series of functions that convert numbers into their byte
representation. There are 16 different functions for the various ways a number
can be represented. In this case, we want the word with default LSB first
representation. The From functions return an array of numbers, so to convert it
into a string, we use the chra() function. This is just like the chr() function,
except it takes an array of values and returns a single string made up of the
characters in the array. If you did just chr() with an array of values, it would
return an array of strings.
It is important to understand that just because dataout is a string, it doesn't mean
it can't be a bunch of unreadable binary codes. The string simply provides a
simple way to manipulate these codes. We could do the same thing by creating
an array of all our values, but would have to convert the array to a string to call
any of the communication functions. As it happens, it is sometimes more useful
to do this. It is entirely up to you.
Moving on, the protocol in our example is different from the previous examples
not only because it is entirely binary, but because it is has a fixed length response
and doesn't have an end of line character. The Poll function is written to read to
an end of line character. So, to read a fixed number of characters, we'll need to
modify the Poll function slightly. Fortunately, all we need to do is change one line.
If you click on the Poll function in the protocol editor you will see all the code for
the Poll function. Skipping the details of the code, which is discussed later,
change the one line that says:
in = ReadUntil(until)
to
in = Read(until)
The Read() function takes a single parameter which is the number of characters to
read, so now our Poll() function's second parameter is the number of characters
to read instead of the end of line character. So, the next line of our I/O type
would be:
private string datain = Poll(dataout,5)
The 5 is the number of characters to read. Now all we need to do is parse the
data:
return(To.Long(AscA(Mid(datain,1,4)))
There are a bunch of functions here, so starting from the inside: