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

📄 dbjy.pas

📁 完成虚拟的多功能电能表
💻 PAS
📖 第 1 页 / 共 4 页
字号:
unit dbjy;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Grids, DataGrid, OleCtrls, MSCommLib_TLB,combas,inifiles,
  ExtCtrls;

type
  TdbjyForm = class(TForm)
    GroupBox1: TGroupBox;
    Grid1: TDataGrid;
    BitBtn1: TBitBtn;
    GroupBox2: TGroupBox;
    MSComm1: TMSComm;
    Timer1: TTimer;
    Grid2: TDataGrid;
    BitBtn2: TBitBtn;
    GroupBox3: TGroupBox;
    Grid3: TDataGrid;
    CheckBox4: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox1: TCheckBox;
    Panel1: TPanel;
    Label1: TLabel;
    GroupBox8: TGroupBox;
    Shaptb: TShape;
    GroupBox4: TGroupBox;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    ua: TComboBox;
    ub: TComboBox;
    uc: TComboBox;
    GroupBox5: TGroupBox;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    ia: TComboBox;
    ib: TComboBox;
    ic: TComboBox;
    GroupBox6: TGroupBox;
    Label33: TLabel;
    Label31: TLabel;
    Combxj: TComboBox;
    Comfx: TComboBox;
    Cmd1: TButton;
    Button1: TButton;
    ListBox1: TListBox;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    Timer2: TTimer;
    GroupBox7: TGroupBox;
    Combtxbw: TComboBox;
    Memo1: TMemo;
    Grid_flsj: TDataGrid;
    procedure MSComm1Comm(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Grid2SetEditText(Sender: TObject; ACol, ARow: Integer;
      const Value: String);
    procedure SpeedButton9Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Cmd1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure SpeedButton10Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Timer2Timer(Sender: TObject);
    procedure GroupBox3DblClick(Sender: TObject);
  private
    { Private declarations }
    GLGBS:integer;
    GLXX,GLDJ,GLWDJ:string;
    GLDY,GLDL:double;
    GLSFBL:DOUBLE;//缩放比例
    GLFX:integer;//方向
    GLCOM:integer;
    GLGXRX:BOOLEAN;//感性容性
    GLBH,GL_TXGY,GLBCMM,GLBCQX,GLXLMM,GLXLQX,GLBTL:Variant;
    tmpua,tmpub,tmpuc,tmpia,tmpib,tmpic:double;
    tmpys:string;
    tmpjd:double;
    jsnr:string;
//    db_time:Variant;  //电表电量起止时间

    sqsdb:Variant;
    db_sds,db_sqzs:integer;
    dqsd:string;
    dqflh:integer;
    start_time,end_time:string;
    function read_db_sj(tmpbh,dta:string;bw:integer):string; //数据读取
    function write_db_sj(tmpbh,dta:string;bw:integer):string;  //数据写入
    function datetime_set(dta:string;bw:integer):string;       //广播校时
    function addr_set(dta:string;bw:integer):string;     //设置表地址
    function txsl_set(dta:string;bw:integer):string;     //设置表通讯速率
    function mm_set(dta:string;bw:integer):string;       //设置密码
    function xl_clear(dta:string;bw:integer):string;     //最大需量清零
    Function sj_js_485(txbw:Integer;str_len:integer;tmpbz:String):String;
    Function sj_jsx(dd:TMSComm):String;
    Function sj_fs_485(dd:TMSComm;str_fs:String;tmpzjys:Integer):Boolean;
    procedure tz_dy_dl(tmp:string;tmp0:boolean); //调整电压电流
    Procedure datacl(dta:string);
  public
    { Public declarations }

  end;

var
  dbjyForm: TdbjyForm;
  db_dq_dl:Variant; //表电量
  db_sxx_dl:Variant; //1--4象限电量
  db_zdxl:Variant;//最大需量 需量发生时间

implementation

{$R *.DFM}

Function TDBJYForm.sj_fs_485(dd:TMSComm;str_fs:String;tmpzjys:Integer):Boolean;
var
  i,len:Integer;
  MyVariant: Variant;
begin
  if not dd.PortOPen then dd.portopen:=True;
  len:=length(str_fs) div 2;
  MyVariant :=VarArrayCreate ([0,0], varByte);
  for i:=0 to len-1 do
    begin
      MyVariant[0]:=StrToInt('$'+Mid(str_fs,i*2+1,2));
      dd.Output:=MyVariant;
      delay(tmpzjys/1000);
    end;
  result:=True;
end;


Function TDBJYForm.sj_js_485(txbw:Integer;str_len:integer;tmpbz:String):String;
var
  tmpstr:String;
begin
  tmpstr:=sj_jsx(Mscomm1);
  if Instr(tmpstr,'68C101')<>0 then
   begin
    spkbeep(50);
    Screen.Cursor:=crDefault;
    tmpstr:= 'RECV_BAD';
   end;
  Result:=tmpstr;
end;


Function TDBJYForm.sj_jsx(dd:TMSComm):String;
var
  MyVariant     : Variant;
  tmpbyte,tmplen: Integer;
  tmpnum,i      : Integer;
  tu            : Double;
  tmppd,tmpbz   : String;
  tmpstr,tmperr : String;
  tmphx         : String;
begin
    tmpnum:=dd.InBufferCount;
    MyVariant := VarArrayCreate([0,tmpnum-1], varByte);
    mscomm1.InputLen:=tmpnum;
    MyVariant:=dd.Input;
    mscomm1.InputLen:=1;
    try
      for i:=0 to tmpnum-1 do
        begin

          tmpbyte:=MyVariant[i];
          tmphx:=IntToHex(tmpbyte,2);
          if (tmphx='68') and (instr(jsnr,'68')=0) then
          begin
            jsnr:=tmphx;
            continue;
          end;
          if tmphx='16' then
          begin
            JSNR:=JSNR+tmphx;
            memo1.lines.add(jsnr);
            try
              datacl(trim(JSNR));
            except
            end;
            JSNR:='';
            EXIT;
          end;
        jsnr:=jsnr+tmphx;
        end;
    except
      beep;
    end;
  Result:=jsnr;

end;


procedure TdbjyForm.MSComm1Comm(Sender: TObject);
var
 dta:String;
 MyVariant     : Variant;
 tmpbyte,tmplen: Integer;
 tmpnum,i      : Integer;
 tu            : Double;
 tmppd,tmpbz   : String;
 tmpstr,tmperr : String;
 tmphx         : String;
begin
 mscomm1.RThreshold:=0;
 dta:=sj_jsx(mscomm1);
 mscomm1.RThreshold:=1;
end;

procedure TdbjyForm.tz_dy_dl(tmp:string;tmp0:boolean);
var
  tmpdy:string;
  i,tmpys:integer;
begin
  if tmp0 then
  begin
    for i:=1 to 6 do
    begin
      tmpdy:=mid(tmp,1,4);
      tmpua:=GLDY*strtofloat(tmpdy)/1000;
    end;
    tmpdy:=mid(tmp,5,4);
    tmpub:=GLDY*strtofloat(tmpdy)/1000;
    tmpdy:=mid(tmp,9,4);
    tmpuc:=GLDY*strtofloat(tmpdy)/1000;
    tmpdy:=mid(tmp,13,4);
    tmpia:=GLDL*strtofloat(tmpdy)/1000;
    tmpdy:=mid(tmp,17,4);
    tmpib:=GLDL*strtofloat(tmpdy)/1000;
    tmpdy:=mid(tmp,21,4);
    tmpic:=GLDL*strtofloat(tmpdy)/1000;
    tmpys:=strtoint(mid(tmp,25,1));
    case tmpys of
     3: tmpjd:=0;
     1: tmpjd:=60;
     4: tmpjd:=-30;
     5: tmpjd:=-60;
     2: tmpjd:=30;
     0: tmpjd:=75;
     6: tmpjd:=-75;
    end;
    if tmpys in [4,5,6] then
      GLGXRX:=false
    else
      GLGXRX:=true;
    dqflh:=0;
    GLFX:=strtoint(mid(tmp,26,1));
    start_time:=formatdatetime('hh:mm:ss',now);
    timer1.Enabled:=true;
    panel1.Visible:=False;
  end else begin
    timer1.Enabled:=false;
    tmpua:=0;
    tmpub:=0;
    tmpuc:=0;
    tmpia:=0;
    tmpib:=0;
    tmpic:=0;
  end;
end;

Procedure TdbjyForm.datacl(dta:string);
var
  tmpstr:string;
  i,m,j:integer;
  TMPBH,TMPKZM,TMPSJCD,TMPSJXBS,TMPSJX,TMPJYM:string;
  sndstr:string;
  tmpbw:integer;
  TMPSJ:string;
  fl:string;

begin
  tmpbw:=-1;
  TMPBH:=data_reset(mid(dta,3,12));
  if (tmpbh='999999999999') or (tmpbh='AAAAAAAAAAAA') then
    tmpbw:=Combtxbw.ItemIndex
  else
  for i:=1 to GLGBS do
  if TMPBH=GLBH[i] then
  begin
    tmpbw:=i;
    break;
  end;
  sndstr:='';
  if tmpbw=-1 then exit;
  TMPKZM:=mid(dta,17,2);
  if tmpkzm='01' then //请求读数据
    sndstr:=read_db_sj(tmpbh,dta,tmpbw)
{  else if tmpkzm='02' then //读后续数据
  begin
    TMPSJCD:=mid(dta,19,2);
    TMPSJXBS:=mid(dta,21,4);
  end;
  else if tmpkzm='03' then //重读数据
  begin
    TMPSJCD:=mid(dta,19,2);
    TMPSJXBS:=mid(dta,21,4);
  end;}
  else if tmpkzm='04' then //写数据
    sndstr:=write_db_sj(tmpbh,dta,tmpbw)
  else if tmpkzm='08' then //广播校时
    sndstr:=datetime_set(dta,tmpbw)
  else if tmpkzm='0A' then //写设备地址
    sndstr:=addr_set(dta,tmpbw)
  else if tmpkzm='0C' then //更改通讯速率
    sndstr:=txsl_set(dta,tmpbw)
  else if tmpkzm='0F' then //修改密码
    sndstr:=mm_set(dta,tmpbw)
  else if tmpkzm='10' then //最大需量清零
    sndstr:=xl_clear(dta,tmpbw);
  memo1.Lines.add(sndstr);
  if sndstr<>'' then
    sj_fs_485(mscomm1,sndstr,5);
end;

function TdbjyForm.xl_clear(dta:string;bw:integer):string;
var
  tmpsjcd,tmpsjx:string;
  i,j,m:integer;
begin
  TMPSJCD:=mid(dta,19,2);
  if TMPSJCD='04' then
  begin
    TMPSJX:=hexdel(mid(dta,21,8));
    if GLXLQX[bw]=mid(tmpsjx,1,2) then
    if GLXLMM[bw]=data_reset(mid(tmpsjx,3,6)) then
    begin
//      for i:=1 to GLGBS do
      for m:=0 to 3 do
      for j:=0 to 4 do
      begin
        db_zdxl[bw,0,m,j]:='0';
        db_zdxl[bw,1,m,j]:='00-00 00:00:00';
        grid3.cells[bw,11]:=db_zdxl[bw,0,m,0]+'kW';
        grid3.cells[bw,12]:=db_zdxl[bw,1,m,0];
      end;
    end;
  end;
  result:='';
end;

function TdbjyForm.mm_set(dta:string;bw:integer):string;
var
  tmpsjcd,tmpsjx,tmpstr:string;
  sndstr:string;
begin
  sndstr:='';
  TMPSJCD:=mid(dta,19,2);
  if tmpsjcd='08' then
  begin
    TMPSJX:=mid(dta,21,16);
    tmpstr:=hexdel(mid(tmpsjx,1,8));
    tmpstr:=leftstr(tmpstr,2)+data_reset(rightstr(tmpstr,6));
    if tmpstr=(GLBCQX[bw]+GLBCMM[bw]) then
    begin
      tmpstr:=hexdel(mid(tmpsjx,9,2));
      GLBCQX[bw]:=tmpstr;
      grid2.cells[4,bw]:=tmpstr;
      tmpstr:=data_reset(hexdel(mid(tmpsjx,11,6)));

⌨️ 快捷键说明

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