⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controlclass.~pas

📁 使用Delphi 6.0开发用于控制空调的程序
💻 ~PAS
字号:
unit ControlClass;

interface

uses
  Messages, SysUtils, Variants, Classes, Dialogs, Controls,StrUtils ;

type TControlCommand = class
protected
  strCommand:string;
  //b4,b3,b2,b1:Byte;
  Len:Integer;
  strErr:string;
public
  // b4,b3,b2,b1:Byte;
  bBuffer:array of Byte;

  //function ASCII(b:Byte):Word;
  constructor Create(l:Integer);
  procedure CollectionRepresentative(strAdd: string);      //收集数据的代表数据 CORP
  procedure CollectionOperationState(strAdd: string);   //收集运转状态数据   COOPS
  procedure CollectionWarmingState(strAdd:string);      //收集警告状态数据   COWS
  procedure RemoteControl(strAdd,strCommType:string);    //远程控制运转、停止操作  RC
  procedure GetAccessParametes(strAdd:string);        // 取得设定参数     GAP
  procedure AccessParametes(strAdd,strCommType,strDatai:string);           //设定参数         AP
  procedure GetCommProVersionInfo(strAdd:string);    //取得通信协议版本情报  GCPVI
  procedure GetSMAddressInfo( strAdd:string );       //取得SM地址情报    GSMAI
  procedure GetEquipmentAndFactoryInfo( strAdd:string );  //取得设备情报和厂家情报   GEFI
  function GetCommandLines():string;       //获取命令代码
  function CHKSUM(strChk:string):string;
  function GetError():string;

  end;
implementation

{ TControlCommand }


function hex(c:char):Integer;
var
   x:integer;
begin
    if c='' then
       x:=0
    else if(Ord(c)>=ord('0'))and (Ord(c)<=ord('9')) then
        x:=Ord(c)-Ord('0')
    else if(Ord(c)>=ord('a'))and (Ord(c)<=ord('f')) then
        x:=Ord(c)-Ord('a')+10
    else if(Ord(c)>=ord('A'))and (Ord(c)<=ord('F')) then
        x:=Ord(c)-Ord('A')+10
    else
     x:=-1;
    Result:=x;
end;
//该函数结束1~2个字符,转换成功,输出对应十六进制的值,转换失败,输出-1
function HexToInt(S:String):Integer;
var
  tmpInt1,tmpInt2:integer;
begin
    if Length(S)=1 then
    begin
       Result:=hex(S[1]);
    end
    else if Length(S)=2 then
    begin
      tmpInt1:=hex(S[1]);
      tmpInt2:=hex(S[2]);
      if(tmpInt1=-1) or (tmpInt2=-1) then
        Result:=-1
      else
        Result:=tmpInt1*16+tmpInt2;
    end
    else
    Result:=-1;
end;

constructor TControlCommand.Create(l:Integer);
begin
   SetLength(bBuffer,l);
   Len := l;
   strCommand := '';
   strErr := '正常';
end;

function TControlCommand.CHKSUM(strChk:string):string;
var
  i:Integer;
  sum,WLen:Word;
  strSum,strReturn:string;
begin
     sum := 0;
     ShowMessage(strChk);
     for i := 1 to Length(strChk) do
     begin
       sum := sum + Ord(strChk[i]) ;
       // sum := sum + bBuffer[i];
     end;
     //有可能时取反加一
     sum := sum xor $FFFF + 1;
     strSum := IntToHex(sum,4);
     ShowMessage('校验CHKSUM:'+strSum);
     WLen := Length(strSum);
     if WLen = 4 then
        strReturn := strSum
     else
     if WLen = 3 then
        strReturn := '0'+ strSum
     else
     if WLen = 2 then
        strReturn := '00'+strSum
     else
     if WLen = 1 then
        strReturn := '000'+strSum
     else
     if  WLen = 0 then
        strReturn := '0000';
      Result := strReturn;   
end;

