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

📄 include.pas

📁 一个DELPHI 云台控制动态库
💻 PAS
字号:
unit include;

interface
uses data_def,types,windows,SysUtils;

type

    pelco_d = class
    private
       function getMessage(address,data1,data2,data3,data4 : byte) : pdata;
       function setCamera(address,value : byte) : pdata;
       function autoScan(address,value : byte) : pdata;
       function setIris(address,value : byte) : pdata;
       function setFocus(address,value : byte) : pdata;
       function setAngle(address,value : byte) : pdata;
       function moveLeft(address,value : byte) : pdata;
       function moveRight(address,value : byte) : pdata;
       function moveUp(address,value : byte) : pdata;
       function moveDown(address,value : byte) : pdata;
       function ClearAction(address,value : byte) : pdata;
    public
      function run_command(address,cmdNo,value : byte) : pdata;
    end;

    function YT_Open(AComNo : Integer; ComSpeed : DWORD) : Boolean;
    function YT_Command(AAddress , ACmdNo , AParam: byte) : Boolean;
    function YT_Close() : Boolean;

implementation
var
	m_hCom :THANDLE;//
    //m_Buffer : array [0..500] of char;
   // m_BufferUsedSize : Integer;
    
function YT_Open(AComNo : Integer; ComSpeed :DWORD) : Boolean;
var
    COMMTIMEOUTS : TCommTimeouts;
    dcb : TDCB;
    ComNo : String;
begin
    Result := False;
    try
      YT_Close();
      ComNo := 'COM' + IntToStr(AComNo);
      m_hCom := CreateFile( PChar(ComNo),
                       GENERIC_READ or GENERIC_WRITE,
                       0,    // comm devices must be opened w/exclusive-access
                       nil, // no security attributes
                       OPEN_EXISTING, // comm devices must use OPEN_EXISTING
                       FILE_ATTRIBUTE_NORMAL,    //0 not overlapped I/O
                       //FILE_FLAG_OVERLAPPED,
                       0  // hTemplate must be NULL for comm devices
                       );
      if(m_hCom = INVALID_HANDLE_VALUE) then
      begin
        Exit;
      end;

      GetCommTimeouts(m_hCom,CommTimeOuts);
      ZeroMemory(@CommTimeOuts,sizeof(TCommTimeouts));
      CommTimeOuts.ReadIntervalTimeout := $ffffffff;   //200毫秒
      CommTimeOuts.WriteTotalTimeoutConstant := $1388;
      SetCommTimeouts(m_hCom,CommTimeOuts);
      GetCommState(m_hCom, dcb);
      ZeroMemory(@dcb,sizeof(TDCB));
      dcb.DCBlength := sizeof(TDCB);
      dcb.BaudRate := ComSpeed;     // set the baud rate
      dcb.ByteSize := 8;             // data size, xmit, and rcv
      dcb.Parity := NOPARITY;        // no parity bit
      dcb.StopBits := ONESTOPBIT;    // one stop bit
      dcb.XonLim := 2048;
      dcb.XoffLim := 512;
      dcb.XonChar := Char(17);
      dcb.XoffChar := Char(19);
      dcb.EofChar := Char(26);
      SetCommState(m_hCom, dcb);

      Result := True;
    except
      Result := False;
    end;
end;

function YT_Command(AAddress , ACmdNo , AParam: byte) : Boolean;
var
    command : pelco_d ;
    bWriteFile : Boolean;
    writtenLen : Cardinal	;
begin
    command := pelco_d.Create ;
    result := False;
    try
        try
          command.run_command(AAddress,ACmdNo,AParam);
          bWriteFile := WriteFile(m_hCom, data_Buffer, Length(data_Buffer) + 1, writtenLen, 0);
        except
          result := False;
          exit;
        end;

        result := bWriteFile

    finally
      command.Free;
    end;
end;

function YT_Close() : Boolean;
begin
  result := False;
  try
    if(INVALID_HANDLE_VALUE <> m_hCom) then
    begin
      CloseHandle(m_hCom);
      m_hCom := INVALID_HANDLE_VALUE;
    end;
    result := True;
  except
    result := False;
  end;
    //ZeroMemory(@m_Buffer,sizeof(m_Buffer));
   // m_BufferUsedSize :=0;
