📄 omron_plc_fins.pas
字号:
{ **************************************************************************** }
{ }
{ Host linker protocol ,Fins command---OMRON SYSMAC CP1H }
{ PC To PLC command frame: }
{ @ 00 FA 0 00 00 00 00 0102 82 006400 0001 'command' 'FCS' *CR}
{ note: @ UN HC RT ICF DA2 SA2 SID WriteMem EMADDR byt fINScomm FCS TERM}
{ 0101(reademem) }
{ PLC to PC response frame: }
{ @ 00 FA 00 40 00 00 00 0102 'responsecode' 'FCS' *CR }
{ note: @ UN HC RT ICF DA2 SA2 SID fINS responsecode FCS TERM }
{ response normal:0000 others:error }
{ }
{ **************************************************************************** }
{ 1 读监控内容共20个字(共40个字节) 2 时间设定共9个字(18个字节) }
{ 3 距离设定共28个字(共56个字节) }
{4 速度设定(14共28个字节) 5 风机设定共5个字9(10个字节) }
{6 低温时间 7 高温加热时间 8 强化时间 9 冷却时间 10 风机1 11 风机2 12 风机3 }
{13 设置间隔时间 14 超温报警 15 超温清除 }
{21 读监控设定值(间隔、低温、高温、强化、冷却) 22 读工艺参数 23 读距离参数 }
{24 读速度参数 25读风机参数 }
{ designed by zhouhuajun }
{ 2008.12 }
unit omron_plc_fins;
interface
uses windows,classes,spcomm,sysutils,dialogs,dsVariable,Str_Utils,cport;
type Tomron_Fins=class(Tobject)
private
fcomport:Tcomport;
lastsendsignal:integer;
readorwrite:string;
function isresponse(buf:Tsbuf):boolean;
function make_response_fcs(buf:Tsbuf;ilength:integer):string;
function calc_response_fcs(buf:Tsbuf;buflength:integer):string;
function maketime(s:string):string;
function dealCompact(s:string):string;
function warnmessage(s:string;pos:integer):boolean;
function OcttoBin(i: integer): string;
function warnprocess(s:string;i:integer;pos:integer):string;
function calc_response_fcs1(scommand:string):string;
public
function MakeSbufSignal(signal: integer): Tsbuf;
procedure comport_receivedata(Sender: TObject; count: integer);
function initcomm(scom:string):boolean;
procedure displaydata(sdata:string);
procedure closecomm;
function CommandsendSignal(Signal: integer):boolean;
constructor create();
destructor Destroy();override;
end;
implementation
uses test;
//uses yk_main, yk_StrUtils;
function Tomron_Fins.initcomm(scom:string):boolean;
begin
result:=true;
fcomport.Port:=scom;
try
// fcomm.StopComm;
// fcomm.StartComm;
fcomport.Connected:=false;
fcomport.Connected:=true;
except
// on ECommsError do
// begin
result:=false;
// end;
end;
end;
function Tomron_Fins.CommandsendSignal(Signal: integer):boolean;
var sinput,scommand:string;
sbuf:Tsbuf;
i,k:integer;
ireadlength:integer;
sfcs:string;
begin
self.lastsendsignal:=signal;
sBuf :=MakeSbufSignal(Signal);
scommand:='';
case signal of
21: ireadlength:=51;
22: ireadlength:=67;
23: ireadlength:=139;
24: ireadlength:=83;
25: ireadlength:=47;
3: ireadlength:=138;
4: ireadlength:=90;
5: ireadlength:=54;
1: ireadlength:=107;
6,7,8,9,10,11,12,13: ireadlength:=27;
end;
try
self.fcomport.ClearBuffer(true,true);
self.fcomport.WriteStr(ssendcommand);
{ if bstart then
sleep(50)
else }
sleep(120);
self.fcomport.ReadStr(sinput,ireadlength);
// sinput:='@00FA00000000000101000007D00000000000000000000000000000000000000000045';
if (self.readorwrite='read') then
begin
if (copy(sinput,20,4)='0000') then //response code <>'0000' 信号传输错误.'0101'代表读数据
begin
// self.CommandsendSignal(self.lastsendsignal); //读数据时,轮循,所以错误不需要重发
sfcs:=self.calc_response_fcs1(copy(sinput,1,length(sinput)-4));
if copy(sinput,length(sinput)-3,2)=sfcs then
displaydata(sinput)
else
frmglass.memlog.Lines.Add(datetimetostr(now)+'传输数据错误!');
end
else
frmglass.memlog.Lines.Add(datetimetostr(now)+' 读数据错误!');
end;
if (copy(sinput,20,4)<>'0000') and (self.readorwrite='write') then //response code <>'0000' 信号传输错误.'0101'代表读数据
begin
sendcount:=sendcount+1;
if sendcount<5 then
begin
self.CommandsendSignal(self.lastsendsignal); //写数据时,要判断,不成功重发,但取多5次;
frmglass.memlog.Lines.Add(datetimetostr(now)+' '+'写数据错误! 错误内容:'+swriteerrormessage[Signal]);
exit;
end;
end;
except
end;
end;
procedure Tomron_Fins.displaydata(sdata:string);
var swarning,smonitor:string;
begin
if self.lastsendsignal=21 then
begin
para_interval:=inttostr(strtoint('$'+copy(sdata,24,4)) div 10);
para_dw:=inttostr(strtoint('$'+copy(sdata,28,4)) div 10);
para_gw:=inttostr(strtoint('$'+copy(sdata,32,4)) div 10);
para_qh:=inttostr(strtoint('$'+copy(sdata,40,4)) div 10);
para_lq:=inttostr(strtoint('$'+copy(sdata,44,4)) div 10);
frmglass.spjgsj.Value:=strtoint(para_interval);
frmglass.splowheat.Value:=strtoint(para_dw);
frmglass.sphighheat.Value:=strtoint(para_gw);
frmglass.spqh.Value:=strtoint(para_qh);
frmglass.splq.Value:=strtoint(para_lq);
frmglass.update_artist;
end;
if self.lastsendsignal=25 then
begin
para_fj1dj:=inttostr(strtoint('$'+copy(sdata,24,4)) div 6);
para_fj1:=inttostr(strtoint('$'+copy(sdata,28,4)) div 6);
para_fj2dj:=inttostr(strtoint('$'+copy(sdata,32,4)) div 6);
para_fj2:=inttostr(strtoint('$'+copy(sdata,36,4)) div 6);
para_fj3:=inttostr(strtoint('$'+copy(sdata,40,4)) div 6);
frmglass.spgyfj1dj.Value:=strtoint(para_fj1dj);
frmglass.spfj1qh.Value:=strtoint(para_fj1);
frmglass.spgyfj2dj.Value:=strtoint(para_fj2dj);
frmglass.spfj2qh.Value:=strtoint(para_fj2);
frmglass.spfj3lq.Value:=strtoint(para_fj3);
frmglass.lbgyfj1dj.Caption:=para_fj1dj;
frmglass.lbgyfj2dj.Caption:=para_fj2dj;
frmglass.update_artist;
end;
if self.lastsendsignal=1 then
begin
sj_interval:=inttostr(strtoint('$'+copy(sdata,24,4)) div 10);
sj_dw:=inttostr(strtoint('$'+copy(sdata,28,4)) div 10);
sj_gw:=inttostr(strtoint('$'+copy(sdata,32,4)) div 10);
sj_qh:=inttostr(strtoint('$'+copy(sdata,36,4)) div 10);
sj_lq:=inttostr(strtoint('$'+copy(sdata,40,4)) div 10);
sj_fj1:=inttostr(strtoint('$'+copy(sdata,44,4)) div 6);
sj_fj2:=inttostr(strtoint('$'+copy(sdata,48,4)) div 6);
sj_fj3:=inttostr(strtoint('$'+copy(sdata,52,4)) div 6);
swarning:=copy(sdata,56,4);
if swarning<>'0000' then
begin
warnmessage(swarning,1);
end;
swarning:=copy(sdata,60,4);
if swarning<>'0000' then
begin
warnmessage(swarning,2);
end;
smonitor:=self.OcttoBin(strtoint('$'+copy(sdata,64,8)));
frmglass.edcd.Text:=inttostr(strtoint('$'+copy(sdata,76,4)+copy(sdata,72,4)) div frmglass.spjpscale.Value);
frmglass.eddw.Text:=inttostr(strtoint('$'+copy(sdata,84,4)+copy(sdata,80,4)) div frmglass.spdwscale.Value);
frmglass.edgw.Text:=inttostr(strtoint('$'+copy(sdata,92,4)+copy(sdata,88,4)) div frmglass.spgwscale.Value);
frmglass.edqh.Text:=inttostr(strtoint('$'+copy(sdata,100,4)+copy(sdata,96,4)) div frmglass.spqhscale.Value);
frmglass.update_artist;
end;
if self.lastsendsignal=22 then //读工艺时间参数
begin
frmglass.lbgyjgsj.Caption:=inttostr(strtoint('$'+copy(sdata,24,4)) div 10);
frmglass.lbgydwjl.Caption:=inttostr(strtoint('$'+copy(sdata,28,4)) div 10);
frmglass.lbgygwjl.Caption:=inttostr(strtoint('$'+copy(sdata,32,4)) div 10);
frmglass.lbgybdsj.Caption:=inttostr(strtoint('$'+copy(sdata,36,4)) div 10);
frmglass.lbgyqhsj.Caption:=inttostr(strtoint('$'+copy(sdata,40,4)) div 10);
frmglass.lbgylqsj.Caption:=inttostr(strtoint('$'+copy(sdata,44,4)) div 10);
frmglass.lbgyfj1tq.Caption:=inttostr(strtoint('$'+copy(sdata,48,4)) div 10);
frmglass.lbgyfm1tq.Caption:=inttostr(strtoint('$'+copy(sdata,52,4)) div 10);
frmglass.lbgyfj2tq.Caption:=inttostr(strtoint('$'+copy(sdata,56,4)) div 10);
frmglass.lbgyfm2tq.Caption:=inttostr(strtoint('$'+copy(sdata,60,4)) div 10);
end;
if self.lastsendsignal=23 then //读距离参数
begin
frmglass.lbjljpbj.Caption:=inttostr(strtoint('$'+copy(sdata,28,4)+copy(sdata,24,4)) div frmglass.spjpscale.Value);
frmglass.lbjldwjpjs.Caption:=inttostr(strtoint('$'+copy(sdata,36,4)+copy(sdata,32,4)) div frmglass.spdwscale.Value);
frmglass.lbjldwlc.Caption:=inttostr(strtoint('$'+copy(sdata,44,4)+copy(sdata,40,4)) div frmglass.spdwscale.Value);
frmglass.lbjldwaq.Caption:=inttostr(strtoint('$'+copy(sdata,52,4)+copy(sdata,48,4)) div frmglass.spdwscale.Value);
frmglass.lbjldwcp.Caption:=inttostr(strtoint('$'+copy(sdata,60,4)+copy(sdata,56,4)) div frmglass.spdwscale.Value);
frmglass.lbjldwcpjs.Caption:=inttostr(strtoint('$'+copy(sdata,68,4)+copy(sdata,64,4)) div frmglass.spdwscale.Value);
frmglass.lbjlgwlc.Caption:=inttostr(strtoint('$'+copy(sdata,76,4)+copy(sdata,72,4)) div frmglass.spgwscale.Value);
frmglass.lbjlgwaq.Caption:=inttostr(strtoint('$'+copy(sdata,84,4)+copy(sdata,80,4)) div frmglass.spgwscale.Value);
frmglass.lbjlgwcp.Caption:=inttostr(strtoint('$'+copy(sdata,92,4)+copy(sdata,88,4)) div frmglass.spgwscale.Value);
frmglass.lbjlgwjs.Caption:=inttostr(strtoint('$'+copy(sdata,100,4)+copy(sdata,96,4)) div frmglass.spgwscale.Value);
frmglass.lbjlqhlc.Caption:=inttostr(strtoint('$'+copy(sdata,108,4)+copy(sdata,104,4)) div frmglass.spqhscale.Value);
frmglass.lbjlqhaq.Caption:=inttostr(strtoint('$'+copy(sdata,116,4)+copy(sdata,112,4)) div frmglass.spqhscale.Value);
frmglass.lbjlqhcp.Caption:=inttostr(strtoint('$'+copy(sdata,124,4)+copy(sdata,120,4)) div frmglass.spqhscale.Value);
frmglass.lbjlccsd.Caption:=inttostr(strtoint('$'+copy(sdata,132,4)+copy(sdata,128,4)) div frmglass.spdwscale.Value);
end;
if self.lastsendsignal=24 then //读速度参数
begin
frmglass.lbjpbjsd.Caption:=inttostr(strtoint('$'+copy(sdata,24,4)) div frmglass.spsdjp.Value);
frmglass.lbdwdjsd.Caption:=inttostr(strtoint('$'+copy(sdata,28,4)) div frmglass.spsddw.Value);
frmglass.lbdwjpsd.Caption:=inttostr(strtoint('$'+copy(sdata,32,4)) div frmglass.spsddw.Value);
frmglass.lbdwjpjs.Caption:=inttostr(strtoint('$'+copy(sdata,36,4)) div frmglass.spsddw.Value);
frmglass.lbdwbdsd.Caption:=inttostr(strtoint('$'+copy(sdata,40,4)) div frmglass.spsddw.Value);
frmglass.lbdwcpsd.Caption:=inttostr(strtoint('$'+copy(sdata,44,4)) div frmglass.spsddw.Value);
frmglass.lbdwcpjs.Caption:=inttostr(strtoint('$'+copy(sdata,48,4)) div frmglass.spsddw.Value);
frmglass.lbgwdjsd.Caption:=inttostr(strtoint('$'+copy(sdata,52,4)) div frmglass.spsdgw.Value);
frmglass.lbgwbdsd.Caption:=inttostr(strtoint('$'+copy(sdata,56,4)) div frmglass.spsdgw.Value);
frmglass.lbgwcpsd.Caption:=inttostr(strtoint('$'+copy(sdata,60,4)) div frmglass.spsdqh.Value);
frmglass.lbgwcpjs.Caption:=inttostr(strtoint('$'+copy(sdata,64,4)) div frmglass.spsdqh.Value);
frmglass.lbqhbdsd.Caption:=inttostr(strtoint('$'+copy(sdata,68,4)) div frmglass.spsdqh.Value);
frmglass.lbqhlqbd.Caption:=inttostr(strtoint('$'+copy(sdata,72,4)) div frmglass.spsdqh.Value);
frmglass.lbqhcpsd.Caption:=inttostr(strtoint('$'+copy(sdata,76,4)) div frmglass.spsdqh.Value);
end;
{ if frmglass.pnconsole.Caption<>'' then
frmglass.pnconsole.Caption:=''
else }
// frmglass.pnconsole.Caption:=sdata;
end;
function Tomron_Fins.warnmessage(s:string;pos:integer):boolean;
var i,ilen:integer;
swarnmessage:string;
sbinstring:string;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -