User`s guide
Channel.AddValue(strDevice, 0,"Input",0, StrToDouble(Parse(DataIn,0,",")))
Channel.AddValue(strDevice, 0,"Input",1, StrToDouble(Parse(DataIn,1,",")))
Endif
All we added was the Parse() function to split the data apart and then two calls to
AddValue to put the data into channels 0 and 1. The Parse command takes 3
parameters, the string to parse, the index you want to retrieve and the delimiter.
Varying data types:
A slightly more advanced streaming protocol is the NMEA standard often used in
devices like GPS. In this case different lines of data are received with different
meanings. The meaning of a particular line is determined by the first 6 characters.
To parse this, we simply look at these first six characters and put the data in
different places depending on this header’s value. You can delineate different
places using either different I/O types or different device numbers. For example:
if (strIn == chr(10))
// now read until the LF
private string datain = ReadUntil(10)
// split out header
private string head = Left(datain,6)
// now examine each line type differently.
switch
case (head == "$GPRMC")
Channel.AddValue(strDevice, 0, "RMC", 0, Parse(datain,1, ",")
Channel.AddValue(strDevice, 0, "RMC", 1, Parse(datain,2, ",")
case (head == "$GPGSA")
Channel.AddValue(strDevice, 0, "GSA", 0, Parse(datain,1, ",")
Channel.AddValue(strDevice, 0, "GSA", 1, Parse(datain,2, ",")
endswitch
endif
If your data is binary, you may want to review the section on Binary Protocols at
the end of the section on Poll / Response protocols to learn about the To and From
series of functions.
Poll / Response and streaming data:
Some devices perform both poll / response type commands and streaming data.
Typically they start in poll / response mode and a particular command will start it
streaming. The streaming continues until another command is sent to the device.
Handling these types of devices is almost entirely a matter of combining the
concepts described above. The exception is that you have to use a flag to keep
the OnReceive event from parsing data when the device isn’t streaming. To do
this you will first need to create the variable for the flag. For this you can use a
local variable. A local variable is similar to a private variable, but it is viewable by
any code in the protocol. It is not viewable outside the protocol. Typically you
will want to declare your local variables in the OnLoad event. Something like this:
local streamflag = 0
Then, in the OnReceive event, you would enclose your stream parsing code with a
simple if:
if (streamflag)
… parse data, we’re streaming