?? term.pas
字號:
unit term;
interface
uses
record_define,IdTCPClient,SysUtils,ADODB,Dialogs;
type
TTerm = class
private
public
TER_SEQUENCE:integer;
gprs_id:integer;
term_id:byte;
app_id:integer;
well_id:integer;
summit_type:byte;
collect_inteval:integer;
packet_inteval:integer;
remedy_count_max:byte;
remedy_cound:byte;
remedy_interval:integer;
second_count:integer;
obtained:boolean;
worked:boolean;
H10_data:TCommBlock;
send_count:integer;
receive_count:integer;
all_respond_time:int64;
start_time:TDateTime;
end_Time:TDateTime;
procedure compose_data;
procedure check_second;
procedure send_data;
procedure save_data(data_time: TDatetime; data_count: integer;buf:array of char ;start_pos: byte;ADOStoredProc1: TADOStoredProc);
procedure save_data2(data_time: TDatetime; data_count: integer;buf:array of char ;start_pos: byte;ADOStoredProc1: TADOStoredProc);
procedure deal_data(buf:array of char;data_len:byte);
procedure get_temp_data;
procedure write_info_data;
end;
implementation
uses Unit1,TcpMod, Unit3, Gprs_Interface;
{ TTerm }
procedure TTerm.check_second;
var
str:string;
begin
second_count:=second_count-(mainform.timer1.Interval div 1000);
str:='odtained:'+booltostr(obtained)+' remedy_cound:'+inttostr(remedy_cound)+' remedy_count_max:'+inttostr(remedy_count_max);
if second_count<1 then
begin
compose_data;
send_data;
//self.get_temp_data;
obtained:=false;
if summit_type=1 then
second_count:=collect_inteval
else
second_count:=packet_inteval;
end
else if (obtained=false) and(remedy_cound<remedy_count_max) and (second_count mod remedy_interval=0) then
//else if (obtained=false) and (remedy_cound<remedy_count_max) then
begin
remedy_cound:=remedy_cound+1;
compose_data;
send_data;
//self.get_temp_data;
end;
end;
procedure TTerm.compose_data;
var
time_str:string;
begin
time_str:= FormatDateTime('yyyy-mm-dd hh:mm:ss', Now);
with H10_data do
begin
data_long:=24;
tcp_data[0]:=char($10);
tcp_data[1]:=char(gprs_id);
tcp_data[2]:=char(gprs_id shr 8);
tcp_data[3]:=char(gprs_id shr 16);
tcp_data[4]:=char(0);
tcp_data[5]:=char(0);
tcp_data[6]:=char(0);
tcp_data[7]:=char(0);
tcp_data[8]:=char(strtoint(copy(time_str,18,2)));
tcp_data[9]:=char(strtoint(copy(time_str,15,2)));
tcp_data[10]:=char(strtoint(copy(time_str,12,2)));
tcp_data[11]:=char(strtoint(copy(time_str,9,2)));
tcp_data[12]:=char(strtoint(copy(time_str,6,2)));
tcp_data[13]:=char(strtoint(copy(time_str,3,2)));
tcp_data[14]:=char(dayofweek(now));
tcp_data[15]:=char(20);
tcp_data[20]:=char(summit_type);
tcp_data[21]:=char(term_id);
end;
end;
procedure TTerm.deal_data(buf:array of char;data_len:byte);
var
data_time:TDatetime;
time_str:string;
packet_count,item_count,i:byte;
begin
receive_count:=receive_count+1;
if summit_type=1 then
all_respond_time:=all_respond_time+collect_inteval-second_count
else
all_respond_time:=all_respond_time+packet_inteval-second_count;
//如果是空包則退出
if buf[32]=char(0) then
exit;
try
time_str:='200'+inttostr(BCD_to_Hex(integer(buf[27])))+'-';
time_str:=time_str+inttostr(BCD_to_Hex(integer(buf[26])))+'-';
time_str:=time_str+inttostr(BCD_to_Hex(integer(buf[25])))+' ';
time_str:=time_str+inttostr(BCD_to_Hex(integer(buf[24])))+':';
time_str:=time_str+inttostr(BCD_to_Hex(integer(buf[23])))+':';
time_str:=time_str+inttostr(BCD_to_Hex(integer(buf[22])));
data_time:=strtodatetime(time_str);
except
write_text('轉(zhuǎn)換時間出錯 '+FormatDateTime('yyyy-mm-dd hh:mm:ss', Now));
exit;
end;
{if data_len-integer(buf[32])>=33 then
begin
obtained:=true;
remedy_cound:=0;
item_count:=integer(buf[31]);
packet_count:=integer(buf[32]) div (item_count*4);
for i:=0 to packet_count-1 do
begin
self.save_data(data_time,item_count,buf,33+i*item_count*4,TcpModule.ADOStoredProc1);
data_time:=data_time+collect_inteval/(24*60*60);
end;
end;}
//040406添加 短距測試用
if data_len-integer(buf[32])>=33 then
begin
//040301 app_id=7 短距測試用
if self.app_id<>7 then
begin
obtained:=true;
item_count:=integer(buf[31]);
packet_count:=integer(buf[32]) div (item_count*4);
for i:=0 to packet_count-1 do
begin
self.save_data(data_time,item_count,buf,33+i*item_count*4,TcpModule.ADOStoredProc1);
data_time:=data_time+collect_inteval/(24*60*60);
end;
end
else
begin
obtained:=true;
item_count:=integer(buf[31]);
packet_count:=integer(buf[32]) div (item_count*2);
for i:=0 to packet_count-1 do
begin
self.save_data2(data_time,item_count,buf,33+i*item_count*2,TcpModule.ADOStoredProc1);
data_time:=data_time+collect_inteval/(24*60*60);
end;
end;
end;
end;
procedure TTerm.get_temp_data;
begin
if self.gprs_id=$AAAAAA then
begin
compose_data;
send_data;
end;
end;
procedure TTerm.save_data(data_time: TDatetime; data_count: integer;
buf:array of char ;start_pos: byte;ADOStoredProc1: TADOStoredProc);
var
sql_str,item_str:string;
i:byte;
temp_data:integer;
task_id:byte;
begin
{sql_str:='select * from data_table where APP_ID=';
sql_str:=sql_str+inttostr(app_id)+' and WELL_ID=';
sql_str:=sql_str+inttostr(well_id)+' and COLLECT_TIME=to_date('+#39;
sql_str:=sql_str+FormatDateTime('yyyy-mm-dd hh:mm:ss',data_time)+#39+','+#39+'YYYY-MM-DD HH24:MI:SS'+#39+')';
write_text(sql_str);
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(sql_str);
ADOQuery1.Open;
if ADOQuery1.RecordCount=0 then
begin
ADOQuery1.Append;
ADOQuery1.FieldByName('APP_ID').AsInteger:=app_id;
ADOQuery1.FieldByName('WELL_ID').AsInteger:=WELL_id;
ADOQuery1.FieldByName('COLLECT_TIME').AsDateTime:=data_time;
ADOQuery1.FieldByName('DATA_COUNT').AsInteger:=data_count;
for i:=1 to data_count do
begin
item_str:='DATA'+inttostr(i);
move(buf[start_pos+(i-1)*4],temp_data,4);
ADOQuery1.FieldByName(item_str).AsInteger:=temp_data;
end;
ADOQuery1.Post;
end;
ADOQuery1.Close;}
//task_id:=integer(buf[0]);
try
//ADOStoredProc1.Close;
//ADOStoredProc1.Prepared;
ADOStoredProc1.Parameters.ParamByName('app_id').Value:=app_id;
ADOStoredProc1.Parameters.ParamByName('well_id').Value:=well_id;
ADOStoredProc1.Parameters.ParamByName('collect_time').Value:=data_time;
ADOStoredProc1.Parameters.ParamByName('data_count').Value:=data_count;
for i:=1 to data_count do
begin
item_str:='DATA'+inttostr(i);
move(buf[start_pos+(i-1)*4],temp_data,4);
ADOStoredProc1.Parameters.ParamByName(item_str).Value:=temp_data;
//****************************************************************
//added for opc Server 2004-11-13 by HF
if Assigned(GPRS_Intf) then
begin
GPRS_Intf.UpdateTag(well_id,i,temp_data,data_time);
end;
//****************************************************************
end;
ADOStoredProc1.ExecProc;
except
write_text('寫入數(shù)據(jù)出錯 '+inttostr(app_id)+' '+FormatDateTime('yyyy-mm-dd hh:mm:ss', Now));
ADOStoredProc1.Connection.Close;
sleep(1000);
ADOStoredProc1.Connection.Open;
sleep(15000);
end;
end;
procedure TTerm.send_data;
var
str:string;
buf: array [0..255] of char;
data_len,i: byte;
begin
//040518修改/////////////////////////////////////////
{sleep(10);
try
if TcpModule.IdTCPClient1.Connected=true then
begin
TcpModule.IdTCPClient1.WriteBuffer (H10_data, SizeOf (H10_data), true);
send_count:=send_count+1;
end
else
begin
TcpModule.Tcp_disconnect;
sleep(1000);
TcpModule.Tcp_connect;
//showmessage('wait');
sleep(1000);
if TcpModule.IdTCPClient1.Connected=true then
begin
TcpModule.IdTCPClient1.WriteBuffer (H10_data, SizeOf (H10_data), true);
send_count:=send_count+1;
end;
end;
except
str:= FormatDateTime('yyyy-mm-dd hh:mm:ss', Now);
write_text('發(fā)送數(shù)據(jù)出錯 '+str);
end; }
////////////////////////////////////////////////////////
//040518添加
data_len:=byte(H10_data.data_long);
for i:=0 to data_len-1 do
begin
buf[i]:=H10_data.tcp_data[i];
end;
TcpModule.send_data(buf,data_len);
end;
procedure TTerm.write_info_data;
var
str:string;
ter_id:integer;
begin
str:='select * from ter_info where rownum<1';
TcpModule.ADOQuery1.Close;
TcpModule.ADOQuery1.SQL.Clear;
TcpModule.ADOQuery1.SQL.Add(str);
TcpModule.ADOQuery1.Open;
TcpModule.ADOQuery1.Append;
TcpModule.ADOQuery1.FieldByName('TER_SEQUENCE').AsInteger:=TER_SEQUENCE;
TcpModule.ADOQuery1.FieldByName('START_TIME').AsDateTime:=start_time;
TcpModule.ADOQuery1.FieldByName('END_TIME').AsDateTime:=now;
TcpModule.ADOQuery1.FieldByName('SEND_COUNT').AsInteger:=send_count;
TcpModule.ADOQuery1.FieldByName('RECEIVE_COUNT').AsInteger:=receive_count;
if receive_count<>0 then
TcpModule.ADOQuery1.FieldByName('AVG_RESPOND_TIME').AsFloat:=all_respond_time/receive_count
else
TcpModule.ADOQuery1.FieldByName('AVG_RESPOND_TIME').AsFloat:=0;
TcpModule.ADOQuery1.Post;
TcpModule.ADOQuery1.Close;
send_count:=0;
receive_count:=0;
all_respond_time:=0;
start_time:=now;
end;
procedure TTerm.save_data2(data_time: TDatetime; data_count: integer;
buf: array of char; start_pos: byte; ADOStoredProc1: TADOStoredProc);
var
sql_str,item_str:string;
i,j,pos:byte;
temp_data:integer;
task_id:byte;
begin
//2004-02-13 調(diào)試短距無線終端加入
//短距無線以2字節(jié)表示一個數(shù)據(jù),應(yīng)用號為80以后
try
ADOStoredProc1.Close;
ADOStoredProc1.Prepared;
ADOStoredProc1.Parameters.ParamByName('app_id').Value:=app_id;
ADOStoredProc1.Parameters.ParamByName('well_id').Value:=well_id;
ADOStoredProc1.Parameters.ParamByName('collect_time').Value:=data_time;
ADOStoredProc1.Parameters.ParamByName('data_count').Value:=8;
for i:=1 to 10 do
begin
pos:=start_pos+(i-1)*16;
ADOStoredProc1.Parameters.ParamByName('app_id').Value:=app_id;
ADOStoredProc1.Parameters.ParamByName('well_id').Value:=well_id+i-1;
ADOStoredProc1.Parameters.ParamByName('collect_time').Value:=data_time;
ADOStoredProc1.Parameters.ParamByName('data_count').Value:=8;
for j:=1 to 8 do
begin
item_str:='DATA'+inttostr(j);
move(buf[pos+(j-1)*2],temp_data,2);
ADOStoredProc1.Parameters.ParamByName(item_str).Value:=temp_data;
end;
ADOStoredProc1.ExecProc;
end;
except
write_text('寫入數(shù)據(jù)出錯 '+inttostr(app_id)+' '+FormatDateTime('yyyy-mm-dd hh:mm:ss', Now));
end;
end;
end.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -