📄 unit1.pas
字号:
//加入中断
if Break.Text = '1' then begin
Break.Text := '0';
receive.Lines.Add('通信被中断---通信结束');
comcolorRed();
Result := 2; //2<-----中断退出
exit;
end;
//加入中断
end;
Result := 0;
end;
//**********************************************************8
//procedure TForm1.comminitialize; //串行口初始化
function TForm1.comminitialize(): string;
var
lpdcb: Tdcb;
cbNum, numRead: Cardinal;
Temp: string;
comnumber: string;
label opencomm;
begin
Temp := 'Com' + IntToStr(RadioGroup1.ItemIndex + 1);
comnumber := IntToStr(RadioGroup1.ItemIndex + 1);
if (comnumber = '1') and (Shape1.Brush.Color = clred) then goto opencomm;
if (comnumber = '2') and (Shape2.Brush.Color = clred) then goto opencomm;
Result := '1'; //失败
exit;
opencomm: hcom := createfile(PCHAR(Temp), generic_read or generic_write, 0, nil,
open_existing,
0, // FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED
0); //打开串行口
if hcom = invalid_handle_value then begin
ShowMessage('端口打开失败');
comcolorRed();
CloseHandle(hcom);
Result := '1'
end
else begin
// messagebox(0,'端口打开','打开端口',MB_OK);
comcolorGreen();
getcommstate(hcom, lpdcb);
//获取串行口当前默认设置
case ComboBox1.ItemIndex of
0: lpdcb.BaudRate := 9600;
1: lpdcb.BaudRate := 1200;
end;
lpdcb.StopBits := Onestopbit;
lpdcb.ByteSize := 8;
lpdcb.Parity := NOParity;
if not Setcommstate(hcom, lpdcb) then begin
messagebox(0, '不能设置端口', '', MB_OK);
CloseHandle(hcom);
Result := '1';
exit;
end;
end; //setupcomm(hcom,4096,4096);
//设置输入,输出缓冲区皆为4096字节 if 语句结束
setcommMask(hcom, ev_rxchar);
//指定串行口事件为接收到字符;
end;
//以下是实时抄收 实时补抄 点抄接受数据
function TForm1.receive656667(datastring: string): integer;
var
WriteNumber, numRead: LongWord;
Write_Buffer, buf, AddBuffer, AddBuffer1: array[0..10] of byte;
TemWrite_Buffer, str2, TempString, S, temstr11: string;
lw: LongWord;
i, ff, ReceiveLen, j, page, ii, Temp_len, DelayNum, DelaySecond, delaytrue: integer;
str1: Word; //byte;
NowDate: tdatetime;
dwEvtMask, dwError: Dword;
wait: Boolean;
cs: Tcomstat;
inbuff, Temp_buff: array[0..255 * 80] of byte;
Bytesread: LongWord;
ToF: file of byte;
LengthWrite_Buf: integer;
begin
try
delaytrue := DelayTest();
if delaytrue = 1 then begin
CloseHandle(hcom);
comcolorRed();
exit;
end;
if delaytrue = 2 then {//中断退出} begin
comcolorRed();
exit;
end;
ClearCommError(hcom, dwError, @cs);
ReceiveLen := 0;
j := 1;
page := 0;
DelaySecond := 1;
for i := 1 to 255 * 80 do begin
inbuff[i] := byte($00);
Temp_buff[i] := byte($00);
end;
while DelaySecond < 640 do {//125毫秒延时} begin
while cs.cbInQue > 0 do begin
if ReadFile(hcom, inbuff, cs.cbInQue, Bytesread, nil) then begin
{将inbuff加入Temp_buff}
for Temp_len := 1 to cs.cbInQue do
Temp_buff[ReceiveLen + Temp_len] := inbuff[Temp_len - 1];
ReceiveLen := ReceiveLen + cs.cbInQue;
{将inbuff加入Temp_buff}
//判断是否失败
if ReceiveLen = 11 then begin
for ii := 1 to 11 do // SizeOf(TComStat)
TempString := TempString + Format('%2x', [Temp_buff[ii]]);
if copy(TempString, 5, 2) = datastring then begin
CloseHandle(hcom);
receive.Lines.Add('通信过程已结束,操作失败!');
comcolorRed();
receive.Lines.Add(TempString);
exit
end;
end;
//判断是否失败
if ReceiveLen - (page + 1) * 26 >= 0 then begin
TempString := '';
for ii := 1 to 26 do {// SizeOf(TComStat)} begin
//TempString := TempString +
temstr11 := Format('%2x', [Temp_buff[ii + page * 26]]);
TempString := TempString + rightstr(('00' + trim(temstr11)), 2)
end;
page := page + 1;
//DBGrid1.DataSource.dataset.DisableControls;
ParseTempString(TempString);
// receive.Lines.Add('正与集中器通信中,请稍后...');
end;
end;
ClearCommError(hcom, dwError, @cs);
DelaySecond := 0; //有数据
end;
Delay(13);
ClearCommError(hcom, dwError, @cs);
if (hcom = 0) then exit;
DelaySecond := DelaySecond + 1; //13毫秒后无数据
if Break.Text = '1' then begin
Break.Text := '0';
receive.Lines.Add('通信被中断---通信结束');
comcolorRed();
exit;
end;
end; {end if while}
CloseHandle(hcom);
receive.Lines.Add('通信过程已结束');
comcolorRed();
except
end;
//DBGrid1.DataSource.dataset.EnableControls;
end;
//以上是实时抄收 实时补抄 点抄接受数据
//6.5点抄 6.6 实时抄收6.7 实时补抄
function TForm1.ParseTempString(datastring: string): integer;
var
i, ii, updatenumber: integer;
BiaoHao, Zong, Feng, Gu, ConStr, S, NowTime, Updatezong, Updatefeng, Updategu: string;
begin
DBGrid2.SelectedRows.Count; //当前行号
S := DBGrid2.Fields[0].AsString;
BiaoHao := '';
Zong := '';
Feng := '';
Gu := '';
ii := 4;
for i := 1 to 12 do begin
BiaoHao := BiaoHao + copy(datastring, ii, 1);
ii := ii + 2;
end;
BiaoHao := rightstr(BiaoHao, 9);
Zong := Zong + copy(datastring, 27, 6) + '.' + copy(datastring, 33, 2);
Feng := Feng + copy(datastring, 35, 6) + '.' + copy(datastring, 41, 2);
Gu := Gu + copy(datastring, 43, 6) + '.' + copy(datastring, 49, 2);
receive.Lines.Add(' 电表局编号:' + BiaoHao + ' 总: ' + Zong + ' 峰: ' + Feng + ' 谷: ' + Gu);
//*******************************
if ExistDBF.Text = '1' then {//1代表有DBF} begin
DBGrid3.SetFocus;
if DBGrid1.Columns.Count = 30 then
ADOQuery3.Locate('bh', BiaoHao, []);
if DBGrid1.Columns.Count = 23 then
ADOQuery3.Locate('UP_BH', BiaoHao, []);
end;
//*******************************
NowTime := FormatDateTime('yyyy/mm/dd hh:mm:ss', now);
if ExistDBF.Text = '1' then begin
//if ExistDBF.Text = 'pppppppp' then begin
if (Zong <> 'FFFFFF.FF') and (BiaoHao <> 'FFFFFFFFF') then //不是空值或错误
{//填加到数据库中} begin
ADOQuery1.First;
// DBGrid3.Enabled:=true;
while not ADOQuery1.Eof do begin
//********苏源系统*********
if DBGrid1.Columns.Count = 30 then begin
if (ADOQuery1.Fields[14].AsString = '4') and (ADOQuery1.Fields[1].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[3].AsString := Zong;
ADOQuery1.Fields[7].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
if (ADOQuery1.Fields[14].AsString = '1') and (ADOQuery1.Fields[1].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[3].AsString := Feng;
ADOQuery1.Fields[7].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
if (ADOQuery1.Fields[14].AsString = '3') and (ADOQuery1.Fields[1].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[3].AsString := Gu;
ADOQuery1.Fields[7].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
end;
//********苏源系统*********
//********寰宇系统*********
if DBGrid1.Columns.Count = 23 then begin
if (ADOQuery1.Fields[12].AsString = '2') and (ADOQuery1.Fields[0].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[1].AsString := Zong;
ADOQuery1.Fields[5].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
if (ADOQuery1.Fields[12].AsString = '4') and (ADOQuery1.Fields[0].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[1].AsString := Feng;
ADOQuery1.Fields[5].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
if (ADOQuery1.Fields[12].AsString = '5') and (ADOQuery1.Fields[0].AsString = BiaoHao) then begin
ADOQuery1.Edit;
ADOQuery1.Fields[1].AsString := Gu;
ADOQuery1.Fields[5].AsString := NowTime;
ADOQuery1.UpdateRecord;
end;
end;
//********寰宇系统*********
ADOQuery1.Next;
end; //wwhile
end;
end;
end;
//6.8上装当日6.9上装当月6.13 上装指定日
function TForm1.receive6869613(datastring: string): integer;
var
WriteNumber, numRead: LongWord;
Write_Buffer, buf, AddBuffer, AddBuffer1: array[0..10] of byte;
TemWrite_Buffer, str2, Resultstr, TempString1: string;
lw: LongWord;
i, ff, ReceiveLen, j, page, ii, Temp_len, DelayNum, DelaySecond, LengthWrite_Buf: integer;
str1: Word; //byte;
NowDate: tdatetime;
wait: Boolean;
cs: Tcomstat;
delaytrue: integer;
Bytesread: LongWord;
dwEvtMask, dwError: Dword;
inbuff, Temp_buff, RealTemp_buff: array[0..255 * 780] of byte;
TempString, S: string;
FromF, ToF, TempFromF: file of byte;
label jieshu, RDdata;
begin
try
Write_Buffer[0] := byte($EA); //$ 表示16进制
Write_Buffer[1] := byte($EA);
DBGrid2.SelectedRows.Count; //当前行号
S := DBGrid2.Fields[0].AsString;
Write_Buffer[3] := HexToDec(copy(S, 3, 4)); // byte($FA);
Write_Buffer[4] := HexToDec(copy(S, 1, 2)); //byte($01);
Write_Buffer[5] := byte($00);
Write_Buffer[6] := byte($00);
Write_Buffer[7] := byte($00);
Write_Buffer[10] := byte($DD);
if datastring = '8E' then Write_Buffer[2] := byte($0E); //上装当月6.9
if datastring = '8F' then Write_Buffer[2] := byte($0F); //上装当日6.9
if datastring = '8B' then Write_Buffer[2] := byte($0B); //上装当日6.9
delaytrue := DelayTest();
if delaytrue = 1 then goto jieshu;
if delaytrue = 2 then {//中断退出} begin
comcolorRed();
RadioGroup1.Enabled := true;
delete.Enabled := true;
Button1.Enabled := true;
ComboBox1.Enabled := true;
exit;
end;
if (hcom = 0) then goto jieshu;
ClearCommError(hcom, dwError, @cs);
ReceiveLen := 0;
j := 1;
page := 0;
DelaySecond := 1;
for i := 1 to 255 * 80 do begin
inbuff[i] := byte($00);
Temp_buff[i] := byte($00);
end;
Resultstr := '';
while DelaySecond < 64 * 5 do begin
while cs.cbInQue > 0 do begin
if ReadFile(hcom, inbuff, cs.cbInQue, Bytesread, nil) then begin
{将inbuff加入Temp_buff}
for Temp_len := 1 to cs.cbInQue do
Temp_buff[ReceiveLen + Temp_len] := inbuff[Temp_len - 1];
ReceiveLen := ReceiveLen + cs.cbInQue;
//receive.Lines.Add(inttostr(ReceiveLen) + '---' +inttostr(cs.cbInQue));
{将inbuff加入Temp_buff}
if (page = 0) and (ReceiveLen = 11) then begin
if (trim(Format('%2x', [Temp_buff[3]])) = 'CB') or (trim(Format('%2x', [Temp_buff[3]])) = 'CE') or (trim(Format('%2x', [Temp_buff[3]])) = 'CF') then begin
receive.Lines.Add('操作失败!');
goto jieshu;
end;
end;
if (page = 0) and (ReceiveLen = 266) then begin//第一块
receive.Lines.Add('共有块数----'+Format('%2d', [Temp_buff[262]]));
end;
if ReceiveLen - (page + 1) * 266 >= 0 then begin
TempString := '';
for ii := 1 to 266 do {// SizeOf(TComStat)} begin
TempString1 := trim(Format('%2x', [Temp_buff[ii + page * 266]]));
TempString := TempString + rightstr(('00' + TempString1), 2);
end;
page := page + 1;
receive.Lines.Add('接受数据' + IntToStr(page) + '块');
//receive.Lines.Add(TempString);
Resultstr := Resultstr + copy(TempString, 13, 510);
//处理数据
if copy(TempString, 5, 2) = datastring then begin
DBGrid1.DataSource.DataSet.DisableControls;
if datastring <> '8E' then {//不解释当月数据} begin
DBGrid1.Visible := false;
DBGrid3.Visible := true;
Parse6869TempString(Resultstr);
DBGrid1.Visible := true;
DBGrid3.Visible := false;
end;
// receive.Lines.Add(Resultstr);
goto RDdata;
end;
//加入处理数据 并再次发送
Write_Buffer[2] := byte($00);
str1 := 0; //必须先赋值
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -