EtherNet/IP Read
Connects to an Allen-Bradley style PLC over EtherNet/IP, reads four floats from
file F103, parses them with the binary reader (with unit conversion), and
tracks data freshness.
let plc = {lastUpdated:0};
while(run()){
// eip.disconnect();
if(eip.connect("192.168.168.1") === 0){ // 0 = no error
let res = eip.read("F103",1,4*4); // File: F103, Offset: 1, Bytes: 4*8 (4x 4 byte floats)
// Files can be prefixed with F,O,I,S,B,T,R,N depending on type
if(res.status === 0){ // 0 = no error
res.data.seek(0); // establish our binary reader cursor to parse the input data
plc = {
flow: res.data.read("F32ABCD"), // Parse the bytes into our object
pressure: res.data.read() * 0.145038,// kPA to PSI,
pumpSpeed: res.data.read(),no read format specified. Use the last defined read format
temp: res.data.read(),
lastUpdated:now()
};
print( string (plc) ); // Print our result data
} else {
print("PLC Read Failed");
}
} else {
print("PLC Connection Failed");
let updatedAgo = now() - plc.lastUpdated;
print("Last Updated ",updatedAgo, " S Ago");
}
eip.disconnect(); // End the connection
waitMS(1000);
}