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

📄 pc与三菱plc通讯中读写各寄存器的delphi源码.txt

📁 PC与三菱PLC通讯中读写各寄存器的DELPHI源码
💻 TXT
字号:
function getmaddr(strmaddrno:string):string;
var lngmlen:integer;
    strmno:string;
begin
  lngmlen:=length(strmaddrno);
  strmno:=copy(strmaddrno,2,lngmlen-1);
  getmaddr:=inttohex((strtoint(strmno) div 8)+256,4);
end;

function getdaddr(strdaddrno:string):string;
var
  lngDlen:integer;
  strdno:string;
begin
  lngdlen:=length(strdaddrno);
  strdno:=copy(strdaddrno,2,lngdlen-1);
  getdaddr:=inttoHex((strtoint(strdno)*2+4096),4);
end;

function checksum(strcmd:char;straddr:string;strbytes:string;strdata:string):string;
var
    lngsum:integer;
    i,j:integer;
    a,b,c:integer;
    lngmod1,lngmod2:string;
begin
  lngsum:=0;
  lngsum:=lngsum+ord(strcmd);
  for i:=1 to length(straddr) do
      lngsum:=lngsum+ord(straddr[i]);
  if strbytes<>'' then
      lngsum:=lngsum+ord(strbytes[1])+ord(strbytes[2]);
  if length(strdata)>0 then
     for j:=1 to length(strdata) do
      lngsum:=lngsum+ord(strData[j]);
  lngsum:=lngsum+ord(etx);
  a:=lngsum mod 16;
  if a<10 then lngmod1:=inttostr(a);
  if a=10 then lngmod1:='A';
  if a=11 then lngmod1:='B';
  if a=12 then lngmod1:='C';
  if a=13 then lngmod1:='D';
  if a=14 then lngmod1:='E';
  if a=15 then lngmod1:='F';
  b:=lngsum div 16;
  c:=b mod 16;
  if c<10 then lngmod2:=inttostr(c);
  if c=10 then lngmod2:='A';
  if c=11 then lngmod2:='B';
  if c=12 then lngmod2:='C';
  if c=13 then lngmod2:='D';
  if c=14 then lngmod2:='E';
  if c=15 then lngmod2:='F';
  checksum:=lngmod2+lngmod1;
end;

function tprocess.writePLCsv():boolean;   //向PLC写入设定值
var
  rst,address:string;
  st,strsum,strsum1,strsum2:string;
  buf:array [0..4096] of Char;
  i,rlen:integer;
begin
  address:=inttohex(200*2+4096,4);  //起始地址200 ---208
  strSum:=CheckSum(cmdwrite, address, '12', data);    //和校验,字节数18个
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdwrite+address+inttohex(18,2)+data+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#6 then result:=true else result:=false;
  end else
    result:=false;
  except
  end;
end;

function tprocess.writePLCsw():boolean;   //向PLC写入开关值
var
  rst,address,data:string;
  st,strsum,strsum1,strsum2:string;
  buf:array [0..4096] of Char;
  i,rlen:integer;
begin
  address:=inttohex(swaddress*2+4096,4);          //起始地址D300 ---319
  data:=swdata;
  strSum:=CheckSum(cmdwrite, address, '02', data);    //和校验,2个字节
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdwrite+address+inttohex(2,2)+data+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#6 then result:=true else result:=false;
  end else
    result:=false;
  except
  end;
end;

function tprocess.writePLCswstop():boolean;   //向PLC写入开关值
var
  rst,address,data:string;
  st,strsum,strsum1,strsum2:string;
  buf:array [0..4096] of Char;
  i,rlen:integer;
begin
  address:=inttohex(300*2+4096,4);          //起始地址D300 ---319
  data:='0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
  i:=length(data);
  strSum:=CheckSum(cmdwrite, address, '28', data);    //和校验,2个字节
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdwrite+address+inttohex(40,2)+data+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(200);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#6 then result:=true else result:=false;
  end else
    result:=false;
  except
  end;
end;


function tprocess.writepid():boolean;   //向PLC写入设定值
var
  rst,address:string;
  st,strsum,strsum1,strsum2:string;
  buf:array [0..4096] of Char;
  i,rlen:integer;
begin
  address:=inttohex(350*2+4096,4);  //起始地址d350 ---361
  strSum:=CheckSum(cmdwrite, address, '18', piddata);    //和校验,字节数24个
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdwrite+address+inttohex(24,2)+piddata+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#6 then result:=true else result:=false;
  end else
    result:=false;
  except
  end;
end;

function tprocess.readPLCpv():boolean;    //读PLC实际温度
var
  strsum,strsum1,strsum2,strpvaddr,strpvaddr1,strpvaddr2,strpvaddr3,strpvaddr4:string;
  st,rst,st1:string;
  buf:array[0..4096] of Char;
  rlen,i:integer;
  strpvdno:string;
begin
  strpvdno:='D100';                                // 地址D100---D118
  strpvaddr:=getdaddr(strpvdno);
  strsum:=checksum(cmdread,strpvaddr,'26','');     //读取38个字节的数据
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);

  st:=chr(stx)+cmdread+strpvaddr+inttohex(38,2)+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#2 then begin result:=true;
     for i:=1 to 19 do values1[i]:=strtoint('$'+copy(rst,i*4,2))*256+strtoint('$'+copy(rst,i*4-2,2));
     end else result:=false;
  end else result:=false;
  except
  end;
end;

function tprocess.readPLCsv():boolean;     //读PLC设定温度
var
  strsum,strsum1,strsum2,strpvaddr,strpvaddr1,strpvaddr2,strpvaddr3,strpvaddr4:string;
  st,rst,st1:string;
  buf:array[0..4096] of Char;
  rlen,i:integer;
  strpvdno:string;
begin
  strpvdno:='D200';                                // 地址D200-D208
  strpvaddr:=getdaddr(strpvdno);
  strsum:=checksum(cmdread,strpvaddr,'12','');     //读取18个字节的数据
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);

  st:=chr(stx)+cmdread+strpvaddr+inttohex(18,2)+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#2 then begin result:=true;
     for i:=1 to 9 do values2[i]:=strtoint('$'+copy(rst,i*4,2))*256+strtoint('$'+copy(rst,i*4-2,2));
     end else result:=false;
  end else result:=false;
  except
  end;
end;

function tprocess.readPLCout():boolean;     //读PLC输出开关量
var
  strsum,strsum1,strsum2,strpvaddr,strpvaddr1,strpvaddr2,strpvaddr3,strpvaddr4:string;
  st,rst,st1:string;
  buf:array[0..4096] of Char;
  rlen,i:integer;
  strpvdno:string;
begin
  strpvdno:='D300';                                // 地址D300-D319
  strpvaddr:=getdaddr(strpvdno);
  strsum:=checksum(cmdread,strpvaddr,'28','');     //读取40个字节的数据
  strSum1:=copy(strsum,1,1);
  strSum2:=copy(strsum,2,1);

  st:=chr(stx)+cmdread+strpvaddr+inttohex(40,2)+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#2 then begin result:=true;
     for i:=1 to 20 do values4[i]:=strtoint('$'+copy(rst,i*4,2))*256+strtoint('$'+copy(rst,i*4-2,2));
     end else result:=false;
  end else result:=false;
  except
  end;
end;

function tprocess.readplcx():boolean;  //读PLC输入值
var
  strsum:string;
  strsum1:string;
  strsum2:string;
  buf:array[0..4096] of Char;
  st,rst:string;
  rlen,i:integer;
  groupaddrx:string;
begin
  groupaddrx:='0080';
  strsum:=checksum(cmdread,groupaddrx,'02','');
  strsum1:=copy(strsum,1,1);
  strsum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdread+groupaddrx+'02'+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#2 then begin result:=true;
     for i:=1 to 2 do values4[i]:=strtoint('$'+copy(rst,i*2,2));
     end else result:=false;
  end else result:=false;
  except
  end;
end;

function tprocess.readplcy():boolean;  //读PLC输出值
var
  strsum:string;
  strsum1:string;
  strsum2:string;
  buf:array[0..4096] of Char;
  st,rst:string;
  rlen,i:integer;
  groupaddrx:string;
begin
  groupaddrx:='00A0';

  strsum:=checksum(cmdread,groupaddrx,'04','');
  strsum1:=copy(strsum,1,1);
  strsum2:=copy(strsum,2,1);
  st:=chr(stx)+cmdread+groupaddrx+'04'+chr(etx)+strsum1+strsum2;
  sio_write(port1,pchar(st),length(st));
  sleep(100);
  rlen:=sio_read(port1,@buf,255);
  setLength(Rst,rlen);
  for i:=0 to rlen-1 do  Rst[i+1]:=Buf[i];
  try
  if rst<>'' then  begin
     if rst[1]=#2 then begin result:=true;
     for i:=1 to 3 do values3[i]:=strtoint('$'+copy(rst,i*2,2));
     end else result:=false;
  end else result:=false;
  except
  end;
end;

⌨️ 快捷键说明

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