-- Initialization
function OnInit()
end
-- Uninitialization
function OnClose()
end
-- Processing
function OnBeforeReading()
local send={};
local sendmask={"byte","byte","byte","byte","byte","byte ","byte","byte","byte","byte","byte","byte"};
local dest={};
local destmask={};
local bb={};
local n=0;
local weight;
local b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12;
repeat
err,dest,len=server.SendAndReceiveDataByMask(0,0,s endmask,send,destmask,12);
--NoErr,err,dest,len=pcall(server.SendAndReceiveData ByMask,0,0,sendmask,send,destmask,12);
n=n+1;
until (err>=0 and len == 12 and dest[1]==02) or n>=3
if err>=0 then
b1=dest[3]-48;
b2=dest[4]-48;
b3=dest[5]-48;
b4=dest[6]-48;
b5=dest[7]-48;
b6=dest[8]-48;
if b1>9 or b1<0 then
b1=0;
end
if b2>9 or b2<0 then
b2=0;
end
if b3>9 or b3<0 then
b3=0;
end
if b4>9 or b4<0 then
b4=0;
end
if b5>9 or b5<0 then
b5=0;
end
if b6>9 or b6<0 then
b6=0;
end
bb[1]=b1*100000;
bb[2]=b2*10000;
bb[3]=b3*1000;
bb[4]=b4*100;
bb[5]=b5*10;
bb[6]=b6*1;
weight=bb[1]+bb[2]+bb[3]+bb[4]+bb[5]+bb[6];
end
--контроль зависания
if weight==0 then
--timeSt=time.TimeStampNow( );
--w={};
--w=time.UnpackTime(timeSt );
--weight=w[6];
--weight=time.TimeNow( );
time1=time.TimeNow( );
w={};
w=time.UnpackTime(time1 );
weight=w[6];
end
--weight=b1*100000+b2*10000+b3*1000+b4*100+b5*10+b6* 1;
res=server.WriteTagByRelativeName("Weight",weight, OPC_QUALITY_GOOD);
--NoErr,res=pcall(server.WriteTagByRelativeName,"Wei ght",weight,OPC_QUALITY_GOOD);
if err<0 or dest[1]~=02 then
weight=0;
res=server.WriteTagByRelativeName("Weight",weight, OPC_QUALITY_BAD);
--NoErr,res=pcall(server.WriteTagByRelativeName,"Wei ght",weight,OPC_QUALITY_BAD);
end
end
-- Processing
function OnAfterReading()
end
-- Device failure
function OnError()
res=server.WriteTagByRelativeName("Weight",weight, OPC_QUALITY_BAD);
end