Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
471 stars 175 forks source link

Can the two's complement be decoding incorrectly? #62

Closed angiehjort closed 7 years ago

angiehjort commented 7 years ago

Hi! i'm getting data from a modbus device and negative temperatures show up in node.js application as 65413-something. I can fix it by manually post-processing the inputs like this:

  var decode = function(x){
    if(x>1000) x = x - 65536; //substract Math.pow(2,16)
    return x;
  }

but i wonder if node-modbus already supposed to interpret this correectly on reading the registers. the "two's complement" and so on...

Here is my device i'm reading from http://www.comet-america.com/products/temperature-humidity-pressure-transmitters-and-regulators/web-sensor-p8641-with-poe-four-channels-remote-thermometer-hygrometer/reg-P8641#downloadd

in the instruction manual here page 22 there is a register map (I'm interested in int*10 values):

image

stefanpoeter commented 7 years ago

If I understand this correct, you are wondering how you can get the two-complement interpretation of your temperature? Since @psorowka made a pull request, you are able to read the result from the modbus request out of a buffer. From that you can just readInt16 your temperature. Have a look here for more help.

angiehjort commented 7 years ago

thanks @BauchBeinePoe! it helped (of course it did)