📄 project1.~dpr
字号:
procedure TForm1.jzqBHFromDBF();
var
ConStr, s, FrontStr, zz, CodeFormat, JzqHao: string;
jzqbh: array[0..3000] of string;
F1, F: TextFile;
NumWritten, i, j, page, ii, jj, LengthWrite_Buf, RepeatAgainNumber, delaytrue: integer;
Write_Buf, buf, Write_Buffer1: array[1..255 * 80 + 1] of byte;
Write_Buffer: array[0..265] of byte;
FromF, ToF, TempFromF: file of byte;
jzBHtest, sqlstr: string;
WriteNumber, numRead: LongWord;
str1: Word;
huangyu: Boolean;
Sr: TSearchRec;
Err: integer;
TrSize, FilePath: string;
label GOtoNext;
label RepeatAgain, proend;
begin
//if OpenDialog1.Execute then
begin
//2004-02-13加入寰宇版的参数设置
huangyu := false;
zz := ExtractFilePath(Application.Exename); //<----程序所在目录
AssignFile(F, zz + 'CodeFormat.txt'); { File selected in dialog box }
Reset(F);
Readln(F, CodeFormat); { Read the first line out of the file }
CloseFile(F);
//2004-02-13加入寰宇版的参数设置
//2004-02-13
DBGrid2.SelectedRows.Count; //当前行号
Err := FindFirst(DBFdir.Text + '*' + DBGrid2.Fields[0].AsString + '.dbf', $37, Sr);
while Err = 0 do begin
Err := FindNext(Sr);
end;
FindClose(Sr);
// showmessage(sr.Name);
s := Sr.name;
//2004-02-13
if DBGrid1.Columns.Count = 30 then begin
sqlstr := 'select distinct bh,dh,dxh from ' + s + ' order by dh,dxh';
FrontStr := '000000000000';
end;
if DBGrid1.Columns.Count = 23 then begin
sqlstr := 'select distinct UP_BH,dh,dxh from ' + s + ' order by dh,dxh';
huangyu := true;
end;
ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source="'
+ DBFdir.Text //dbf目录
+ '";Extended Properties=dBase 5.0;Persist Security Info=False';
ADOQuery1.ConnectionString := ConStr;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(sqlstr);
ADOQuery1.Active := true;
ADOQuery1.Open;
// DBGrid1.DataSource.DataSet := ADOQuery1;
//以下下载表号
ADOQuery1.Recordset.MoveFirst;
i := 0;
s := copy(s, 1, Pos('.dbf', Lowercase(s)) - 1);
AssignFile(F1, Filedir.Text + '\Su\SU_' + rightstr(s, 4) + '.txt');
Rewrite(F1);
while not ADOQuery1.Eof do begin
jzqbh[i] := FrontStr + trim(ADOQuery1.Fields[0].AsString);
jzqbh[i] := rightstr(jzqbh[i], 12);
if huangyu = true then begin
jzqbh[i] := trim(ADOQuery1.Fields[0].AsString);
jzqbh[i] := CodeFormat + rightstr(jzqbh[i], 7);
end;
//写入文件
jzBHtest := jzqbh[i];
for ii := 1 to 12 do begin
if not (jzBHtest[ii] in ['0'..'9', '.']) then begin
goto GOtoNext;
end
end;
Writeln(F1, jzqbh[i]);
//写入文件
GOtoNext: ADOQuery1.Next;
i := i + 1;
end;
CloseFile(F1);
//以上下载表号
//ADOQuery1
ADOQuery1.ConnectionString := ConStr;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from ' + s);
ADOQuery1.Active := true;
ADOQuery1.Open;
DBGrid1.DataSource.DataSet := ADOQuery1;
DbgridTitle('DBGrid1', s);
//ADOQuery1
//********************************************
AssignFile(FromF, Filedir.Text + '\Su\SU_' + rightstr(s, 4) + '.txt'); //Assign 分配, 指派到FromF文件
Reset(FromF); { Record size = 1 }
for i := 1 to FileSize(FromF) do
Write_Buf[i] := byte($FF); //给全部BUF赋初值
buf[i] := byte($FF);
BlockRead(FromF, buf, FileSize(FromF), numRead); //读数据赋值给BUF
//以下是将数据存入Temp.dat文件
i := 1;
ii := 1;
LengthWrite_Buf := 1;
while i <= FileSize(FromF) do begin
if (buf[i] >= byte($30)) and (buf[i] <= byte($39)) then begin
for j := 1 to 6 do begin
Write_Buffer1[(j - 1) * 2 + 1] := byte(buf[(j - 1) * 2 + 1 + i - 1] and $0F); //39
Write_Buffer1[(j - 1) * 2 + 2] := byte(buf[(j - 1) * 2 + 2 + i - 1] and $0F); //30
Write_Buf[j + ii - 1] := byte((Write_Buffer1[(j - 1) * 2 + 1] shl 4) + Write_Buffer1[(j - 1) * 2 + 2]); //90
LengthWrite_Buf := LengthWrite_Buf + 1;
end;
i := i + 12;
ii := ii + 6;
end
else
i := i + 1;
end;
// showmessage(inttostr(LengthWrite_Buf));
LengthWrite_Buf := LengthWrite_Buf - 1;
AssignFile(ToF, Filedir.Text + '\rd_dat\Temp.dat'); { Open output file }
Rewrite(ToF);
BlockWrite(ToF, Write_Buf, LengthWrite_Buf, LengthWrite_Buf);
CloseFile(FromF);
CloseFile(ToF);
//以上是将数据存入Temp.dat文件
//********************************************
AssignFile(FromF, Filedir.Text + '\rd_dat\Temp.dat'); //Assign 分配, 指派到FromF文件
Reset(FromF);
page := round(FileSize(FromF) / 255);
if round(FileSize(FromF) / 255) * 255 < FileSize(FromF) then
page := round(FileSize(FromF) / 255) + 1;
for i := 1 to page * 255 do
buf[i] := byte($FF);
BlockRead(FromF, buf, FileSize(FromF), numRead); //读数据赋值给BUF
Write_Buffer[0] := byte($EA); //$ 表示16进制
Write_Buffer[1] := byte($EA);
//以下是控制码
Write_Buffer[2] := byte($2C); //有后续报文时
DBGrid2.SelectedRows.Count; //当前行号
s := rightstr(DBGrid2.Fields[0].AsString, 4);
Write_Buffer[3] := HexToDec(copy(s, 3, 4)); // byte($FA);
Write_Buffer[4] := HexToDec(copy(s, 1, 2)); //byte($01);
Write_Buffer[5] := byte($FF);
receive.Lines.Add('正在与集中器通讯中...');
for i := page downto 1 do {//块号从大到小} begin
if i = 1 then
Write_Buffer[2] := byte($0C); //无后续报文时
for j := 1 to 255 do
Write_Buffer[j + 5] := buf[j + (page - i) * 255];
str1 := 0; //必须先赋值
//以下是帧编码
Write_Buffer[261] := byte(i); //前8位;
Write_Buffer[262] := byte($00); //后8位
for ii := 0 to 262 do
str1 := str1 + Write_Buffer[ii];
Write_Buffer[263] := byte(str1 and $00FF);
Write_Buffer[264] := byte((str1 shr 8) and $00FF);
Write_Buffer[265] := byte($DD);
if (hcom = 0) then goto proend;
RepeatAgainNumber := 0;
RepeatAgain: writefile(hcom, Write_Buffer, length(Write_Buffer), WriteNumber, nil);
delaytrue := DelayTest();
if delaytrue = 1 then begin
goto proend
end;
if delaytrue = 2 then begin
comcolorRed();
RadioGroup1.Enabled := true;
delete.Enabled := true;
Button1.Enabled := true;
ComboBox1.Enabled := true;
PopupMenu1true();
exit;
end;
Edit1.Text := '6.4'; //置一个标志表示是下载局编号
Edit2.Text := IntToStr(page - i + 1);
if ParseDataOnlyOne(11) = 1 then {//0 表示成功 1 表示失败} begin
RepeatAgainNumber := RepeatAgainNumber + 1;
if RepeatAgainNumber = 5 then goto proend;
goto RepeatAgain;
end;
RepeatAgainNumber := 0;
// 实时处理数据
end;
proend: CloseHandle(hcom);
comcolorRed();
RadioGroup1.Enabled := true;
delete.Enabled := true;
Button1.Enabled := true;
ComboBox1.Enabled := true;
PopupMenu1true();
exit;
end;
end;
procedure TForm1.test(Column: TColumn);
var
i, j: integer;
S: string;
begin
if DBGrid1.Columns.Count > 1 then begin
DBGrid1.PopupMenu := PopupMenu2;
DBGrid1.SelectedRows.Count; //当前行号
S := DBGrid1.Fields[1].AsString;
// showmessage(S);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i: integer;
begin
DBGrid1.DataSource.DataSet.DisableControls;
{ FileListBox1.ApplyFilePath ('D:\Documents and Settings\user\桌面\dbf');
for I := 0 to FileListBox1.Items.Count - 1 do
FileListBox1.Items[I] := ChangeFileExt(FileListBox1.Items[I], '');
filelistbox1.Refresh; }
end;
procedure TForm1.FormCreate(Sender: TObject);
var
ConStr, S, zz, CBJzz, JZQzz: string;
z1: array[0..254] of char;
begin
{
dbgrid1.SelectedRows.Count;
//当前行号
dbgrid1.Fields[1].AsString;
//取第二个字段的值
}
comcolorRed();
DBGrid3.Visible := false;
Shape2.Brush.Color := clred;
Shape2.Visible := false;
getcurrentdirectory(254, z1);
zz := string(z1); //<-----当前目录
//showmessage(zz);
Filedir.Text := zz;
//2002-4-1加入
CBJzz := zz + '\FSJC_CBJ.dbf';
JZQzz := zz + '\FSJC_JZQ.dbf';
//CopyFile(Pchar(CBJzz), Pchar('c:\FSJC_CBJ.dbf'), false);
//CopyFile(Pchar(JZQzz), Pchar('c:\FSJC_JZQ.dbf'), false);
//2002-4-1加入
FileListBox1.Clear;
ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source="'
+ ExtractFilePath(zz + '\Dbf\jzq.dbf')
+ '";Extended Properties=dBase 5.0;Persist Security Info=False';
S := ExtractFileName(zz + '\Dbf\jzq.dbf');
S := copy(S, 1, Pos('.dbf', Lowercase(S)) - 1);
ADOQuery2.ConnectionString := ConStr;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add('select * from ' + S);
ADOQuery2.Active := true;
ADOQuery2.Open;
DBGrid2.DataSource.DataSet := ADOQuery2;
DbgridTitle('DBGrid2',S);
end;
procedure TForm1.ChoseDbf(Sender: TObject);
var
ConStr, S: string;
var z1: array[0..254] of char; zz: string;
begin
getcurrentdirectory(254, z1);
zz := string(z1);
ShowMessage(zz);
// filelistbox1.Clear;
ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source="'
+ ExtractFilePath(FileListBox1.filename)
+ '";Extended Properties=dBase 5.0;Persist Security Info=False';
S := ExtractFileName(FileListBox1.filename);
// S := Copy(S, 1, Pos('.dbf', Lowercase(S)) -1);
ADOQuery1.ConnectionString := ConStr;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select * from ' + S);
ADOQuery1.Active := true;
ADOQuery1.Open;
DBGrid1.DataSource.DataSet := ADOQuery1;
end;
procedure TForm1.FileListBox1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
if (FileListBox1.Items.Count <> 0) and (FileListBox1.filename <> '') then //0代表无文件在filelistbox里
FileListBox1.PopupMenu := PopupMenu1;
end;
//读时间 设置时间 读版本 集中器清空 集中器复位
procedure TForm1.Receivedata(datastring: string);
var
Str, TempString, JzpYear, NowTime, ConStr, updatejzq, JzqHao: string;
inbuff: array[0..2048] of byte;
Bytesread, dwError: LongWord;
cs: Tcomstat;
i, delaytrue: integer;
dwErrorFlags: Dword;
hReader: TReader; // handle of data reader
label proend;
begin
try
delay(500);
delaytrue := DelayTest();
if delaytrue = 1 then {//8秒之后退出} begin
CloseHandle(hcom);
goto proend;
end;
if delaytrue = 2 then {//中断退出} begin
goto proend;
end;
//Delay(500);
DBGrid2.SelectedRows.Count; //当前行号
JzqHao := DBGrid2.Fields[0].AsString;
//showmessage(dbgrid2.Fields[0].AsString);
receive.Text := '';
ClearCommError(hcom, dwError, @cs);
if (hcom = 0) then goto proend; ;
//cbInQue 在输入缓冲区中还没有被readfile函数读取的字节数
if not ReadFile(hcom, inbuff, cs.cbInQue, Bytesread, nil) then exit;
if cs.cbInQue > sizeof(inbuff) then begin //超过最大字节数
PurgeComm(hcom, PURGE_RXCLEAR);
CloseHandle(hcom);
goto proend;
end;
for i := 0 to cs.cbInQue - 1 do {// SizeOf(TComStat)} begin
Str := Format('%2x', [inbuff[i]]);
TempString := TempString + Str;
end;
//以下是解析时间
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -