Darren
04 Oct 05, 02:32 PM
In this example, we are going to show how to interact with a hypothetical thermostat via a serial (RS232) interface.
CONTROL
The format of the control string is :
Qa,z,n,s<cr>
Where :
'a' is the board address
'z' is the selected zone
'n' is the desired ON/OFF state (OFF = 0, ON = 1)
's' is the desired setpoint in degrees C
<cr> = Carriage Return (ASCII #13)
For example, the command "Q1,2,1,30<cr>" would set board 1, zone 2 to ON at 30C.
The user can set the set point via a System IO variable (called "Set Point"). We want to write some code to transmit the new set point each time it changes. An example of code to do this is :
{ variables section }
s : string;
LastSetPoint : integer;
{ initialisation section }
OpenSerial(2, 2, 9600, 8, 1, 0, 0);
LastSetPoint := 0;
{ module }
{ send new set point if it has changed }
if (GetIntSystemIO("Set Point") <> LastSetPoint) then
begin
{ prepare the control string }
format(s, 'Q1,2,1,',GetIntSystemIO("Set Point"):0, #13);
{ send the string }
WriteSerial(2, s);
{ reset the LastSetPoint variable }
LastSetPoint := GetIntSystemIO("Set Point");
end;
REPLY
The format of the reply string from the thermostat is :
Ra,z,n,s,t<cr>
Where :
'a','z','n','s' are the current address, zone, on/off and set point as above
't' is the current zone temperature
For example, the reply "R1,2,1,30,20<cr>" would mean than board 1, zone 2 is on, with a set point of 30 degrees and a current temperature of 20 degrees.
We want to extract the current zone temperature from the reply and display it on the screen. The temperature is the last number in the list, so the process to extract the number is as follows :
1. Find the last comma in the reply string
2. Copy everything after the comma to another string
3. Extract the number from this string
4. Display this number
Some code to do this is :
{ variables section }
s, TemperatureString : string;
i, Temperature, LastCommaPosition : integer;
{ module }
ReadSerial(2, s, #13);
if s <> '' then
begin
{ find the position of the last comma in the string }
for i := 1 to length(s) do
begin
if s[i] = ',' then
LastCommaPosition := i;
end;
{ copy the text from the character after the last comma }
Copy(TemperatureString, s, LastCommaPosition + 1, 3);
{ extract the temperature from the string }
Temperature := StringToInt(TemperatureString);
{ display the data }
format(s, 'Current Temp = ', Temperature:3, 'C');
TextPos(300, 100);
DrawText(s);
end;
CONTROL
The format of the control string is :
Qa,z,n,s<cr>
Where :
'a' is the board address
'z' is the selected zone
'n' is the desired ON/OFF state (OFF = 0, ON = 1)
's' is the desired setpoint in degrees C
<cr> = Carriage Return (ASCII #13)
For example, the command "Q1,2,1,30<cr>" would set board 1, zone 2 to ON at 30C.
The user can set the set point via a System IO variable (called "Set Point"). We want to write some code to transmit the new set point each time it changes. An example of code to do this is :
{ variables section }
s : string;
LastSetPoint : integer;
{ initialisation section }
OpenSerial(2, 2, 9600, 8, 1, 0, 0);
LastSetPoint := 0;
{ module }
{ send new set point if it has changed }
if (GetIntSystemIO("Set Point") <> LastSetPoint) then
begin
{ prepare the control string }
format(s, 'Q1,2,1,',GetIntSystemIO("Set Point"):0, #13);
{ send the string }
WriteSerial(2, s);
{ reset the LastSetPoint variable }
LastSetPoint := GetIntSystemIO("Set Point");
end;
REPLY
The format of the reply string from the thermostat is :
Ra,z,n,s,t<cr>
Where :
'a','z','n','s' are the current address, zone, on/off and set point as above
't' is the current zone temperature
For example, the reply "R1,2,1,30,20<cr>" would mean than board 1, zone 2 is on, with a set point of 30 degrees and a current temperature of 20 degrees.
We want to extract the current zone temperature from the reply and display it on the screen. The temperature is the last number in the list, so the process to extract the number is as follows :
1. Find the last comma in the reply string
2. Copy everything after the comma to another string
3. Extract the number from this string
4. Display this number
Some code to do this is :
{ variables section }
s, TemperatureString : string;
i, Temperature, LastCommaPosition : integer;
{ module }
ReadSerial(2, s, #13);
if s <> '' then
begin
{ find the position of the last comma in the string }
for i := 1 to length(s) do
begin
if s[i] = ',' then
LastCommaPosition := i;
end;
{ copy the text from the character after the last comma }
Copy(TemperatureString, s, LastCommaPosition + 1, 3);
{ extract the temperature from the string }
Temperature := StringToInt(TemperatureString);
{ display the data }
format(s, 'Current Temp = ', Temperature:3, 'C');
TextPos(300, 100);
DrawText(s);
end;