?? 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 + -