end;

function pelco_d.autoScan(address,value: byte): pdata;
begin
    if value = $00 then
    result := clearAction(address,value )
    else if value = $01 then
    result := getMessage(address,AutoscanOn,Zero,Zero,Zero);
end;

function pelco_d.ClearAction(address, value: byte): pdata;
begin
    result := getMessage(address,Zero,Zero,Zero,Zero);
end;

function pelco_d.getMessage(address, data1, data2, data3,
  data4: byte): pdata;
var
    checkSum : byte;
begin
    if (address < $00 ) or (address > $1F) then
    raise Exception.Create('Protocol Pelco P support 32 devices only');
    checkSum := (address + data1 + data2 + data3 + data4) mod 256;
    data_Buffer[0] := STX;
    data_Buffer[1] := address;
    data_Buffer[2] := data1;
    data_Buffer[3] := data2;
    data_Buffer[4] := data3;
    data_Buffer[5] := data4;
    data_Buffer[6] := checkSum;
    //data_Buffer[7] := checkSum;
    result := True;
end;

function pelco_d.moveDown(address, value: byte): pdata;
begin
    if ( value > TiltSpeedMin ) and ( value <= TiltSpeedMax) then
    result := getMessage(address,Zero,TiltDown,Zero,value)
    else if value = 0 then
    result := clearAction(address,value )
    else
    raise Exception.Create('Speed beyond Support');
end;

function pelco_d.moveLeft(address, value: byte): pdata;
begin
    if (( value > PanSpeedMin ) and ( value <= PanSpeedMax)) or ( value = PanSpeedMost)then
    result := getMessage(address,Zero,PanLeft,value,Zero)
    else if value = 0 then
    result := clearAction(address,value )
    else
    raise Exception.Create('Speed beyond Support');
end;

function pelco_d.moveRight(address, value: byte): pdata;
begin
    if (( value > PanSpeedMin ) and ( value <= PanSpeedMax)) or ( value = PanSpeedMost)then
    result := getMessage(address,Zero,PanRight,value,Zero)
    else if value = 0 then
    result := clearAction(address,value )
    else
    raise Exception.Create('Speed beyond Support');
end;

function pelco_d.moveUp(address, value: byte): pdata;
begin
    if ( value > TiltSpeedMin ) and ( value <= TiltSpeedMax) then
    result := getMessage(address,Zero,TiltUp,Zero,value)
    else if value = 0 then
    result := clearAction(address,value )
    else
    raise Exception.Create('Speed beyond Support');
end;

function pelco_d.run_command(address, cmdNo, value : byte): pdata;
begin
    case cmdNo of
    1 : setCamera(address,value);
    2 : autoScan(address,value) ;
    3 : setIris(address,value ) ;
    11 : setFocus(address,value);
    12 : setAngle(address,value) ;
    21 : moveLeft(address,value ) ;
    22 : moveRight(address,value) ;
    23 : moveUp(address,value) ;
    24 : moveDown(address,value ) ;
    0 : clearAction(address,value ) ;
    end
end;

function pelco_d.setAngle(address, value: byte): pdata;
begin
  if value = $00 then
    result := getMessage(address,Zero,ZoomTele,Zero,Zero)
  else if value = $01 then
    result := getMessage(address,Zero,ZoomWide,Zero,Zero);
end;

function pelco_d.setCamera(address, value: byte): pdata;
begin
  if value = $00 then
    result := getMessage(address,CameraOff,Zero,Zero,Zero)
  else if value = $01 then
    result := getMessage(address,CameraOn,Zero,Zero,Zero);
end;

function pelco_d.setFocus(address, value: byte): pdata;
begin
  if value = $00 then
    result := getMessage(address,FocusNear+$10,Zero,Zero,Zero)
  else if value = $01 then
    result := getMessage(address,Zero+$10,FocusFar,Zero,Zero);
end;

function pelco_d.setIris(address, value: byte): pdata;
begin
  if value = $00 then
    result := getMessage(address,IrisClose,Zero,Zero,Zero)
  else if value = $01 then
    result := getMessage(address,IrisOpen,Zero,Zero,Zero);
end;

end.

⌨️ 快捷键说明

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