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

📄 txjunit1.~pas

📁 实现了断路器群控系统
💻 ~PAS
字号:
unit txjUnit1;

interface

uses
  ADODB, IniFiles, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB, ExtCtrls, DB;

type
  Ttongxinji = class(TForm)
    Label1: TLabel;
    ListBox1: TListBox;
    Label2: TLabel;
    ListBox2: TListBox;
    Button1: TButton;
    Timer1: TTimer;
    MSComm1: TMSComm;
    Button2: TButton;
    Button3: TButton;
    ADOCommand1: TADOCommand;
    ADOQuery1: TADOQuery;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure MSComm1Comm(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  tongxinji: Ttongxinji;
  g_connection : TAdoConnection = nil; //与数据库的连接对象,供系统中其他数据控件与数据库连接使用
  Bresult: boolean =True;
  nCount:integer =0;      //参数序号
  shebexuha:integer =0;   //1为断路器1;2为断路器2;3为断路器3

implementation

{$R *.dfm}
Procedure TimeDelay(DT:DWORD);
var
  TT:DWORD;
begin
  TT:=GetTickCount();
  while GetTickCount()-TT<DT do
    Application.ProcessMessages;
end;

procedure Ttongxinji.Button1Click(Sender: TObject);    //返回按钮
begin
   timer1.Enabled := False;
   close;
end;

procedure Ttongxinji.Timer1Timer(Sender: TObject);      //间隔五秒 轮循发送
var
   str1:string;

   maxA1:integer;                //断路器1的最大负载电流
   maxA2:integer;
   maxA3:integer;

   order1:string;                //闭合/断开电路器1的命令
   order2:string;
   order3:string;

begin

  str1:='select 最大负载电流值 from dbo.电流设定值表 where 编号=1';
  self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  maxA1:=self.ADOQuery1.FieldValues['最大负载电流值'];
  self.ADOQuery1.Close;

  str1:='select 最大负载电流值 from dbo.电流设定值表 where 编号=2';
  self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  maxA2:=self.ADOQuery1.FieldValues['最大负载电流值'];
  self.ADOQuery1.Close;

  str1:='select 最大负载电流值 from dbo.电流设定值表 where 编号=3';
  self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  maxA3:=self.ADOQuery1.FieldValues['最大负载电流值'];
  self.ADOQuery1.Close;

   str1:='select 动作 from dbo.命令 where 设备名=''1''' ;
   self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  order1:=self.ADOQuery1.FieldValues['动作']+'1';
  self.ADOQuery1.Close;

   str1:='select 动作 from dbo.命令 where 设备名=''2''' ;
   self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  order2:=self.ADOQuery1.FieldValues['动作']+'2';
   self.ADOQuery1.Close;

    str1:='select 动作 from dbo.命令 where 设备名=''3''' ;
   self.ADOQuery1.Close;
  self.ADOQuery1.SQL.Clear;
  self.ADOQuery1.SQL.Add(str1);
  self.ADOQuery1.Open;
  order3:=self.ADOQuery1.FieldValues['动作']+'3';
  self.ADOQuery1.Close;

 { str1:='select 最大负载电流值 from dbo.电流设定值表 where 编号=2' ;
  self.ADOQuery2.SQL.Add(str1);
  self.ADOQuery2.Open;
  maxA2:=self.ADOQuery2.FieldValues['最大负载电流值'];
  self.ADOQuery2.Close;

  str1:='select 最大负载电流值 from dbo.电流设定值表 where 编号=3' ;
  self.ADOQuery3.SQL.Add(str1);
  self.ADOQuery3.Open;
  maxA3:=self.ADOQuery3.FieldValues['最大负载电流值'];
  self.ADOQuery3.Close;


  str1:='select 动作 from dbo.命令 where 设备名=''1''' ;
  self.ADOQuery4.SQL.Add(str1);
  self.ADOQuery4.Open;
  order1:=self.ADOQuery4.FieldValues['动作']+'1';
  self.ADOQuery4.Close;

   str1:='select 动作 from dbo.命令 where 设备名=''2''' ;
  self.ADOQuery5.SQL.Add(str1);
  self.ADOQuery5.Open;
  order2:=self.ADOQuery5.FieldValues['动作']+'2';
  self.ADOQuery5.Close;

   str1:='select 动作 from dbo.命令 where 设备名=''3''' ;
  self.ADOQuery6.SQL.Add(str1);
  self.ADOQuery6.Open;
  order3:=self.ADOQuery6.FieldValues['动作']+'3';
  self.ADOQuery6.Close;        }
// showmessage(order1);




 //showmessage(inttostr(maxA2)) ;

    if MSComm1.PortOpen then MSComm1.Output:=order1;
        TimeDelay(100);

     if MSComm1.PortOpen then MSComm1.Output:=order2;
        TimeDelay(100);

     if MSComm1.PortOpen then MSComm1.Output:=order3;
        TimeDelay(100);


    str1:='set A1';

   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
       ListBox1.Items.Add(str1);
       TimeDelay(20);
       MSComm1.Output:=intToStr(maxA1);   //将断路器1的最大负载电流输出
   end
   else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);

    TimeDelay(30);





     str1:='set A2';
   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
       ListBox1.Items.Add(str1);
          TimeDelay(20);
       MSComm1.Output:=intToStr(maxA2);
   end
   else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);
  // showmessage(inttostr(maxA2)) ;
  TimeDelay(30);


    str1:='set A3';
   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
       ListBox1.Items.Add(str1);
      TimeDelay(20);
       MSComm1.Output:=intToStr(maxA3);
   end
   else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);

     TimeDelay(30);



   str1:='read D1';                  //读断路器1
   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
      ListBox1.Items.Add(str1);
  end
  else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);
 TimeDelay(1000);         //延迟时间需大于前置机发送的延迟

  str1:='read D2';                    //读断路器2
   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
      ListBox1.Items.Add(str1);
  end
  else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);
   TimeDelay(1000);

  str1:='read D3';                    //读断路器3
   if MSComm1.PortOpen then begin
      MSComm1.Output:=str1;
      ListBox1.Items.Add(str1);
  end
  else
     Messagedlg('通信端口1未打开!',mtError,[mbok],0);
end;



procedure Ttongxinji.MSComm1Comm(Sender: TObject);
var
  R:string;
  ReceBuf:string;
  strSql:string;
  strDate:string;
  strTime:string;
  canshuming:string;

begin
    ReceBuf:= Trim(MSComm1.Input);
     R:=ReceBuf;

 //showmessage('.'+ReceBuf+'.');
  //TimeDelay(10);
  ListBox2.Items.Add(ReceBuf);

  strDate:= DateToStr(Date());
  strTime:= TimeToStr(Time());
  if ReceBuf='断路器1:' then begin
     shebexuha:=1;
     nCount:=0;
     exit;
  end
  else if ReceBuf='断路器2:' then begin
     shebexuha:=2;
     nCount:=0;
     exit;
  end
  else if ReceBuf='断路器3:' then begin
     shebexuha:=3;
     nCount:=0;
     exit;
    end;

  if shebexuha=1 then begin
   ReceBuf :=COPY(R,1,4);              //断路器1
     nCount:=nCount+1;
     if nCount=1 then canshuming:='电流'
     else if nCount=2 then canshuming:='电压'
     else if nCount=3 then canshuming:='温度'
     else if nCount=4 then canshuming:='开关状态'////
     else shebexuha:=0;

     strSql:='Insert into Canshu(canshuming,canshuzhi,riqi,shijian,shebeiming)';
     strSql:=strSql + 'Values(';
     strSql:=strSql +''''+ canshuming +''''+ ',';
     strSql:=strSql + '''' + ReceBuf + '''' + ',';
     strSql:=strSql + '''' + strDate + '''' + ',';
     strSql:=strSql + '''' + strTime + '''' + ',';
     strSql:=strSql + '''' + '断路器1' + '''' + ')';
     self.ADOCommand1.CommandText:=strSql;
     self.ADOCommand1.Execute;
     exit;
  end;

  if shebexuha=2 then begin
  ReceBuf :=COPY(R,1,4);
     nCount:=nCount+1;
     if nCount=1 then canshuming:='电流'
     else if nCount=2 then canshuming:='电压'
     else if nCount=3 then canshuming:='温度'
     else if nCount=4 then canshuming:='开关状态'
     else shebexuha:=0;
     strSql:='Insert into Canshu(canshuming,canshuzhi,riqi,shijian,shebeiming)';
     strSql:=strSql + 'Values(';
     strSql:=strSql +''''+ canshuming +''''+ ',';
     strSql:=strSql + '''' + ReceBuf + '''' + ',';
     strSql:=strSql + '''' + strDate + '''' + ',';
     strSql:=strSql + '''' + strTime + '''' + ',';
     strSql:=strSql + '''' + '断路器2' + '''' + ')';
     self.ADOCommand1.CommandText:=strSql;
     self.ADOCommand1.Execute;
  end;

  if shebexuha=3 then begin
  ReceBuf :=COPY(R,1,4);
     nCount:=nCount+1;
     if nCount=1 then canshuming:='电流'
     else if nCount=2 then canshuming:='电压'
     else if nCount=3 then canshuming:='温度'
     else if nCount=4 then canshuming:='开关状态'
     else shebexuha:=0;
     strSql:='Insert into Canshu(canshuming,canshuzhi,riqi,shijian,shebeiming)';
     strSql:=strSql + 'Values(';
     strSql:=strSql +''''+ canshuming +''''+ ',';
     strSql:=strSql + '''' + ReceBuf + '''' + ',';
     strSql:=strSql + '''' + strDate + '''' + ',';
     strSql:=strSql + '''' + strTime + '''' + ',';
     strSql:=strSql + '''' + '断路器3' + '''' + ')';
     self.ADOCommand1.CommandText:=strSql;
     self.ADOCommand1.Execute;
  end;

end;

procedure Ttongxinji.FormCreate(Sender: TObject);
var
  strpath:string;     //配置文件路径
  strsyspara:Tinifile;  //系统特殊数据存放文件

begin
  strpath := ExtractFilePath(Application.ExeName);
  strsyspara := Tinifile.Create(strpath+'shenchang_CONFIG.INI');
  if NOT FileExists(strpath+'shenchang_CONFIG.INI')  then
     begin
            showmessage('警告:系统配置文件不存在!');
            Bresult:=False;
            Exit;
     end;
  g_connection:=TADOConnection.Create(nil);
  g_connection.ConnectionString:=strsyspara.ReadString('system','connectionstring','');
  g_connection.LoginPrompt := False;
  g_connection.Open;

  self.ADOCommand1.Connection := g_Connection;
  
  self.ADOQuery1.Connection:= g_Connection;
  {self.ADOQuery2.Connection:= g_Connection;
  self.ADOQuery3.Connection:= g_Connection;
  self.ADOQuery4.Connection:= g_Connection;
  self.ADOQuery5.Connection:= g_Connection;
  self.ADOQuery6.Connection:= g_Connection; }

  //self.ADODataSet1.Connection := g_Connection;

  if Not MSComm1.PortOpen then
  begin
     MSComm1.PortOpen:=True;
     MSComm1.DTREnable:=True;
     MSComm1.RTSEnable:=True;
  end;
  MSComm1.RThreshold := 1;
end;

procedure Ttongxinji.Button2Click(Sender: TObject);     //    开始按钮
begin
   timer1.Enabled := True;
end;

procedure Ttongxinji.Button3Click(Sender: TObject);     //    结束按钮
begin
    timer1.Enabled := False;
end;

end.

⌨️ 快捷键说明

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