question

Paul F Prinsloo avatar image
Paul F Prinsloo asked

Modbus TCP - corect DC values

Hello I am new to the modbus interface and have been using MQTT with my Victron setup, so far I have managed to get a connection and reading the values from the Modbus but nothing I can't figure is how "negative" value is handled if that is the correct question.

I am pulling the battery Watts value from the Slave adress: 100 and the Register value of 842 but the value I receive is 65242 instead of -298 watts. I am assuming that there should be a conversion of sorts but I can't seem to find any info on the topic.
Any help?
Regards

Paul

Modbus TCP
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

3 Answers
Ingo avatar image
Ingo answered ·

65242 is roughly the 2's compliment of -298.

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Paul F Prinsloo avatar image Paul F Prinsloo commented ·

Thank you for the Info. I had a look and very intresting.

0 Likes 0 ·
Paul F Prinsloo avatar image
Paul F Prinsloo answered ·

Apologies, but I am not sure how to use this to get the correct value. Is there a formula that I could use?

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Ingo avatar image Ingo commented ·

You need to do the following:


lets take the example of decimal (-44) to form 2's complements as follows:
Step 1: Begin with the decimal value -44
The value will be converted into binary as 00101100
Step 2: Formed the one's complement as follows:
11010011
Step 3: Add 1 to the one's complement
11010011 (One's complement)
+ 1 (Add 1)
-----------
11010100 (Two's complement)


This for 8-bit, you will need to adapt it for 16-bit. Check Google, there are lots of examples and online calculators.

0 Likes 0 ·
Paul F Prinsloo avatar image
Paul F Prinsloo answered ·

The other comments here are very valuable in understanding the basics of how data is sent.
Just as a side note for someone, like me , that is very new to most of this. If you are running node red and pulling the data from the modbus and you want to get the correct value from the payload
and I assume that there will be a much better sulutiuon.

Use this :

if (msg.payload > 50000){ // there is no way that my value will be bigger than this so it is safe to assume that it is a negative value...

msg.payload = msg.payload[0] - 65535

return msg;

}else{

msg.payload = msg.payload[0]

return msg;

}


2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Related Resources