📄 abcommatrix.pas
字号:
unit ABComMatrix;
{
AB矩阵控制协议.
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, CPort;
const
HeadChar = $FE;
EndChar = $FF;
type
TCommandKind = (CmUnknow,CmInMenu,CmOutMenu,CmSwMonitor,CmSwCamera);
TControlEvent = procedure(Sender: TObject; InfoMsg:string) of object;
TABMatrixComCtrl = class(Tcomponent)
private
FDeviceID:string;
FDeviceName:string;
FCommPort:string;
FBaudRate:Dword;
FComPort:TComPort;
FMonitorID:integer;
FCameraID:integer;
FOnControlMsg:TControlEvent;
FCurrentCmd:TCommandKind;
FPTZSpeed:integer;
procedure SetCommPort(Value: string);
procedure SetBaudRate(Value: Dword);
protected
FReceiveLen:integer;
FSendBuf:array[0..255] of char;
FReceiveBuf:array[0..511] of char;
procedure CommReceiveData(Sender: TObject; Count: Integer);
function SendCommData(SendBuffer:array of char; const SendLength:Integer):Boolean;
public
//function LoginMenu:Boolean;
//function LogOutMenu:Boolean;
procedure SwitchMonitor;
procedure SwitchCamera;
procedure SwitchMonitorAndCamera;
procedure MoveDown;
procedure MoveUp;
procedure MoveLeft;
procedure MoveRight;
procedure LevelStop;
procedure VertStop;
//procedure IrisOpen; //光圈打开
//procedure IrisClose; //光圈关闭
//procedure FocusFar; //远焦控制
//procedure FocusNear; //近焦控制
procedure ZoomWide; //视角变宽
procedure ZoomTele; //视角变窄
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
function ControlStart: boolean; //开始控制
procedure ControlEnd; //结束控制
//--辅助控制命令
published
property MonitorID:integer read FMonitorID write FMonitorID;
property CameraID:integer read FCameraID write FCameraID;
property PTZSpeed:integer read FPTZSpeed write FPTZSpeed;
property DeviceID:string read FDeviceID write FDeviceID;
property DeviceName:string read FDeviceName write FDeviceName;
property ComPort:TComPort read FComPort Write FComPort;
property CommPort:string read FCommPort write SetCommPort;
property BaudRate:Dword read FBaudRate write SetBaudRate;
property CurrentCmd:TCommandKind read FCurrentCmd;
property OnControlMsg:TControlEvent read FOnControlMsg write FOnControlMsg;
end;
procedure Register;
implementation
uses Contnrs;
procedure Register;
begin
RegisterComponents('GSMonitor', [TABMatrixComCtrl]);
end;
{ TABMatrixComCtrl }
function getdigitStr(digit:integer):string;
begin
if digit<10 then
begin
Result := Chr(digit);
Exit;
end;
if digit<100 then
begin
Result := Chr(digit div 10)+Chr(digit mod 10);
Exit;
end;
if digit<1000 then
begin
Result := Chr(digit div 100) + chr( (digit mod 100) div 10 ) + chr( (digit mod 100) mod 10 );
end;
end;
procedure TABMatrixComCtrl.SetCommPort(Value: string);
begin
if Value <> FCommPort then
begin
FCommPort := Value;
end;
end;
procedure TABMatrixComCtrl.SetBaudRate(Value: Dword);
begin
if Value <> FBaudRate then
begin
FBaudRate := Value;
end;
end;
procedure TABMatrixComCtrl.ControlEnd;
begin
if FComPort.Connected then
FComPort.Close;
end;
function TABMatrixComCtrl.ControlStart: boolean;
var tmpResult : Boolean;
begin
tmpResult := true;
FComPort.BaudRate := brCustom;
FComPort.CustomBaudRate := FBaudRate;
FComPort.Port := FCommPort;
FComPort.OnRxChar := CommReceiveData;
try
if not FComPort.Connected then
FComPort.Open;
except
tmpResult := False;
end;
Result := tmpResult;
end;
constructor TABMatrixComCtrl.Create(AOwner: TComponent);
begin
inherited Create( AOwner );
FReceiveLen := 0;
FBaudRate := 1200;
FPTZSpeed := 3;
end;
destructor TABMatrixComCtrl.Destroy;
begin
inherited Destroy;
end;
function TABMatrixComCtrl.SendCommData(SendBuffer:array of char; const SendLength:Integer):Boolean;
begin
Result := (FComPort.Write(SendBuffer,SendLength)= SendLength);
end;
procedure TABMatrixComCtrl.CommReceiveData(Sender: TObject; Count: Integer);
var BufferLength:integer;
begin
end;
procedure TABMatrixComCtrl.SwitchMonitor;
var tmpCmd:string;
begin
//发送.
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := chr(HeadChar)+getdigitStr(FMonitorID)+Chr($13)+chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.SwitchCamera;
var tmpCmd:string;
begin
//发送.
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := Chr(HeadChar)+getdigitStr(FCameraID) +Chr($11)+Chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.ZoomTele;
begin
FillChar(FSendBuf,Sizeof(FSendBuf),0);
FSendBuf[0] := chr(HeadChar);
FSendBuf[1] := chr($1F);
FSendBuf[2] := chr(EndChar);
SendCommData(FSendBuf,3);
end;
procedure TABMatrixComCtrl.ZoomWide;
begin
FillChar(FSendBuf,Sizeof(FSendBuf),0);
FSendBuf[0] := chr(HeadChar);
FSendBuf[1] := chr($20);
FSendBuf[2] := chr(EndChar);
SendCommData(FSendBuf,3);
end;
procedure TABMatrixComCtrl.MoveDown;
var
tmpCmd:string;
begin
//FEH,速度值,1CH,FFH ,PTZSpeed速度1..7
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := Chr(HeadChar)+getdigitStr(FPTZSpeed) +Chr($1C)+Chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.MoveLeft;
var
tmpCmd:string;
begin
//FEH,速度值,19H,FFH ,PTZSpeed速度1..7
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := Chr(HeadChar)+getdigitStr(PTZSpeed) +Chr($19)+Chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.MoveRight;
var
tmpCmd:string;
begin
//FEH,速度值,1AH,FFH ,PTZSpeed速度1..7
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := Chr(HeadChar)+getdigitStr(PTZSpeed)+Chr($1A)+Chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.MoveUp;
var
tmpCmd:string;
begin
//FEH,速度值,1AH,FFH ,PTZSpeed速度1..7
FillChar(FSendBuf,Sizeof(FSendBuf),0);
tmpCmd := Chr(HeadChar)+getdigitStr(PTZSpeed) +Chr($1B)+Chr(EndChar);
StrPCopy(FSendBuf, tmpCmd);
SendCommData(FSendBuf,length(tmpCmd));
end;
procedure TABMatrixComCtrl.SwitchMonitorAndCamera;
var tmpCmd:string;
begin
SwitchMonitor;
SwitchCamera;
end;
procedure TABMatrixComCtrl.LevelStop;
begin
FillChar(FSendBuf,Sizeof(FSendBuf),0);
FSendBuf[0] := chr(HeadChar);
FSendBuf[1] := chr(0);
FSendBuf[2] := chr($1A);
FSendBuf[3] := chr($FF);
FSendBuf[4] := chr($FE);
FSendBuf[5] := chr(0);
FSendBuf[6] := chr($19);
FSendBuf[7] := chr(EndChar);
SendCommData(FSendBuf,8);
end;
procedure TABMatrixComCtrl.VertStop;
begin
FillChar(FSendBuf,Sizeof(FSendBuf),0);
FSendBuf[0] := chr(HeadChar);
FSendBuf[1] := chr(0);
FSendBuf[2] := chr($1C);
FSendBuf[3] := chr($FF);
FSendBuf[4] := chr($FE);
FSendBuf[5] := chr(0);
FSendBuf[6] := chr($1B);
FSendBuf[7] := chr(EndChar);
SendCommData(FSendBuf,8);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -