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

📄 controlclass.pas

📁 求是科技出版的《Delphi串口通信工程开发实例导航》所有的源代码。是一本很好的书。拿出来与大家共享。
💻 PAS
字号:
unit ControlClass;

interface


type TControlMachine = class
public
    BasePort: word ;
    AlertLight : byte;
    AlertSpeaker : byte;
    AlertSensor: byte;
    constructor Create(ABasePort,AlertLight,AlertSpeaker,AlertSensor:Byte);
    function LightCommand(AOnOff:boolean):boolean;
    function SpeakerCommand(AOnOff:boolean):boolean;
    function IsSensorOn(): boolean;
private
    function AsmIn(APort:word) : Byte;
    procedure AsmOut(APort:word;AByte:byte);
    function GetByteByBit(ABit : Byte): Byte;

end;

var
    gControl:TControlMachine;
    
implementation

uses
    sysutils;
const
     PORT_DATA = $0;
     PORT_State = $1;
     PORT_CONTROL = $2;

{ TControlMachine }

function TControlMachine.AsmIn(APort: word): Byte;
begin
    asm
        mov dx,APort
        in al,dx
        mov Result,al
    end;
end;

procedure TControlMachine.AsmOut(APort: word; AByte: byte);
begin
    asm
        mov dx,APort
        mov al,AByte
        out dx,al
    end;
end;

constructor TControlMachine.Create(ABasePort,AlertLight, AlertSpeaker,
  AlertSensor:byte);
begin
    Self.BasePort := ABasePort;
    Self.AlertLight := AlertLight;
    Self.AlertSpeaker := AlertSpeaker;
    Self.AlertSensor := AlertSensor;
end;

function TControlMachine.GetByteByBit(ABit: Byte): Byte;
begin
    if ABit > 7 then raise Exception.Create('wrong bit!');
    case Abit of
        0: Result := $01;
        1: Result := $02;
        2: Result := $04;
        3: Result := $08;

        4: Result := $10;
        5: Result := $20;
        6: Result := $40;
        7: Result := $80;

        else Result := $00;
    end;

end;

function TControlMachine.IsSensorOn: boolean;
var
    mByte : Byte ;
    mByteOn : Byte;
begin
    
    mByte := AsmIn(BasePort + PORT_State);
    mByteOn := GetByteByBit(AlertSensor);
    if (mByte and mByteOn) > 0 then Result := True
    else Result := False;
end;

function TControlMachine.LightCommand(AOnOff: boolean): boolean;
var
    mByte : Byte ;
begin
    mByte := GetByteByBit(AlertLight);
    mByte := mByte or AsmIn(BasePort + PORT_DATA);
    AsmOut(BasePort + PORT_DATA,mByte);
    if AsmIn(BasePort + PORT_DATA) = mByte then Result := True
    else Result := False;
end;

function TControlMachine.SpeakerCommand(AOnOff: boolean): boolean;
var
    mByte : Byte ;
begin
    mByte := GetByteByBit(AlertSpeaker);
    mByte := mByte or AsmIn(BasePort + PORT_DATA);
    AsmOut(BasePort + PORT_DATA,mByte);
    if AsmIn(BasePort + PORT_DATA) = mByte then Result := True
    else Result := False;
end;

end.

⌨️ 快捷键说明

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