Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert response from ReadHoldingRegisters to Decimal #71

Closed
alancsdev opened this issue Mar 9, 2022 · 2 comments
Closed

Convert response from ReadHoldingRegisters to Decimal #71

alancsdev opened this issue Mar 9, 2022 · 2 comments
Labels

Comments

@alancsdev
Copy link

alancsdev commented Mar 9, 2022

I'm getting this response <Buffer 00 28>, how can i convert to dec?

Thanks

var modbus = require("modbus-stream");

modbus.tcp.connect(502, "10.10.10.8", (err, connection) => {
  if (err) {
    console.log("Falha!");
    throw err;
  }
  console.log("Conectado");

  connection.readHoldingRegisters({ address: 1, quantity: 1 }, (err, res) => {
    if (err) throw err;

    console.log(res.response.data[0]); // response


    console.log(new Uint8Array(res.response.data[0]));
  });
});
@dresende
Copy link
Member

dresende commented Mar 9, 2022

Modbus does not enforce types, so you need to know the type of data. Usually, for unsigned integers of 2 bytes, modbus implementers use uint16be. You can do this:

console.log(res.response.data[0].readUInt16BE(0));

@alancsdev
Copy link
Author

Modbus does not enforce types, so you need to know the type of data. Usually, for unsigned integers of 2 bytes, modbus implementers use uint16be. You can do this:

console.log(res.response.data[0].readUInt16BE(0));

It worked perfectly, thank you very much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants