⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 此程序完全按照645协议编写的485抄表程序 波特率1200
💻 PAS
📖 第 1 页 / 共 3 页
字号:
 label6.Caption:='';
 label6.Refresh ;
 label6.Caption:='正 在 通 讯 . . . . !';
 memo1.Text:='';
 memo1.Refresh ;
 if trim(combobox1.Text)='COM1' then
  fhbz:=Open('com1','600')
 ELSE
 fhbz:=Open('com2','600');
 rr:=trim(edit1.Text);
 stra:='BBBB'+rr;
 ss:=hex_str1(stra);
 WriteCom(ss);
 sleep(1000);
 j:=ReadCom(val);
 Close();
 shu:='';
 count:=0;
 str:='';
 count:=0;
 while count<6 do
 begin
   lsls:=inttostr(ord(val[count]));
   sj:=f_dectohex(lsls);
   str:=str+sj;
   count:=count+1;
 end;
 if (copy(str,3,2)='4F') AND (copy(str,5,2)='4B') AND (copy(str,7,2)='0D') THEN
 begin
  edit5.Text:=str;
  SHOWMESSAGE('通讯成功');
  label6.Caption:=' 通 讯 成功 ....!';
  EXIT;
  END
 else
  BEGIN
  edit5.Text:=str;
  SHOWMESSAGE('通讯失败');
  label6.Caption:=' 通 讯 失 败 ....!';
  EXIT;
  END;
end ;

procedure TForm1.Button10Click(Sender: TObject);
var
  cc:Tcommconfig;
  Temp:string;
  rr,s,ttdd,CK,com,str,s0,xyw,stra,ss,shu,head,lsls,sj:string;
  fhbz,sjlong,i,xy,j,count,long,len:integer;
  fp:textfile;
  val:pchar;
begin
  com:=trim(edit1.Text);
  Temp:=com;
  hcomm:=createfile(pchar(temp),generic_read or generic_write,0,nil,Open_existing,0,0);
  if (hcomm=invalid_handle_value) then
  begin
   //result:=-1;
   exit;
   showmessage('err0r');
  end;
  //DWORD fDtrControl:2;       // DTR flow control type
  //DWORD
  cc.dcb.Flags:=0;
  getcommstate(hcomm,cc.dcb);
  cc.dcb.baudrate:=cbr_1200;
  cc.dcb.bytesize:=8;
  //cc.dcb.
  //cc.dcb.fRtsControl:=1;
  cc.dcb.parity:=evenparity;//noparity;
  cc.dcb.stopbits:=onestopbit;
  setcommstate(hcomm,cc.dcb);
  //setcommMask(hcomm,EV_DSR);
  //ev_rxchar);
  //
  //EV_DSR
 if not setcommstate(hcomm,cc.dcb)  then
 begin
     showmessage('打开串口失败');
     exit;
 end;
 rr:=trim(edit1.Text);
 while (i<length(str)) do
 begin
  s0:=h_dec(copy(str,i,2));
  xy:=xy+strtoint(s0);
  i:=i+2;
 end;
 xyw:=f_dectohex(inttostr(xy));
 if length(xyw)>2 then
 xyw:=copy(xyw,2,2);
 stra:=stra+xyw+'0D';
 ss:=hex_str1(stra);
 showmessage(ss);
 WriteCom(ss);
 //sleep(1000);
 getcommstate(hcomm,cc.dcb);
 cc.dcb.baudrate:=cbr_1200;
 cc.dcb.bytesize:=8;
 cc.dcb.Flags:=1;
 //cc.dcb.fRtsControl:=1;
 cc.dcb.parity:=evenparity;//noparity;
 cc.dcb.stopbits:=onestopbit;
 //setcommMask(hcomm,EV_CTS);
 setcommstate(hcomm,cc.dcb);
 j:=ReadCom(val);
 shu:='';
 count:=0;
 str:='';
 head:=copy(trim(edit1.Text),1,2);
 long:=strtoint(trim(edit6.Text));
 i:=1 ;
 xy:=0;
 while i<7 do
 begin
   lsls:=inttostr(ord(val[count]));
   sj:=f_dectohex(lsls);
   xy:=xy+strtoint(h_dec(sj));
   str:=str+sj;
 end;
 count:=i;
 showmessage(str);
 if (copy(str,1,2)=head) and (copy(str,3,2)='4F') AND (copy(str,5,2)='4B') AND (copy(str,7,2)=head) and ((copy(str,9,2)='AA') OR (copy(str,9,2)='A5')) THEN
 begin
  while count<=long do
  begin
   lsls:=inttostr(ord(val[count]));
   sj:=f_dectohex(lsls);
   if length(sj)<2 then
   sj:='0'+sj;
   str:=str+sj;
   xy:=xy+strtoint(h_dec(sj));
   alldata:=alldata+sj;
   count:=count+1;
  end;
  if long<10 then
  len:=long
  else
  len:=long*2;
  xyw:=f_dectohex(inttostr(xy));
  if (sj='0D') and (count=10+len) and (copy(str,length(str)-4,2)=xyw) then
  begin
   memo1.Text :=alldata;
   edit5.Text:=str;
  end
  else
  BEGIN
  edit5.Text:=str;
  SHOWMESSAGE('通讯失败');
  label6.Caption:=' 通 讯 失 败 ....!';
  EXIT;
  END;
end
else
BEGIN
  SHOWMESSAGE('通讯失败');
  label6.Caption:=' 通 讯 失 败 ....!';
  EXIT;
END;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
 //setcommMask(hcomm,EV_CTS);
end;

procedure TForm1.Button12Click(Sender: TObject);
var
  cc:Tcommconfig;
  Temp:string;
  rr,s,ttdd,CK,com,str,s0,xyw,stra,ss,shu,head,lsls,sj:string;
  fhbz,sjlong,i,xy,j,count,long,len:integer;
  fp:textfile;
  val:pchar;
begin
  label6.Caption:='';
  label6.Refresh;
  edit5.Text:='';
  edit5.Refresh;
  com:=trim(combobox1.text);
  Temp:=com;
  hcomm:=createfile(pchar(temp),generic_read or generic_write,0,nil,Open_existing,0,0);
  if (hcomm=invalid_handle_value) then
  begin
   exit;
   showmessage('err0r');
  end;

  getcommstate(hcomm,cc.dcb);
  cc.dcb.baudrate:=cbr_1200;
  cc.dcb.bytesize:=8;
  cc.dcb.parity:=evenparity;//noparity;
  cc.dcb.stopbits:=onestopbit;
  cc.dcb.Flags:=SETRTS;
  //EscapeCommFunction(hcomm, SETDTR);
  //EscapeCommFunction(hcomm, CLRRTS);
  //EscapeCommFunction(hcomm, SETDTR);
  if not setcommstate(hcomm,cc.dcb)  then
  begin
     showmessage('打开串口失败');
     exit;
  end;
 i:=1;
 xy:=0;
 str:=trim(edit1.Text);
 while (i<length(str)) do
 begin
  s0:=h_dec(copy(str,i,2));
  xy:=xy+strtoint(s0);
  i:=i+2;
 end;
 xyw:=f_dectohex(inttostr(xy));
 if length(xyw)>2 then
 xyw:=copy(xyw,2,2);
 stra:=str+xyw+'0D';
 edit2.Text:=stra;
 ss:=hex_str1(stra);
 edit3.Text:=ss;
 WriteCom(ss);

 getcommstate(hcomm,cc.dcb);
 cc.dcb.baudrate:=cbr_1200;
 cc.dcb.bytesize:=8;
 cc.dcb.parity:=evenparity;
 cc.dcb.stopbits:=onestopbit;
 //cc.dcb.Flags:=0;
 //EscapeCommFunction(hcomm,CLRDTR);
 //EscapeCommFunction(hcomm,SETRTS);
 //EV_CTS	The CTS (clear-to-send) signal changed state.
 //EV_DSR
 //setcommmask(hcomm,ev_RXCHAR);
 //setcommmask(hcomm,EV_RXFLAG);
 //setcommmask(hcomm,EV_DSR);
 setcommstate(hcomm,cc.dcb);
 setcommmask(hcomm,EV_CTS);
 

 sleep(2500);
 j:=ReadCom(val);
 Close();
 shu:=str_hex(val,long*2+18);
 edit5.Text:=shu;
 {shu:='';
 count:=0;
 str:='';
 Close();
 head:=copy(trim(edit1.Text),1,2);
 long:=strtoint(trim(edit6.Text));
 i:=1 ;
 xy:=0;
 while i<long*2+8 do
 begin
   lsls:=inttostr(ord(val[count]));
   sj:=f_dectohex(lsls);
   xy:=xy+strtoint(h_dec(sj));
   str:=str+sj;
   count:=count+1;
   i:=i+1;
 end;
 if (copy(str,1,2)=head) and (copy(str,3,2)='4F') AND (copy(str,5,2)='4B')  THEN
 begin
  edit5.Text:=str;
  label6.Caption:=' 通 讯 成功 ....!';
 end
 else
 begin
  edit5.Text:=str;
  label6.Caption:=' 通 讯 失 败 ....!';
  EXIT;
 end;
    }
end;

procedure TForm1.Button13Click(Sender: TObject);
var
  cc:Tcommconfig;
  Temp:string;
  rr,s,ttdd,CK,com,str,s0,xyw,stra,ss,shu,head,lsls,sj:string;
  fhbz,sjlong,i,xy,j,count,long,len:integer;
  fp:textfile;
  vala:pchar;
begin
  memo1.Text:='';
  memo1.Refresh ;
  com:=trim(combobox1.text);
  com:='com2';
  Temp:=com;
  hcomm:=createfile(pchar(temp),generic_read or generic_write,0,nil,Open_existing,0,0);
  if (hcomm=invalid_handle_value) then
  begin
   showmessage('err0r');
   exit;
  end;
  //DWORD fDtrControl:2;       // DTR flow control type
  //DWORD
  //cc.dcb.Flags:=0;
  //cc.dcb.fOutxCtsFlow:=1;
  getcommstate(hcomm,cc.dcb);
  cc.dcb.baudrate:=cbr_1200;
  cc.dcb.bytesize:=8;
  //cc.dcb.
  //cc.dcb.fRtsControl:=1;
  cc.dcb.parity:=evenparity;//noparity;
  cc.dcb.stopbits:=onestopbit;
  //setcommstate(hcomm,cc.dcb);
  //setcommMask(hcomm,EV_DSR);
  //ev_rxchar);
  //EV_DSR
 if not setcommstate(hcomm,cc.dcb)  then
 begin
   showmessage('打开串口失败');
   exit;
 end;
 //fhbz:=Open('com2');
 i:=1;
 xy:=0;
 str:=trim(edit1.Text);
 while (i<length(str)) do
 begin
  s0:=h_dec(copy(str,i,2));
  xy:=xy+strtoint(s0);
  i:=i+2;
 end;
 xyw:=f_dectohex(inttostr(xy));
 if length(xyw)>2 then
 xyw:=copy(xyw,2,2);
 stra:=str+xyw+'0D';
 ss:=hex_str1(stra);
 edit3.Text:=ss;
 WriteCom(ss);
 getcommstate(hcomm,cc.dcb);
 cc.dcb.baudrate:=cbr_1200;
 cc.dcb.bytesize:=8;
 //cc.dcb.Flags:=1;
 //cc.dcb.fRtsControl:=1;
 //CC.dcb.fDtrControl:=1;
 cc.dcb.parity:=evenparity;//noparity;
 cc.dcb.stopbits:=onestopbit;
 //setcommMask(hcomm,EV_CTS);
 //cc.dcb.fOutxCtsFlow:=1;      // CTS output flow control
 // cc.dcb.fOutxDsrFlow:=1;      // DSR output flow control
 //cc.dcb.fDtrControl:=2;       // DTR flow control type
 //cc.dcb.fDsrSensitivity:=1;   // DSR sensitivity
 //cc.dcb.fOutX:= 1;            // XON/XOFF out flow control
 //cc.dcb.fInX:= 1;             // XON/XOFF in flow control
 //cc.dcb.fRtsControl:=2;       // RTS flow control
 //  WORD XonLim;               // transmit XON threshold
 //  WORD XoffLim;              // transmit XOFF threshold
 //  char XonChar;              // Tx and Rx XON character
 //  char XoffChar;             // Tx and Rx XOFF character
 //  char ErrorChar;            // error replacement character

 //  char EofChar;              // end of input character
 //  char EvtChar;              // received event character
 //  WORD wReserved1;           // reserved; do not use
 //EV_CTS	The CTS (clear-to-send) signal changed state.
 //EV_DSR
 setcommstate(hcomm,cc.dcb);
 //setcommmask(hcomm,ev_rxchar);
 setcommmask(hcomm,EV_DSR);
 sleep(500);
 j:=ReadCom(vala);
 str:='';
 head:=copy(trim(edit1.Text),1,2);
 long:=strtoint(trim(edit6.Text))*2+8;
 i:=1 ;
 xy:=0;
 str:='';
 xy:=0;
 count:=0;
 while i<long do
 begin
   lsls:=inttostr(ord(vala[count]));
   sj:=f_dectohex(lsls);
   xy:=xy+strtoint(h_dec(sj));
   str:=str+sj;
   i:=i+1;
   count:=count+1;
 end;
 memo1.Text:=str;
 if (copy(str,3,2)='4F') AND (copy(str,5,2)='4B') THEN
 begin
  edit5.Text:=str;
  label6.Caption:=' 通 讯 成功 ....!';
  Close();
 end
  else
  BEGIN
  edit5.Text:=str;
  label6.Caption:=' 通 讯 失 败 ....!';
  Close();
  END;
end;

procedure TForm1.Button14Click(Sender: TObject);
var rr,s,ttdd:string;
    fhbz,sjlong:integer;
    fp:textfile;
begin
  aa:='';
  bb:='';
  ccc:='';
  memo1.Text:='';
  memo1.Refresh ;
  fhbz:=Open('com1','600');
  edit1.Text:='68999999999999680806101010101004' ;
  rr:=trim(edit1.Text);
  ttdd:='BDBDFE00000000780D';
  fhbz:=comoutfh(ttdd,9);
  fhbz:=comout(rr,strtoint(edit6.text));
  edit2.text:=aa;
  edit3.text:=bb;
  IF CCc='9999' THEN
  BEGIN
   SHOWMESSAGE('通讯失败');
   EXIT;
  END;
  memo1.text:=ccc;
  sjlong:=strtoint(copy(memo1.Text,17,2))*2;

end;

procedure TForm1.Button18Click(Sender: TObject);
var ck,str,sj,xyw,sendhead,lsls,s0,stra,ss,ls,btl:string;
    fh,count,xy,i,jslong,jylong,sjlong,delay2,delay3,k:integer;
    file1:textfile;
begin
   memo1.Text:='';
   memo1.Refresh;
   jylong:=0;
   ss:='';
   ck:=trimleft(trimright(combobox1.text));
   btl:=trimleft(trimright(combobox2.text));
   if trimleft(trimright(maskedit1.Text))='' then
   begin
   showmessage('发延时数据错误');
   exit;
   end;
   if trimleft(trimright(maskedit2.Text))='' then
   begin
   showmessage('收延时数据错误');
   exit;
   end;

   delay2:=strtoint(trimleft(trimright(maskedit1.Text)));
   delay3:=strtoint(trimleft(trimright(maskedit2.Text)));

   if btl='300' then
    fh:=Open300(ck);
   if btl='600' then
    fh:=Open600(ck);
   if btl='1200' then
    fh:=Open1200(ck);
   if btl='2400' then
    fh:=Open2400(ck);
   if btl='4800' then
    fh:=Open4800(ck);
   if btl='9600' then
    fh:=Open9600(ck);
   if btl='' then
    begin
     showmessage('请选择波特率');
     exit;
    end;

     if fh<>0  then
       begin
         showmessage('串口错误!');
   if btl='300' then
    fh:=Close300();
   if btl='600' then
    fh:=Close600();
   if btl='1200' then
    fh:=Close1200();
   if btl='2400' then
    fh:=Close2400();
   if btl='4800' then
    fh:=Close4800();
   if btl='9600' then
    fh:=Close9600();
         exit;
       end
     else
   begin
   sendhead:=trim(edit1.text);
   if btl='300' then
    fh:=ComOut300(sendhead,length(sendhead),delay2,delay3);
   if btl='600' then
    fh:=ComOut600(sendhead,length(sendhead),delay2,delay3);
   if btl='1200' then
    fh:=ComOut1200(sendhead,length(sendhead),delay2,delay3);
   if btl='2400' then
    fh:=ComOut2400(sendhead,length(sendhead),delay2,delay3);
   if btl='4800' then
    fh:=ComOut4800(sendhead,length(sendhead),delay2,delay3);
   if btl='9600' then
    fh:=ComOut9600(sendhead,length(sendhead),delay2,delay3);
   if fh<>0 then
   begin
   showmessage('通讯失败');
   end
   else
   begin
   AssignFile(file1, 'revc.dat');
   Reset(file1);
   Readln(file1,str);
   CloseFile(file1);
   showmessage('通讯成功!');
   memo1.text:=str;
   edit6.Text:=inttostr(length(str));
   end;
   if btl='300' then
    fh:=Close300();
   if btl='600' then
    fh:=Close600();
   if btl='1200' then
    fh:=Close1200();
   if btl='2400' then
    fh:=Close2400();
   if btl='4800' then
    fh:=Close4800();
   if btl='9600' then
    fh:=Close9600();
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -