?? txjunit1.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; //與數(shù)據(jù)庫的連接對象,供系統(tǒng)中其他數(shù)據(jù)控件與數(shù)據(jù)庫連接使用
Bresult: boolean =True;
nCount:integer =0; //參數(shù)序號
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); //間隔五秒 輪循發(fā)送
var
str1:string;
maxA1:integer; //斷路器1的最大負(fù)載電流
maxA2:integer;
maxA3:integer;
order1:string; //閉合/斷開電路器1的命令
order2:string;
order3:string;
begin
str1:='select 最大負(fù)載電流值 from dbo.電流設(shè)定值表 where 編號=1';
self.ADOQuery1.Close;
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Add(str1);
self.ADOQuery1.Open;
maxA1:=self.ADOQuery1.FieldValues['最大負(fù)載電流值'];
self.ADOQuery1.Close;
str1:='select 最大負(fù)載電流值 from dbo.電流設(shè)定值表 where 編號=2';
self.ADOQuery1.Close;
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Add(str1);
self.ADOQuery1.Open;
maxA2:=self.ADOQuery1.FieldValues['最大負(fù)載電流值'];
self.ADOQuery1.Close;
str1:='select 最大負(fù)載電流值 from dbo.電流設(shè)定值表 where 編號=3';
self.ADOQuery1.Close;
self.ADOQuery1.SQL.Clear;
self.ADOQuery1.SQL.Add(str1);
self.ADOQuery1.Open;
maxA3:=self.ADOQuery1.FieldValues['最大負(fù)載電流值'];
self.ADOQuery1.Close;
str1:='select 動作 from dbo.命令 where 設(shè)備名=''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 設(shè)備名=''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 設(shè)備名=''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 最大負(fù)載電流值 from dbo.電流設(shè)定值表 where 編號=2' ;
self.ADOQuery2.SQL.Add(str1);
self.ADOQuery2.Open;
maxA2:=self.ADOQuery2.FieldValues['最大負(fù)載電流值'];
self.ADOQuery2.Close;
str1:='select 最大負(fù)載電流值 from dbo.電流設(shè)定值表 where 編號=3' ;
self.ADOQuery3.SQL.Add(str1);
self.ADOQuery3.Open;
maxA3:=self.ADOQuery3.FieldValues['最大負(fù)載電流值'];
self.ADOQuery3.Close;
str1:='select 動作 from dbo.命令 where 設(shè)備名=''1''' ;
self.ADOQuery4.SQL.Add(str1);
self.ADOQuery4.Open;
order1:=self.ADOQuery4.FieldValues['動作']+'1';
self.ADOQuery4.Close;
str1:='select 動作 from dbo.命令 where 設(shè)備名=''2''' ;
self.ADOQuery5.SQL.Add(str1);
self.ADOQuery5.Open;
order2:=self.ADOQuery5.FieldValues['動作']+'2';
self.ADOQuery5.Close;
str1:='select 動作 from dbo.命令 where 設(shè)備名=''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的最大負(fù)載電流輸出
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); //延遲時間需大于前置機(jī)發(fā)送的延遲
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:='開關(guān)狀態(tài)'////
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:='開關(guān)狀態(tài)'
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:='開關(guān)狀態(tài)'
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; //系統(tǒng)特殊數(shù)據(jù)存放文件
begin
strpath := ExtractFilePath(Application.ExeName);
strsyspara := Tinifile.Create(strpath+'shenchang_CONFIG.INI');
if NOT FileExists(strpath+'shenchang_CONFIG.INI') then
begin
showmessage('警告:系統(tǒng)配置文件不存在!');
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); // 結(jié)束按鈕
begin
timer1.Enabled := False;
end;
end.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -