Created
March 3, 2020 18:51
-
-
Save ASolchen/fb80b3c6f0e8e57810832b6349c102a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Controller, Tag, EthernetIP } = require("ethernet-ip"); | |
const { SINT, DINT } = EthernetIP.CIP.DataTypes.Types; | |
const PLC = new Controller(); | |
const readString = (name)=>{ | |
return new Promise(async (res, rej)=>{ | |
try{ | |
str = '' | |
for(let i=0;i<82;i++){ | |
let t = new Tag(name + `.DATA[${i}]`, null, SINT); | |
await PLC.readTag(t); | |
if(!t.value){ | |
break; | |
} | |
str += String.fromCharCode(t.value) | |
} | |
res(str); | |
} | |
catch(err){ | |
rej(err) | |
} | |
}) | |
} | |
const writeString = (name, str)=>{ | |
return new Promise(async (res, rej)=>{ | |
try{ | |
let len = Math.min(str.length, 81); | |
let buff = Buffer.from(str); | |
for(let i=0;i<82;i++){ | |
let t = new Tag(name + `.DATA[${i}]`, null, SINT); | |
if(buff.length >= i - 1){ | |
t.value = buff[i] | |
} else { | |
t.value = 0; //NULL Term | |
} | |
await PLC.writeTag(t); | |
} | |
let l = new Tag(name + `.LEN`, null, DINT); | |
l.value = len; | |
await PLC.writeTag(l); | |
res(len); | |
} | |
catch(err){ | |
rej(err) | |
} | |
}) | |
} | |
const DI_len = 32; | |
const DO_len = 16; | |
PLC.connect("10.10.90.175", 0).then(async () => { | |
try{ | |
for(let chan=0; chan<DI_len; chan++){ | |
await writeString(`Area01_DI_03_ChanDesc[${chan}]`, `New Chan ${chan}`) | |
let str = await readString(`Area01_DI_03_ChanDesc[${chan}]`) | |
console.log(str) | |
} | |
} | |
catch(err){ | |
console.log(err) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment