plcpeople / nodepccc

Library for node.js to communicate with some Allen-Bradley programmable controllers (PLCs)
MIT License
81 stars 24 forks source link

[Question!] Show 16 bits integer in an array (micrologix 1400) #11

Closed Jotan closed 7 years ago

Jotan commented 7 years ago

Hello, first, thanks for this, I'm working on a HMI with NodeJS and this is perfect, my question is when i'm showing the data of the PLC, in the taglookup function I have this:

switch (tag) { case 'TEST': return 'N3:0/0,8'; } this show the right values, but, when i change the function: switch (tag) { case 'TEST': return 'N3:0/0,16'; } this show bad and undefined values, also i tried with this: switch (tag) { case 'TEST': return 'N3:0/8,8'; } but the same result bad and undefined values. is there a way to call the 16 bits of an integer ?

Thank You

Jotan commented 7 years ago

I solved already... Thanks

joaomello-zz commented 7 years ago

How did you solved, @Jotan?

Jotan commented 7 years ago

Hi @joaomello, in the module of nodepccc (nodepccc.js) I modified some lines of codes...here are (with the number of every line): Real

  1. if ((((arrayIndex + theItem.bitOffset + 1) % 8) == 0) || (arrayIndex == theItem.arrayLength - 1)){ Modified to:
  2. if ((((arrayIndex + theItem.bitOffset + 1) % 16) == 0) || (arrayIndex == theItem.arrayLength - 1)){

As you can see I change the 8 value to 16 thats take 16 bits instead of 8, then you must change all the case X value format, I'll show you on the next lines: Real 1614.theItem.value.push(((theItem.byteBuffer.readUInt8(thePointer) >> (bitShiftAmount)) & 1) ? true : false); Modified to: 1614.theItem.value.push(((theItem.byteBuffer.readUInt16LE(thePointer) >> (bitShiftAmount)) & 1) ? true : false);

Real

  1. if ((((arrayIndex + theItem.bitOffset + 1) % 8) == 0) || (arrayIndex == theItem.arrayLength - 1)){

Modified to:

  1. if ((((arrayIndex + theItem.bitOffset + 1) % 16) == 0) || (arrayIndex == theItem.arrayLength - 1)){

Real

  1. theItem.writeBuffer.writeUInt8(theByte, thePointer);

Modified to:

  1. theItem.writeBuffer.writeUInt16LE(theByte, thePointer);

Real

  1. if ((((arrayIndex + theItem.bitOffset + 1) % 8) == 0) || (arrayIndex == theItem.arrayLength - 1)){

Modified to: 1818.if ((((arrayIndex + theItem.bitOffset + 1) % 16) == 0) || (arrayIndex == theItem.arrayLength - 1)){

Real

  1. theItem.writeBuffer.writeUInt8(((theItem.writeValue) ? 1 : 0), thePointer); // checked ===true but this caused problems if you write 1

Modified to:

  1. theItem.writeBuffer.writeUInt16LE(((theItem.writeValue) ? 1 : 0), thePointer); // checked ===true but this caused problems if you write 1

With that changes you can get and write data like "N16:0/0,32"

I hope it helps you.

plcpeople commented 7 years ago

Thank you for this I have integrated it into the package now. Finally. PR's are always welcome.

Note there was a bug affecting writing non-zero values to bit arrays > length 16 that is fixed in 0.1.5 as well.

Jotan commented 7 years ago

Thanks to you for this Library :)