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

📄 unfunction.~pas

📁 delphi 编写的云镜控制支持PELCO P/D协议的DLL。 支持对云台镜头及辅助开关的操作。直接封装了SPCOM类
💻 ~PAS
字号:
unit Unfunction;

interface
  uses
  SysUtils,
  Classes,
  SPComm ;
  var
  sbuf: array[0..7] of byte;
  comm1:Tcomm ;

  function   MP_IntChang(cs_lx:ShortString;cs_int:integer):ShortString;stdcall;export;
  function   LocalIP:ShortString; stdcall;export;
  function   StrtoCommand(Scommand:ShortString;sed_addr:integer;
             yunjingSeep:integer;spelco:integer):shortstring;stdcall;export;

  function  commCS(BR:integer;comm_name:Shortstring):boolean;stdcall;export;


  procedure  Sb_stop();stdcall;
  procedure  SbAutoz();stdcall;
  procedure  Sbbrush1Down();stdcall;
  procedure  Sbbrush2Down();stdcall;
  procedure  sbupDown(YunJingSeep:integer)stdcall;
  procedure  sbdownDown(YunJingSeep:integer);stdcall;
  procedure  sbrightDown(YunJingSeep:integer);stdcall;
  procedure  sbleftDown(YunJingSeep:integer);stdcall;
  procedure  sbfocus1Down;stdcall;
  procedure  sbfocus2Down;stdcall;
  procedure  sbFUZHU1Close;stdcall;
  procedure  sbFUZHU1Down;stdcall;
  procedure  sbfuzhu2close;stdcall;
  procedure  sbfuzhu2down;stdcall;
  procedure  sbiris1Down;stdcall;
  procedure  sbiris2down;stdcall;
  procedure  sbZoom1Down;stdcall;
  procedure  sbZoom2Down;stdcall;

  function senddata(sed_addr:integer;spelco:integer):shortstring;stdcall;
  
  implementation


{**************************将十进制数值转化为其它进制字符***********************
*  函数名 MP_IntChang   功能:将十进制数值转化为其它进制字符;                  *
*  类 别:其它                                                                 *
*  X进制算法                                                                   *
*  参数:进制cs_lx,十进制数值cs_int                                             *
*  v_str:=MP_IntChang('16',123);   得到2进制的字符。                           *
*  '02'表二进制,'08'可以表示8进制                                              *
{*****************************************************************************8*}
function   MP_IntChang(cs_lx:ShortString;cs_int:integer):ShortString;stdcall;export;
  var
      v_cc,v_chr:ShortString;
      V_y,i,V_ix:integer;
  begin
  result:='';
  v_chr:='0123456789ABCDEF';
  //showmessage(cs_lx);
  v_ix:=strtoint(cs_lx);
  if   (v_ix>16)   or   (v_ix<2)   then   exit;
  V_y:=cs_int;
  v_cc:='';
  while   V_y>0   do
      begin
      i:=   V_y   mod   v_ix;
      V_y:=V_y   div   v_ix;
      v_cc:=copy(v_chr,i+1,1)+v_cc;
      end;
 if length(V_cc)<2 then V_cc:='0'+V_cc;
 result:=v_cc;
end;
{*************************查出本地IP******************************************8*
*   函数名 localip                                                             *
*   功能:查出本地IP 需加 uses winsock;                                        *
*****************************************************************************8*}
function LocalIP:ShortString;stdcall;export;
var
  phoste:PHostEnt;
  Buffer:array[0..100] of char;
  WSAData:TWSADATA;
begin
  result:='';
  if WSASTartup($0101, WSAData) <> 0 then exit;
  GetHostName(Buffer,Sizeof(Buffer));
  phoste:=GetHostByName(buffer);
  if phoste = nil then
  begin
    result:='127.0.0.1';
  end
  else
    result:=StrPas(inet_ntoa(PInAddr(phoste^.h_addr_list^)^));
  WSACleanup;
end;

{************************************命令代码转换为操作代码*********************
*                                                                               *
*  Scommand          :指令码                                                    *
*  comm1             :COM口号                                                   *
*  sed_addr          :解码器地址                                                *
*  yunjingseep       :云镜速度                                                  *
*  spelco            :解码器协议( 0.P 1.D)
********************************************************************************}
function  commCS(BR:integer;comm_name:Shortstring):boolean;stdcall;export;
begin
 try
  comm1:=Tcomm.Create(nil);
  comm1.CommName:=comm_name;
  comm1.BaudRate:=BR ;
  //comm1.ByteSize:=byte;
  comm1.StartComm;
  result:=true;
 except
  result:=false;
 end;
end;
function StrtoCommand(Scommand:ShortString;sed_addr:integer;
          yunjingSeep:integer;spelco:integer):shortstring;stdcall;export;
begin
    try
    result:='true';
    if (Scommand='ClainClose')or (Scommand='sbautoN')or(Scommand='sbstop') then
    begin
      sb_stop;
      //senddata(comm1,sed_addr,spelco);
      result:='停止'+senddata(sed_addr,spelco) ;
      exit;
    end;
{------------云台控制----------------------------------------------------------}
    if Scommand='sbup' then
    begin
      sbupDown(yunjingSeep);
      //senddata(comm1,sed_addr,spelco);
      result:='向上'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbdown' then
    begin
      sbdownDown(yunjingSeep) ;
      //senddata(comm1,sed_addr,spelco);
      result:='向下' +senddata(sed_addr,spelco);
      exit;
    end;
    if Scommand='sbleft' then
    begin
      sbleftDown(yunjingSeep);
      //senddata(sed_addr,spelco);
      result:='向左'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbright' then
    begin
      sbrightDown(yunjingSeep);
      //senddata(sed_addr,spelco);
      result:='向右'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbautoY' then
    begin
      sbautoZ ;
      //senddata(sed_addr,spelco);
      result:='打开自动扫描'+senddata(sed_addr,spelco) ;
      exit;
    end ;
{---------------镜头控制-------------------------------------------------------}
    if Scommand='sbzoom-' then
    begin
      SbZoom1Down;
      //senddata(sed_addr,spelco);
      result:='变倍-'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbzoom+' then
    begin
      SBZoom2Down;
      //senddata(sed_addr,spelco);
      result:='变倍+'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbfocus+' then
    begin
       sbfocus1Down;
       //senddata(sed_addr,spelco);
       result:='聚焦近'+senddata(sed_addr,spelco) ;
       exit;
    end;
    if Scommand='sbfocus-' then
    begin
      sbfocus2Down;
      //senddata(sed_addr,spelco);
      result:='聚焦远'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbiris-' then
    begin
      sbiris1Down;
      //senddata(sed_addr,spelco);
      result:='光圈-'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbiris+' then
    begin
      sbiris2Down;
      //senddata(sed_addr,spelco);
      result:='光圈+'+senddata(sed_addr,spelco) ;
      exit;
    end;
{-----------辅助开关-----------------------------------------------------------}
    if Scommand='sbbrush_close' then
    begin
      sbbrush1Down;
      //senddata(sed_addr,spelco);
      result:='关闭雨刷'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbbrush_open' then
    begin
      sbbrush2Down;
      //senddata(sed_addr,spelco);
      result:='打开雨刷'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbfz2_close' then
    begin
      SbFUZHU2CLOSE;
      //senddata(sed_addr,spelco);
      result:='关闭辅助开关2'+senddata(sed_addr,spelco) ;
      exit;
    end;
    if Scommand='sbfz1_close' then
    begin
      SbFUZHU1CLOSE;
      //senddata(sed_addr,spelco);
      result:='关闭辅助开关1'+senddata(sed_addr,spelco);
      exit;
    end;
    if Scommand='sbfz2_open' then
    begin
      SbFUZHU2Down;
      //rsenddata(comm1,sed_add,spelco);
      result:='打开辅助开关2'+senddata(sed_addr,spelco) ;
      exit;
    end;           
    if Scommand='sbfz1_open' then
    begin
      SbFUZHU1Down;
      //senddata(sed_addr,spelco);
      result:='打开辅助开关1'+senddata(sed_addr,spelco);
      exit;
    end;
    except
      result:='false';
    end;
   //senddata(sed_addr,spelco);
 end;
{------------------------------------------------------------------------------}
{ 解码器协议操作代码                                                           }
procedure SbFUZHU1CLOSE();stdcall;
BEGIN
  //FOCUS-  \A0\01\00\20\00\00\AF\2E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($0B);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($01);
  sbuf[6]:= byte($AF);
END;
procedure SbFUZHU1Down();stdcall;
BEGIN
  //FOCUS-  \A0\01\00\20\00\00\AF\2E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($09);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($01);
  sbuf[6]:= byte($AF);
END;
procedure  SbFUZHU2CLOSE();stdcall;
BEGIN
  //FOCUS-  \A0\01\00\20\00\00\AF\2E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($0B);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($02);
  sbuf[6]:= byte($AF);
END;
procedure  SbFUZHU2Down();stdcall;
BEGIN
  //FOCUS-  \A0\01\00\20\00\00\AF\2E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($09);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($02);
  sbuf[6]:= byte($AF);
END;
procedure  SbZoom1Down();stdcall;
begin
  //FOCUS-  \A0\01\00\20\00\00\AF\2E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($40);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure Sb_stop();stdcall;
begin
  //停止     \A0\01\00\00\00\00\AF\0E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($00);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  SbZoom2Down();stdcall;
begin
   //FOCUS+  \A0\01\00\40\00\00\AF\4E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($20);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  Sbfocus1Down();stdcall;
begin
  //ZOOM-  \A0\01\00\80\00\00\AF\8F   变焦+
  sbuf[2]:= byte($01);
  sbuf[3]:= byte($00);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  Sbfocus2Down();stdcall;
begin
   //ZOOM+  \A0\01\01\00\00\00\AF\0F   变焦 -
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($80);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  SbupDown(yunjingSeep:integer);stdcall;
begin
  //if onoff then onoff:=false;
  //上     \A0\01\00\08\2F\2F\AF\06
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($08);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte(yunjingseep);
  sbuf[6]:= byte($AF);
end;
procedure  SbdownDown(yunjingSeep:integer);stdcall;
begin
  //if onoff then onoff:=false;
  //下     \A0\01\00\10\2F\2F\AF\1E
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($10);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte(yunjingseep);
  sbuf[6]:= byte($AF);
end;
procedure  SbleftDown(yunjingSeep:integer);stdcall;
begin
  //if onoff then onoff:=false;
  //左 \A0\01\00\04\2F\2F\AF\0A
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($04);
  sbuf[4]:= byte(yunjingseep);
  sbuf[5]:= byte($00);
end;
procedure  SbrightDown(yunjingSeep:integer);stdcall;
begin
  //右     \A0\01\00\02\2F\2F\AF\0C
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($02);
  sbuf[4]:= byte(yunjingseep);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  Sbiris1Down();stdcall;
begin
  //IRLS-    \A0\01\04\00\00\00\AF\0A
  sbuf[2]:= byte($04);
  sbuf[3]:= byte($00);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
end;
procedure  Sbiris2Down();stdcall;
begin

  //IRLS+   \A0\01\02\00\00\00\AF\0A
  sbuf[2]:= byte($02);
  sbuf[3]:= byte($00);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($00);
  sbuf[6]:= byte($AF);
  sbuf[7]:= byte($0B);
end;
procedure  Sbbrush1Down();stdcall;
begin
  //雨刷 \A0\01\00\0B\00\04\AF\01  (off)
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($0B);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($03);
  sbuf[6]:= byte($AF);
end;
procedure  Sbbrush2Down();stdcall;
begin
  //雨刷    \A0\01\00\09\00\04\AF\03  (on)
  sbuf[2]:= byte($00);
  sbuf[3]:= byte($09);
  sbuf[4]:= byte($00);
  sbuf[5]:= byte($03);
  sbuf[6]:= byte($AF);
end;
procedure  SBAutoZ();stdcall;
begin
    sbuf[2]:= byte($00);
    sbuf[3]:= byte($07);
    sbuf[4]:= byte($00);
    sbuf[5]:= byte($63);
    sbuf[6]:= byte($AF);
end;


{------------------------------------------------------------------------------}
{                                发送数据给COM                                 }
function senddata(sed_addr:integer;spelco:integer):shortstring;stdcall;
var
  i:integer;
  str:shortstring;
begin
   str:='';
   case spelco of
{-----------P协议 8位----------------------------------------------------------}
   0:begin
     sbuf[0]:= byte($A0);
     sbuf[1]:=byte(sed_addr);

     // 计算校验码 8= 1 xor 2 xor 3..6
     sbuf[7]:=sbuf[0];
     for i:=1 to 6 do
     begin
     sbuf[7]:=sbuf[7] Xor sbuf[i];
     end;


     for i := 0 to 7 do
     begin
      str:=str+MP_IntChang('16',sbuf[i])+'-';
      if not comm1.writecommdata(@sbuf[i],1) then
      begin
         break;
      end;
      sleep(2);
     end;
   end;
{-----------D协议 7位----------------------------------------------------------}
   1:begin
     sbuf[0]:=byte($FF);
     sbuf[1]:=byte(sed_addr+1);

    // 计算校验码 7= mod[(2+..6/100H)]
     sbuf[6]:=sbuf[1];
     for i:=2 to 5 do
     begin
     sbuf[6]:=sbuf[6]+sbuf[i];
     end;
     sbuf[6]:=sbuf[6] mod 256;
     for i := 0 to 6 do
     begin
      str:=str+MP_IntChang('16',sbuf[i])+'-';
      if not comm1.writecommdata(@sbuf[i],1) then
      begin
         break;
      end;
      sleep(2);
     end;
   end;
{-----------D协议 7位----------------------------------------------------------}
 end;
 result:=str;
end; 

end.


⌨️ 快捷键说明

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