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

📄 controlclass.~pas

📁 求是科技出版的《Delphi串口通信工程开发实例导航》所有的源代码。是一本很好的书。拿出来与大家共享。
💻 ~PAS
📖 第 1 页 / 共 4 页
字号:
        end;
        Inc(mCounter);
    end;

    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -1;
    end else
    begin
        //解析
        ACardUse.Card := mBottomMsg.GetPosStr(10)
                        +mBottomMsg.GetPosStr(11)
                        +mBottomMsg.GetPosStr(12)
                        +mBottomMsg.GetPosStr(13);
        ACardUse.EnterState := mBottomMsg.GetPosByte(14);

        try
          //解析时间
          Year1 := StrToInt(mBottomMsg.GetPosStr(15));
          Year2 := StrToInt(mBottomMsg.GetPosStr(16));
          Month := StrToInt(mBottomMsg.GetPosStr(17));
          Day := StrToInt(mBottomMsg.GetPosStr(18));
          Hour := StrToInt(mBottomMsg.GetPosStr(19));
          Minute := StrToInt(mBottomMsg.GetPosStr(20));
          Second := StrToInt(mBottomMsg.GetPosStr(21));
        except
            Result := -101;
            Exit;
        end;
        ACardUse.EnterTime := EncodeDate(Year1*100+Year2,Month,Day)
                           + EncodeTime(Hour,Minute,Second,0);//微秒为零。

        ACardUse.RecordNo := mBottomMsg.GetPosByte(22);
        mStr := mBottomMsg.GetPosStr(9);
        if mStr = '00' then Result := 1
        else if mStr = '01' then Result := -1
        else if mStr = '04' then Result := -4
        else if mStr = '06' then Result := -6
        else if mStr = '07' then Result := -7
        else if mStr = '09' then Result := -9
        else Result := -1;
    end;
    //释放资源
    mCommand.Free;
    mBottomMsg.Free;

end;

procedure TControlMachine.SetAddress(Address: string);
begin
    FAddr1 := Copy(Address,1,2);
    FAddr2 := Copy(Address,3,2);
end;

function TControlMachine.ShakeHands: TBottomMsg;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    mBuf: array of byte;
begin
    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := nil;
        Exit;
    end;
    //构造上位机报文
    mCommand := TCommand.Create('B3B3B3 04 FB XXXX 10 XX');
    //构造下位机应答信息类,准备接收下位机消息
    mBottomMsg := TBottomMsg.Create();
    //替换地址参数
    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址
    //通信次数计数器
    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        //输出上位机报文
        FMSComm.Output := mCommand.FBuffer;
        //等待下位机响应
        Sleep(WAIT_TIME);
        //检查上传报文长度
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            mBuf := FMSComm.Input;
            //设置下位机应答信息类分析
            mBottomMsg.SetBuffer(mBuf);
            //分析应答数据
            if mBottomMsg.GetPosStr(9) = '00' then break;
        end;
        Inc(mCounter);
    end;
    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := nil ;
        mBottomMsg.Free;
    end
    else begin
        Result := mBottomMsg;
    end;

    mCommand.Free;
    //需要返回该对象,所以不能使用Free函数
    //mBottomMsg.Free;
end;

function TControlMachine.UpdateFixCardUser(ACommandNo:integer;
       ACardNo:string): integer;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    mBuf: array of byte;
    mCardNoHard : TCardNoHard;
    mStr : string;
begin


    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
    
    mCommand := TCommand.Create('B3B3B3 09 F6 XXXX 17 XX XXXX XXXX XX');
    mBottomMsg := TBottomMsg.Create();

    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址

    case ACommandNo of
        UPDATE_CLAIM_LOSE : mCommand.ParamByPos(3,'01');//;
        UPDATE_NOT_CLAIM_LOSE : mCommand.ParamByPos(3,'02');
        UPDATE_ALLOW_IN : mCommand.ParamByPos(3,'03');
        UPDATE_ALLOW_OUT : mCommand.ParamByPos(3,'04');
        else mCommand.ParamByPos(3,'FE');
    end;

    mCardNoHard := CardNo_SoftToHard(ACardNo);
    mCommand.ParamByPos(4,mCardNoHard.N1);//
    mCommand.ParamByPos(5,mCardNoHard.N2);//
    mCommand.ParamByPos(6,mCardNoHard.N3);//
    mCommand.ParamByPos(7,mCardNoHard.N4);//

    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        FMSComm.Output := mCommand.FBuffer;
        Sleep(WAIT_TIME);
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            mBuf := FMSComm.Input;
            mBottomMsg.SetBuffer(mBuf);
            break;
        end;
        Inc(mCounter);
    end;
    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -100 ;
    end
    else begin
        //分析返回数据包
        mStr := mBottomMsg.GetPosStr(9);
        if mStr = '00' then  Result := 1
        else if mStr = '01' then Result := -1 //无效命令
        else if mStr = '04' then Result := -4 //校验错
        else if mStr = '03' then Result := -3 //卡号不在名单内
        else if mStr = '09' then Result := -9 //卡类型错
        else Result := -100;
    end;

    mCommand.Free;
    mBottomMsg.Free;

