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

📄 u_function.pas

📁 此系统完成了单向复费率电能表的抄表以及编程
💻 PAS
字号:
unit U_function;

interface
uses
  Windows,SysUtils,inifiles,Dialogs, Forms,TComm1;

  function OpenCOM(commport:Tcomportnumber;baudrate:Tbaudrate;parity:Tparity):integer;
                  stdcall; external 'rs485.dll';
  function CloseCOM(): integer;
                  stdcall; external 'rs485.dll';
  function ComOut(aa: string; bb: integer): longint;
                  stdcall; external 'rs485.dll';
  function NgOUT(aa: string;h_ys:integer;revc_ys:integer): integer;
                  stdcall; external 'rs485.dll';
  function EasOUT(aa: string;mac_flag:integer;h_ys:integer;revc_ys:integer): integer;
                  stdcall; external 'rs485.dll';
  function XcOUT(aa: string;h_ys:integer;revc_ys:integer): integer;
                  stdcall; external 'rs485.dll';

  function MyMsgBox(const ACaption,ATitle:string;Option:integer):integer;
  function RWdatfile(const RWflag:string; const Filename:string;
                     var data:string):integer;
  procedure PCsound(len:integer;times:integer);
  {* 返回从psInput字符串左边开始用pcPadWith填充后总长度为PiWidth的字符串}
  function PadLStr(psInput:String; piWidth:Integer; pcPadWith:Char):String;
  {* 返回从psInput字符串右边开始用pcPadWith填充后总长度为PiWidth的字符串}
  function PadRStr(psInput:String; piWidth:Integer; pcPadWith:Char):String;
  ///
  function yhedit(str1: string;zs:integer;xs:integer): string;
  {* 返回替换后字符串 Examples: StrTran('aruyfbn','ruy','====='); 返回'a=====fbn'}
  function StrTran(psInput:String; psSearch:String; psTranWith:String):String;
  function d_hex(dec:string):string;  //&&10进制转换为长度为4位的16进制
  function hextochar(str:string):string;  //二进制转换成16进制
  function tzstrx(bb: string): string;
  ///写日志 INI 文件
  function f_rz_ini(str1:string;str2:string):integer;
//判断1月2日3时4分5秒6周次的合法性 ,>=0 合法,小于0 非法
  procedure datetime(number:integer;flag:integer;var sf:integer);
var
   myinifile:tinifile;
implementation

uses U_data, U_645_Command;


function MyMsgBox(const ACaption,ATitle:string;Option:integer):integer;
begin
  case Option of
    0: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_OK+MB_ICONERROR);                              //错误提示

    1: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_OK+MB_ICONINFORMATION);                        //信息提示

    2: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_OK+MB_ICONWARNING);                            //警告提示

    3: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_YESNO+MB_ICONQUESTION+MB_DEFBUTTON1);          //选择,Y默认

    4: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_YESNO+MB_ICONQUESTION+MB_DEFBUTTON2);          //选择,N默认

    5: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_YESNOCANCEL+MB_ICONQUESTION+MB_DEFBUTTON1);

    6: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_YESNOCANCEL+MB_ICONQUESTION+MB_DEFBUTTON2);

    7: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_YESNOCANCEL+MB_ICONQUESTION+MB_DEFBUTTON3);

    8: Result:=MessageBox(Application.Handle, PChar(ACaption),
               PChar(ATitle), MB_ABORTRETRYIGNORE+MB_ICONQUESTION+MB_DEFBUTTON3);
  end;
end;

/////////////////读写 *.dat文件 //////////////////////////
// 返回值:                                             //
//        1    成功                                     //
//      -1    读失败                                   //
//      -2    函数入口参数错误                         //
//////////////////////////////////////////////////////////
function RWdatfile(const RWflag:string; const Filename:string;
                var data:string):integer;
var
   fp:textfile;
   s:string;
begin
   s:='';
   if UpperCase(RWflag) = 'R' then
   Begin
      if fileexists(Filename) then
      begin
         AssignFile(fp, Filename);
         reset(fp);
         Readln(fp,data);
         CloseFile(fp);
         Result := 1;     //读文件成功;
      end
      else
      begin
         Result := -1;     //读文件失败;
      end;
   end
   else if UpperCase(RWflag) = 'W' then
   Begin
      AssignFile(fp, Filename);
      Rewrite(fp);
      Writeln(fp, data);
      CloseFile(fp);
      Result := 1;     //覆盖写文件成功;
   end
   else if UpperCase(RWflag) = 'X' then
   Begin
      if fileexists(Filename) then
      begin
        AssignFile(fp, Filename);
        append(fP);
        Writeln(fp, data);
        CloseFile(fp);
        Result := 1;     // 追加写文件成功;
      end
      else
      begin
        AssignFile(fp, Filename);
        Rewrite(fp);//append(fP);
        Writeln(fp, data);
        CloseFile(fp);
        Result := 1;     //写文件成功;
      end;
   end
   else
   begin
     Result := -2;     //函数参数错误;
   end;
end;

procedure PCsound(len:integer;times:integer);  //len=200 ;
var
  i:integer;
begin
  for i := 1  to times do
  begin
     windows.Beep($2FF,len);
     sleep(10);
  end;
end;

function Replicate(pcChar:Char; piCount:integer):string;
begin
	Result:='';
	SetLength(Result,piCount);
	fillChar(Pointer(Result)^,piCount,pcChar)
end;

{* 返回从psInput字符串左边开始用pcPadWith填充后总长度为PiWidth的字符串}
function PadLStr(psInput:String; piWidth:Integer; pcPadWith:Char):String;
begin
	Result:=Replicate(pcPadWith,piWidth-Length(psInput))+psInput
end;

{* 返回从psInput字符串右边开始用pcPadWith填充后总长度为PiWidth的字符串}
function PadRStr(psInput:String; piWidth:Integer; pcPadWith:Char):String;
begin
	Result:=psInput+Replicate(pcPadWith,piWidth-Length(psInput))
end;

function yhedit(str1: string;zs:integer;xs:integer): string;
var
  i, s, ipos: integer;
  str, zs1, xs1, str2: string;
begin
    str := '';
    str2 := '';
    str2 := str1;
    i := pos('.',trim(str2));
    if i = 0 then
    begin
       if length(str2) > zs   then
       begin
         MESSAGEDLG(#13+'数据:'+ str1 +#13+'整数部分的长度大于规定长度!',MTINFORMATION,
           [mbOk], 0);
         result := '';
       end
       else
       begin
         zs1 := PadLStr(trim(str1),zs,'0');
         //xs1 := '00';
         xs1 := PadRStr(xs1,xs,'0');
         str:=zs1+'.'+xs1;
         result := str;
       end;
    end
    else
    begin
      delete(str2,1,i);
      i := pos('.',trim(str2));
      if i > 0 then
      begin
         MESSAGEDLG(#13+'数据:'+ str1 +#13+'不是合法的数值型数据!',MTINFORMATION,
           [mbOk], 0);
         result := '';
      end
      else
      begin
          ipos := pos('.',trim(str1));

          zs1 := trim(copy(trim(str1), 1, ipos-1));
          zs1 := PadLStr(zs1,zs,'0');

          xs1 := copy(trim(str1), ipos+1, xs);
          xs1 := PadRStr(xs1,xs,'0');

          str:=zs1+'.'+xs1;
          result := str;
       end;
    end;
end;

{* 返回替换后字符串 Examples: StrTran('aruyfbn','ruy','====='); 返回'a=====fbn'}
function StrTran(psInput:String; psSearch:String; psTranWith:String):String;
var
	liPosition,liLenOfSrch,liLenOfIn:integer;
begin
	liPosition:=Pos(psSearch,psInput);
	liLenOfSrch:=Length(psSearch);
	liLenOfIn:=Length(psInput);
	while liPosition>0 do
	begin
		psInput:=Copy(psInput,1,liPosition-1)
			+psTranWith
      +Copy(psInput,liPosition+liLenOfSrch,liLenOfIn);
		liPosition:=Pos(psSearch,psInput)
	end;
	Result:=psInput
end;

function tzstrx(bb: string): string;
var lenn, i: integer;
  aa: string;
begin
  lenn := length(bb);
  aa := '';
  i := lenn;
  while i > 0 do
  begin
    aa := aa + copy(bb, i - 1, 2);
    lenn := lenn - 2;
    i := lenn;
  end;
  tzstrx := aa;
end;

///写日志 INI 文件
function f_rz_ini(str1:string;str2:string):integer;
var
   str,filename ,up_flag,rzsj:string;
begin
   rzsj := formatdatetime('yyyy-mm-dd hh:mm:ss ddd',now);
////////////////检测注册文件/////////////////
   up_flag := AnsiUpperCase(str1);
   if up_flag = 'S'  then
      str :='发送数据'
   else
      str :='返回数据';
   filename:=extractfilepath(paramstr(0))+'log.ini';   //////得到完整的INI文件路径名///
   myinifile:=tinifile.create(filename);            //////打开INI文/////////////
   myinifile.WriteString(rzsj,str,str2);
   result:=1;
end;

//判断1月2日3时4分5秒6周次的合法性 ,>=0 合法,小于0 非法
procedure datetime(number:integer;flag:integer;var sf:integer);
begin
  sf := 0;
  case flag of
  1:
    if number > 12 then
    begin
      sf := -1;
      MyMsgBox('【月】 不能大于 12','系  统  错  误', 0);
      exit;
    end;
  2:
    if number > 31 then
    begin
      sf := -2;
      MyMsgBox('【日】 不能大于 31','系  统  错  误', 0);
      exit;
    end;
  3:
    if number > 23 then
    begin
      sf := -3;
      MyMsgBox('【时】 不能大于 23','系  统  错  误', 0);
      exit;
    end;
  4:
    if number >= 60 then
    begin
      sf := -4;
      MyMsgBox('【分】 不能大于 59','系  统  错  误', 0);
      exit;
    end;
  5:
    if number >= 60 then
    begin
      sf := -5;
      MyMsgBox('【秒】 不能大于 59','系  统  错  误', 0);
      exit;
    end;
  6:
    if (number > 7) or (number <= 0) then
    begin
      sf := -6;
      MyMsgBox('周次必须是 1 ~ 7 之间的数','系  统  错  误', 0);
      exit;
    end;
  end;

end;
function d_hex(dec:string):string;  //&&10进制转换16进制
var bb,sixt,cc,mod1:integer;
     c1,ccc,modi1,dh:string;
begin
bb:=strtoint(dec);
sixt:=16;
cc:=bb;

c1:='';
while cc>=sixt  do
begin
   mod1:=cc mod sixt;
   if mod1>=10 then
   begin
      case mod1 of
      10:modi1:='A';
      11:modi1:='B';
      12:modi1:='C';
      13:modi1:='D';
      14:modi1:='E';
      15:modi1:='F';
      end;
    end
   else
    modi1:=trim(inttostr(mod1));
   c1:=modi1+c1;

   cc:=cc div sixt;
end;

if cc>=10 then
   case cc of
   10:ccc:='A';
   11:ccc:='B';
   12:ccc:='C';
   13:ccc:='D';
   14:ccc:='E';
   15:ccc:='F';
   end
else
   ccc:=trim(inttostr(cc));
dh:=ccc+c1;
if length(dh)<4 then
  dh:='0'+dh;
if length(dh)<4 then
  dh:='0'+dh;
if length(dh)<4 then
  dh:='0'+dh;
if length(dh)<4 then
  dh:='0'+dh;
d_hex:=dh;
end;
function hextochar(str:string):string;
begin
   if str='0000' then
      str:='0';
   if str='0001' then
      str:='1';
   if str='0010' then
      str:='2';
   if str='0011' then
      str:='3';
   if str='0100' then
      str:='4';
   if str='0101' then
      str:='5';
   if str='0110' then
      str:='6';
   if str='0111' then
      str:='7';
   if str='1000' then
      str:='8';
   if str='1001' then
      str:='9';
   if str='1010' then
      str:='A';
   if str='1011' then
      str:='B';
   if str='1100' then
      str:='C';
   if str='1101' then
      str:='D';
   if str='1110' then
      str:='E';
   if str='1111' then
      str:='F';
  hextochar:=str;
end;



end.

⌨️ 快捷键说明

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