📄 unit1.pas
字号:
unit unit1;
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;
ADOTable1: TADOTable;
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为空调;2为UPS
meijilu :array[0..12] of integer;
dianjilu: array[0..12] of integer;
shuijilu: array[0..12] of integer;
idn:integer=1;
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
ReceBuf:string[255];
strSql:string;
strDate:string;
strTime:string;
canshuming:string;
i:integer;
a:integer;
b:integer;
j:integer;
count:integer;
temp:string;
st:string;
temp1:integer;
time2:Tdatetime;
str1:string;
tag:boolean;
begin
// time2:=date();
strDate:= DateToStr(Date());
strTime:= TimeToStr(Time());
tag:=false;
if(strTime[1]='1')and(strTime[2]='1')and(strTime[3]=':')and(strTime[4]='0')and(strTime[5]='4')and(tag=false)
then
begin
str1:='抄表';
if MSComm1.PortOpen then begin
MSComm1.Output:=str1;
ListBox1.Items.Add(str1);
tag:=true;
end
else
Messagedlg('通信端口1未打开!',mtError,[mbok],0);
TimeDelay(1000);
end;
tag:=false;
{ timedelay(500);
ReceBuf := Trim(MSComm1.Input);
ListBox2.Items.Add(ReceBuf);
a:=1 ;
count:=1;
if recebuf<>'*'
then
begin
if recebuf<>''
then
begin
for i:=1 to 255
do
begin
if a=1
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
count:= strtoint(strsql);
a:=2;
strsql:='';
continue;
end;
end;
if a=2
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
meijilu[count]:= strtoint(strsql);
a:=3;
strsql:='';
continue;
end;
end;
if a=3
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
dianjilu[count]:= strtoint(strsql);
a:=4;
strsql:='';
continue;
end;
end;
if a=4
then
begin
if (recebuf[i]<>'&')and (a=4)
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
shuijilu[count]:= strtoint(strsql);
a:=1;
strsql:='';
continue;
end;
end;
end;
end;
end
else
showmessage('通讯故障');
if count=12
then
begin
// i:=1;
self.ADOTable1.Close;
self.ADOTable1.Open;
temp:=self.ADOTable1.FieldValues['采集号'];
// self.ADOCommand1.CommandText:=strSql;
st:='';
// self.ADOCommand1.Execute;
b:=1;
b:=length(temp);
for j:=1 to b
do
begin
if (temp[j]<>' ')
then
st:=st+temp[j];
end;
idn:=strtoint(st);
temp1:=idn;
for i:=1 to 12
do
begin
// strSql:='select 采集号 from caijihao' ;
{ strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate + '''' + ',';
strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(dianjilu[i]) + '''' + ')'; }
{
strSql:='Insert into dian(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime +'''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(dianjilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
strSql:='Insert into mei(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime +'''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(meijilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
strSql:='Insert into shui(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime + '''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(shuijilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
idn:=idn+1;
end;
strSql:='update caijihao set 采集号 =(''';
strSql:=strSql +inttostr(idn)+''')';
strSql:=strSql +'where 采集号='''+temp +'''';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
end;
end;
}
end;
procedure Ttongxinji.MSComm1Comm(Sender: TObject);
var
ReceBuf:string[255];
strSql:string;
strDate:string;
strTime:string;
canshuming:string;
i:integer;
a:integer;
b:integer;
j:integer;
count:integer;
temp:string;
st:string;
temp1:integer;
begin
timedelay(500);
ReceBuf := Trim(MSComm1.Input);
ListBox2.Items.Add(ReceBuf);
strDate:= DateToStr(Date());
strTime:= TimeToStr(Time());
a:=1 ;
count:=1;
if recebuf<>'*'
then
begin
if recebuf<>''
then
begin
for i:=1 to 255
do
begin
if a=1
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
count:= strtoint(strsql);
a:=2;
strsql:='';
continue;
end;
end;
if a=2
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
meijilu[count]:= strtoint(strsql);
a:=3;
strsql:='';
continue;
end;
end;
if a=3
then
begin
if (recebuf[i]<>'&')
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
dianjilu[count]:= strtoint(strsql);
a:=4;
strsql:='';
continue;
end;
end;
if a=4
then
begin
if (recebuf[i]<>'&')and (a=4)
then
begin
strsql:=strsql+recebuf[i];
end
else
begin
shuijilu[count]:= strtoint(strsql);
a:=1;
strsql:='';
continue;
end;
end;
end;
end;
end
else
showmessage('通讯故障');
if count=12
then
begin
// i:=1;
self.ADOTable1.Close;
self.ADOTable1.Open;
temp:=self.ADOTable1.FieldValues['采集号'];
// self.ADOCommand1.CommandText:=strSql;
st:='';
// self.ADOCommand1.Execute;
b:=1;
b:=length(temp);
for j:=1 to b
do
begin
if (temp[j]<>' ')
then
st:=st+temp[j];
end;
idn:=strtoint(st);
temp1:=idn;
for i:=1 to 12
do
begin
// strSql:='select 采集号 from caijihao' ;
{ strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate + '''' + ',';
strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(dianjilu[i]) + '''' + ')'; }
strSql:='Insert into dian(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime +'''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(dianjilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
strSql:='Insert into mei(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime +'''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(meijilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
strSql:='Insert into shui(户主编号,采集号,采集时间,度数)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ inttostr(i) +''''+ ',';
strSql:=strSql + '''' + inttostr(idn) + '''' + ',';
strSql:=strSql + '''' + strDate +' '+ strTime + '''' + ',';
// strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + inttostr(shuijilu[i]) + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
idn:=idn+1;
end;
strSql:='update caijihao set 采集号 =(''';
strSql:=strSql +inttostr(idn)+''')';
strSql:=strSql +'where 采集号='''+temp +'''';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
end;
end;
{ if ReceBuf='空调:' then begin
shebexuha:=1;
nCount:=0;
exit;
end
else if ReceBuf='UPS:' then begin
shebexuha:=2;
nCount:=0;
exit;
end;
if shebexuha=1 then begin
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 if nCount=5 then canshuming:='加湿/除湿状态'
else shebexuha:=0;
strSql:='Insert into Canshu(canshuming,canshuzhi,riqi,shijian,shebeming)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ canshuming +''''+ ',';
strSql:=strSql + '''' + ReceBuf + '''' + ',';
strSql:=strSql + '''' + strDate + '''' + ',';
strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + '空调' + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
exit;
end;
if shebexuha=2 then begin
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,shebeming)';
strSql:=strSql + 'Values(';
strSql:=strSql +''''+ canshuming +''''+ ',';
strSql:=strSql + '''' + ReceBuf + '''' + ',';
strSql:=strSql + '''' + strDate + '''' + ',';
strSql:=strSql + '''' + strTime + '''' + ',';
strSql:=strSql + '''' + 'UPS' + '''' + ')';
self.ADOCommand1.CommandText:=strSql;
self.ADOCommand1.Execute;
end; }
procedure Ttongxinji.FormCreate(Sender: TObject);
var
strpath:string; //配置文件路径
strsyspara:Tinifile; //系统特殊数据存放文件
i:integer;
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.ADOTable1.Connection:=g_Connection;
self.ADOTable1.TableName:='caijihao';
self.ADOTable1.Active:=true;
//self.ADODataSet1.Connection := g_Connection;
if Not MSComm1.PortOpen then
begin
MSComm1.PortOpen:=True;
MSComm1.DTREnable:=True;
MSComm1.RTSEnable:=True;
end;
MSComm1.RThreshold := 1;
for i:=0 to 12
do
begin
meijilu[i]:=0;
dianjilu[i]:=0;
shuijilu[i]:=0;
end;
end;
procedure Ttongxinji.Button2Click(Sender: TObject);
var
str1:string;
begin
str1:='抄表';
if MSComm1.PortOpen then begin
MSComm1.Output:=str1;
ListBox1.Items.Add(str1);
end
else
Messagedlg('通信端口1未打开!',mtError,[mbok],0);
TimeDelay(1000);
{ 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 + -