end;

function TControlMachine.WriteCasualCardNo(ACardNo: string): integer;
begin

    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
end;

function TControlMachine.WriteFestival(ACommandNo: integer;
  ADate: TDateTime; ARecordNo: integer): integer;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    Year1,Year2,Month,Day : word;
    mBuf: array of byte;
    mStr : string;
begin

    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
    
    mCommand := TCommand.Create('B3B3B3 0A F5 XXXX 14 XX XXXX XXXX XX XX');
    mBottomMsg := TBottomMsg.Create();

    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址

    DecodeDate(ADate,Year1,Month,Day);
    Year2 := Year1 mod 100;
    Year1 := Year1 div 100;

    case ACommandNo of
        FESTIVAL_START: mCommand.ParamByPos(3,'01');//副命令
        FESTIVAL_APPEND: mCommand.ParamByPos(3,'02');//副命令
        FESTIVAL_END: mCommand.ParamByPos(3,'03');//副命令
    end;

    mCommand.ParamByPos(4,GetBCD(Year1));//
    mCommand.ParamByPos(5,GetBCD(Year2));//
    mCommand.ParamByPos(6,GetBCD(Month));//
    mCommand.ParamByPos(7,GetBCD(Day));//

    mCommand.ParamByPos(8,IntToHex(ARecordNo,2));//记录号

    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        FMSComm.Output := mCommand.FBuffer;
        Sleep(WAIT_TIME);
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            mBuf := FMSComm.Input;
            mBottomMsg.SetBuffer(mBuf);
            break;
        end;
        Inc(mCounter);
    end;

    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -100 ;  //放弃通信
    end
    else begin
        //分析应答数据
        mStr :=  mBottomMsg.GetPosStr(9);
        if mStr = '00' then  Result := 1
        else if mStr = '04' then Result := -4 //校验错误
        else if mStr = '05' then Result := -5
        else if mStr = '01' then Result := -1
        else if mStr = '06' then Result := -6
        else Result := -100;

    end;

    mCommand.Free;
    mBottomMsg.Free;
end;

//函数没有完成
function TControlMachine.WriteFixCardUser(ACommandNo: integer;
  ACardNo: string; ACardState: integer; ADateTime: TDateTime;
  ARecordNo: byte): integer;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    mBuf: array of byte;
    mCardNoHard:TCardNoHard;
    Year1,Year2,Month,Day,Hour,Minute,Second : word;
begin

    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
    //构造上位机报文
    mCommand := TCommand.Create('B3B3B3 0F F0 XXXX 16 XX XXXXXXXX XX XXXXXXXX XX XX');
    //构造下位机应答信息类,准备接收下位机消息
    mBottomMsg := TBottomMsg.Create();
    //替换地址参数
    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址
    ////替换子命令参数
    case ACommandNo of
    1 :    mCommand.ParamByPos(3,'01');
    2 :    mCommand.ParamByPos(3,'02');
    3 :    mCommand.ParamByPos(3,'03');
    else  mCommand.ParamByPos(3,'00');
    end;
    //卡号转换
    mCardNoHard := CardNo_SoftToHard(ACardNo);
    //替换卡号参数
    mCommand.ParamByPos(4,mCardNoHard.N1);
    mCommand.ParamByPos(5,mCardNoHard.N2);
    mCommand.ParamByPos(6,mCardNoHard.N3);
    mCommand.ParamByPos(7,mCardNoHard.N4);
    //替换状态参数
    mCommand.ParamByPos(8,IntToHex(ACardState,2));

    DecodeDate(ADateTime,Year1,Month,Day);
    Year2 := Year1 mod 100; // 低位
    Year1 := Year1 div 100; //高位
    //替换时间参数
    mCommand.ParamByPos(9,GetBCD(Year1));
    mCommand.ParamByPos(10,GetBCD(Year2));
    mCommand.ParamByPos(11,GetBCD(Month));
    mCommand.ParamByPos(12,GetBCD(Day));
    //替换记录号参数
    mCommand.ParamByPos(13,IntToHex(ARecordNo,2));
    //通信次数计数器
    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        //输出上位机报文
        FMSComm.Output := mCommand.FBuffer;
        //等待下位机响应
        Sleep(WAIT_TIME);
        //检查上传报文长度
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            //读取上传报文
            mBuf := FMSComm.Input;
            //设置下位机应答信息类分析
            mBottomMsg.SetBuffer(mBuf);
            //分析应答数据
            if mBottomMsg.GetPosStr(9) <> '04' then break;
        end;
        Inc(mCounter);
    end;
    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -4;
    end
    else begin
        Result := 1;
    end;

    mCommand.Free;
    mBottomMsg.Free;
end;

//照明灯自动开关时间
function TControlMachine.WriteLightAutoTime(AOnHour, AOnMinute, AOffHour,
  AOffMinute: integer): integer;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    mBuf: array of byte;
begin

    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
    //构造上位机报文
    mCommand := TCommand.Create('B3B3B3 08 F7 XXXX 1E XXXX XXXX XX');
    mBottomMsg := TBottomMsg.Create();
    //替换地址参数
    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址
    //替换时间参数
    mCommand.ParamByPos(3,IntToHex(AOnHour,2));
    mCommand.ParamByPos(4,IntToHex(AOnMinute,2));
    mCommand.ParamByPos(5,IntToHex(AOffHour,2));
    mCommand.ParamByPos(6,IntToHex(AOffMinute,2));
    //通信次数计数器
    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        //输出上位机报文
        FMSComm.Output := mCommand.FBuffer;
        //等待下位机响应
        Sleep(WAIT_TIME);
        //检查上传报文长度
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            //读取上传报文
            mBuf := FMSComm.Input;
            //设置下位机应答信息类分析
            mBottomMsg.SetBuffer(mBuf);
            //分析应答数据
            if mBottomMsg.GetPosStr(9) = '00' then break;
        end;
        Inc(mCounter);
    end;
    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -4 ;
    end
    else begin
        Result := 1;
    end;

    mCommand.Free;
    mBottomMsg.Free;
end;

function TControlMachine.WritePictureTime(
  AOneTenthSecond: integer): integer;
var
    mCounter : integer;
    mCommand : TCommand;
    mBottomMsg : TBottomMsg;
    mBuf: array of byte;
begin

    //端口没有打开
    if not FMSComm.PortOpen then
    begin
        Result := -100;
        Exit;
    end;
    
    mCommand := TCommand.Create('B3B3B3 05 FA XXXX 1E XX XX');
    mBottomMsg := TBottomMsg.Create();

    mCommand.ParamByPos(1,FAddr1);//地址
    mCommand.ParamByPos(2,FAddr2);//地址

    mCommand.ParamByPos(3,IntToHex(AOneTenthSecond,2));

    mCounter := 0 ;
    while mCounter <  TRY_TIMES do
    begin
        FMSComm.Output := mCommand.FBuffer;
        Sleep(WAIT_TIME);
        if FMSComm.InBufferCount >= 10 then
        begin
            //将OLE变量付值给树组
            SetLength(mBuf,FMSComm.InBufferCount);
            mBuf := FMSComm.Input;
            mBottomMsg.SetBuffer(mBuf);
            //分析应答数据
            if mBottomMsg.GetPosStr(9) = '00' then break;
        end;
        Inc(mCounter);
    end;
    //放弃通信
    if mCounter >= TRY_TIMES then
    begin
        Result := -4;
    end
    else begin
        Result := 1;
    end;

    mCommand.Free;
    mBottomMsg.Free;
end;

{ TCardReader }

function TCardReader.CardNo_HardToSoft(ACardNoHard: TCardNoHard): string;
var
    mCard: integer;
begin
    Result := ACardNoHard.N1
             +ACardNoHard.N2
             +ACardNoHard.N3
             +ACardNoHard.N4;

⌨️ 快捷键说明

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