procedure TControlCommand.CollectionRepresentative(strAdd: string);
var
  //strTemp:string;
  //i :Integer;
  strChksum : string;
begin
     SetLength(bBuffer,18);
     strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '收集装置的代表数据时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60420000';
     strChksum := CHKSUM(Copy(strCommand,2,Length(strCommand)-1));
     strCommand := strCommand + strChksum + #13;
end;
procedure TControlCommand.AccessParametes(strAdd,strCommType,strDatai:string);
var
 // i:Integer;
  strSum:string;
begin
    SetLength(bBuffer,24);
     //strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
   strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '设定参数时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '6049A006';
     if Length(strCommType) <> 2 then
     begin
       strErr := '设定参数时:指定参数错误!';
       Exit;
     end;
     if Length(strDatai) <> 4 then
     begin
       strErr := '设定参数时:设定数值错误!';
       Exit;
     end;
     if (strCommType <> '81') and (strCommType <> '82') and
        (strCommType <> '83') and (strCommType <> '84') and
        (strCommType <> '85') and (strCommType <> '80') then
     begin
       strErr := '设定参数时:参数错误!应该为:80——85,而不是:'+strCommType;
       Exit;
     end;  

     strCommand := strCommand + strCommType + strDatai;
     strSum := CHKSUM(Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strSum + #13;
end;

procedure TControlCommand.CollectionOperationState(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
    // strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0403'+'00000000'+'00000000'+'0D';
     //        起始位  versiosn address   C1D1   C1D2    LCHKSUM    CHKSUM   结束符
    //SetLength(bBuffer,18);
     strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '收集运转状态数据时:地址长度错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60430000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;
end;



procedure TControlCommand.CollectionWarmingState(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
    // strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0404'+'00000000'+'00000000'+'0D';
     //        起始位  versiosn address   C1D1   C1D2    LCHKSUM    CHKSUM   结束符
     strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '收集警告状态数据时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60440000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;

end;



procedure TControlCommand.GetAccessParametes(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
    // strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
    strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '获取设定参数时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60470000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;
end;

function TControlCommand.GetCommandLines: string;
begin
    Result := strCommand;
end;

procedure TControlCommand.GetCommProVersionInfo(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
    // strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
    strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '获取协议版本信息时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '604F0000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;
end;

procedure TControlCommand.GetEquipmentAndFactoryInfo(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
    // strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
   strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '获取设备和厂家信息时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60510000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;

end;

procedure TControlCommand.GetSMAddressInfo(strAdd: string);
var
  strChksum:string;
begin
    SetLength(bBuffer,18);
   //  strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
    strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '获取SM地址信息时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '60500000';
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;


end;

procedure TControlCommand.RemoteControl(strAdd, strCommType: string);
var
   strChksum:string;
begin
     SetLength(bBuffer,20);
     strCommand :='7E'+'0200'+'00'+strAdd+'0600'+'0402'+'00000000'+'00000000'+'0D';
    //        起始位  versiosn address   C1D1   C1D2    LCHKSUM    CHKSUM   结束符
     strCommand :='~20';
     if Length(strAdd) <> 2 then
     begin
       strErr := '远程控制时:地址错误';
       Exit;
     end
     else
         strCommand := strCommand + strAdd;
     strCommand := strCommand + '6045E002';
     if Length(strCommType) <> 2 then
     begin
       strErr := '远程控制时:命令错误,应为:10,11,1F,而不是:'+strCommType;
       Exit;
     end ;
     if (strCommType <>'10') and (strCommType <>'11') and (strCommType <>'1F') then
     begin
         strErr := '远程控制时:命令错误,应为:10,11,1F,而不是:'+strCommType;
         Exit;
     end;

     strCommand := strCommand + strCommType;
     strChksum := CHKSUM( Copy(strCommand,2,(Length(strCommand)-1) ) );
     strCommand := strCommand + strChksum + #13;       
end;



function TControlCommand.GetError: string;
begin
    Result := strErr;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -