Controlling Serial Devices [CIS Example]

Discussion in 'Pascal Logic Code Examples' started by Darren, Oct 4, 2005.

  1. Darren

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    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 :
    Code:
    { 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 :
    Code:
    { 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;
    
     
    Darren, Oct 4, 2005
    #1
  2. Darren

    justinw

    Joined:
    Nov 3, 2004
    Messages:
    21
    Likes Received:
    0
    Location:
    christchurch
    Q

    hello darren,
    what does the q stand for and is it required to communicate to the thermostat (or whatever)
    cheers justinw
     
    justinw, Nov 13, 2005
    #2
  3. Darren

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    The "q" doesn't necessarily stand for anything in particular. This is a purely hypothetical case to demonstrate how to build up and send serial strings and how to receive serial strings and pull out the relevant bits.
     
    Darren, Nov 14, 2005
    #3
  4. Darren

    muppets

    Joined:
    Oct 26, 2007
    Messages:
    98
    Likes Received:
    0
    Is this the standard system used by a/c rs232 devices? I have a module I am installing that uses the exact same syntax - (accent air system interface module).

    Your example code will be invaluable when it comes time to code for it.


    Cheers.
     
    muppets, Oct 28, 2007
    #4
  5. Darren

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    This is not based on any particular standard. You will need to ensure that the protocol used by your system is identical before using the code.
     
    Darren, Oct 29, 2007
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.