Giter VIP home page Giter VIP logo

Comments (3)

jisotalo avatar jisotalo commented on September 7, 2024 1

If you really need to read struct with BITs it's possible but needs a little work. I tested the following and it works fine!

PLC side

TYPE ST_Bits:
STRUCT
  Bit_0 : BIT;
  Bit_1 : BIT;
  Bit_2 : BIT;
  Bit_3 : BIT;
  Bit_4 : BIT;
  Bit_5 : BIT;
  Bit_6 : BIT;
  Bit_7 : BIT;
END_STRUCT
END_TYPE
//GVL_BitTest
{attribute 'qualified_only'}
VAR_GLOBAL
  Bits : ST_Bits;
END_VAR

Node.js side

const ads = require('ads-client');
const client = new ads.Client({
  targetAmsNetId: 'localhost',
  targetAdsPort: 851
});

client.connect()
  .then(async res => {   
    console.log(`Connected to the ${res.targetAmsNetId}`);
    console.log(`Router assigned us AmsNetId ${res.localAmsNetId} and port ${res.localAdsPort}`);

    //First getting the symbol info for this variable
    const info = await client.getSymbolInfo('GVL_BitTest.Bits');

    //Using symbol info to subscribe to raw data
    const sub = await client.subscribeRaw(info.indexGroup, info.indexOffset, info.size, (data, sub) => {
      //Converting Buffer to byte
      const value = data.value.readUint8();
      
      const converted = {
        bit_0: !!(value & (0x01 << 0)),
        bit_1: !!(value & (0x01 << 1)),
        bit_2: !!(value & (0x01 << 2)),
        bit_3: !!(value & (0x01 << 3)),
        bit_4: !!(value & (0x01 << 4)),
        bit_5: !!(value & (0x01 << 5)),
        bit_6: !!(value & (0x01 << 6)),
        bit_7: !!(value & (0x01 << 7)),
      }
      console.log(converted);
      /* Should print something like:
      {
        bit_0: true,
        bit_1: false,
        bit_2: true,
        bit_3: false,
        bit_4: true,
        bit_5: false,
        bit_6: true,
        bit_7: false
      }
      */
    });
  })
  .catch(err => {
    console.log('Something failed:', err);
  })

Thanks again and have a nice christmas!

from ads-client.

jisotalo avatar jisotalo commented on September 7, 2024

Hi!

Actually the BIT data type is not available/working at the moment. I thought that I had it in README but I didn't.. Basically it is in the code however handled like a BOOL and it would require some extra effort to be handled correctly.

So I would suggest to use BOOL instead if possible, or just a single WORD and then get the bits yourself. Personally I never use BITs with TwinCAT as the support is not that good.

Edit: I will keep this issue open as this certainly should work some day if it's technically doable.

from ads-client.

isaac-nls avatar isaac-nls commented on September 7, 2024

Hi jisotalo,

Thanks very much for the quick reply. Good to know I wasn't going crazy! We initially were using BITs instead of BOOLs to reduce the size of the overall packets over the network, but it's not a big deal for us to change over to using BOOLs instead. I'll keep it in mind for future development not to use BITs.

Once again, I very much appreciate the time and effort you've put into this library.

Thanks!
Isaac

from ads-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.