News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

How to read INT64 data?

Started by shantanu@india, Jul 22, 2022, 11:42 AM

Previous topic - Next topic

shantanu@india

My Schneider energy meter is giving 8 bytes for energy in signed INT64 format.
How to process?... break it up into two DWords, the higher number signed and the lower number unsigned?
Any pointers please?
Regards
Shantanu

John Lawton


shantanu@india

#2
I polled the EM6400 with Modscan and got the following hex values corresponding to Wh
00 00 00 00 01 30 1E 10
This corresponds to decimal 19930640.
Strictly speaking I don't need negative values since the power flow direction in my case would always be positive... towards load.
Regards
Shantanu

shantanu@india

INT64 has a staggering range...9223372036854 Mega Watt hour of total accumulated energy measurement!!
Beyond a poor 8-bit PIC.
I need daily differential readings....these are managable.
But how to subtract one 64-bit number from another 64-bit number?
Regards
Shantanu

John Lawton

Do subtraction by first principles, i.e. long subtraction?
Maybe split into a 64 member byte array to do this.
Example here: https://www.mathematics-monster.com/lessons/long_subtraction.html

shantanu@india

Maybe I'm making a basic mistake of not thinking in practical terms.
If I consider the 4 lower bytes of the INT64 as a Dword(unsigned...since I'm assuming unidirectional energy flow) , then the contribution of those LS bytes in the total energy value is (2^32-1) Watt hour = 4294967 kWh = 4.2 GWh !!!
I do not need to monitor applications where the daily energy consumption is in gigawatt range.
I can safely ignore the four upper bytes & work with the four lower bytes as a Dword.The daily energy consumption pattern would affect only the four lower bytes & any changes in the four upper bytes would be very very long term.
Am I making a mistake in my presumptions? John??
In any case a workable 64-bit subtraction routine would be nice.
Regards
Shantanu

John Lawton

Is this meter owned by your utility company, in which case the reading will be cumulative, or if it is your meter then maybe not. What model is it, something like this: Meter ?


trastikata

#7
Quote from: shantanu@india on Jul 22, 2022, 02:45 PMAm I making a mistake in my presumptions?

Given the upper 4 bytes are the same, just put the lower 4 bytes in Dword variables and use regular math operation to subtract.

shantanu@india

Quote from: John Lawton on Jul 22, 2022, 03:44 PMIs this meter owned by your utility company, in which case the reading will be cumulative, or if it is your meter then maybe not. What model is it, something like this: Meter ?


No this is a generic solution for a IOT device connected to energy meters. A 18F is working in conjunction with a ESP8266.The 18F reads the energy data and passes it to the ESP via the SPI interface.
Regards
Shantanu

shantanu@india

Quote from: trastikata on Jul 22, 2022, 04:13 PM
Quote from: shantanu@india on Jul 22, 2022, 02:45 PMAm I making a mistake in my presumptions?

Given the upper 4 bytes are the same, just put the lower 4 bytes in Dword variables and use regular math operation to subtract.
Yes Trastikata you're right... I was thinking along those lines previously. But consider a "once in a bluemoon situation "... today the energy value has overflowed from the fourth byte to the fifth byte. So while caculating tomorrow's consumption only by considering byte 1-4 would give wrong result.
Regards
Shantanu

RGV250

Hi,
I have a couple of questions, does the PIC do anything else apart from read the data, if not why not just use the ESP.
I have had a little play with an ESP32 and from what I can gather that has 64bit maths, not sure if the 8266 has but it might be easier to do it all in the one device.

Regards,
Bob

trastikata

#11
Quote from: shantanu@india on Jul 22, 2022, 04:43 PMYes Trastikata you're right... I was thinking along those lines previously. But consider a "once in a bluemoon situation "... today the energy value has overflowed from the fourth byte to the fifth byte. So while caculating tomorrow's consumption only by considering byte 1-4 would give wrong result.


Then check for both minuend and subtrahend size. Again, assuming no more than one power of 32 overflow, using only the lower 32 bits:

I. Check from the lower 4 bytes if minuend is larger than subtrahend - means no overflow

If minuend > subtrahend Then difference = minuend - subtrahend 

II. With overflow, reasonable to assume no more than 2^32 watts in one day  ;), then from the lower 4 bytes minuend will be smaller than subtrahend

If minuend < subtrahend Then difference = 4,294,967,296 - (subtrahend - minuend)

III. When equal equal - just to cover all possibilities.

If minuend = subtrahend Then difference = 0

Example 1 (second term is the new meter data):

0x46ABEA3B545CD658 = 5,092,421,344,211,818,072
0x46ABEA3C575CD658 = 5,092,421,344,262,149,720
5,092,421,344,262,149,720 - 5,092,421,344,211,818,072 = 50,331,648

|
|
V

0x545CD658 = 1,415,370,328
0x575CD658 = 1,465,701,976
1,465,701,976 - 1,415,370,328 = 50,331,648   

50,331,648 = 50,331,648   

-----------------------------------
Example 2 (second term is the new meter data):

0x46ABEA3BFEDCBA98 = 5,092,421,347,072,326,296
0x46ABEA3C12345678 = 5,092,421,347,396,834,936
5,092,421,347,396,834,936 - 5,092,421,347,072,326,296 = 324,508,640

|
|
V

0x46ABEA3B = 1,185,671,739
0x46ABEA3C = 1,185,671,740
1,185,671,740 - 1,185,671,739 = 1

0xFEDCBA98 = 4,275,878,552
0x12345678 = 305,419,896
4,294,967,296 - (4,275,878,552 - 305,419,896) = 324,508,640

324,508,640 = 324,508,640

By the way, as a contingency you may add some check-ups based on common logic that no more than 2^32 watts in one day may be consumed and no reverse current is allowed:

- If the higher 4 bytes are equal and minuend < subtrahend - then error
- If the higher 4 bytes are not equal:
-- If difference in higher 4 bytes is more than 1 - then error 
-- If difference in higher 4 bytes is 1, but minuend > subtrahend - then error

shantanu@india

Wow this is really a great analysis Trastikata.
Thanks.
Regards
Shantanu

shantanu@india

Quote from: RGV250 on Jul 22, 2022, 06:05 PMHi,
I have a couple of questions, does the PIC do anything else apart from read the data, if not why not just use the ESP.
I have had a little play with an ESP32 and from what I can gather that has 64bit maths, not sure if the 8266 has but it might be easier to do it all in the one device.

Regards,
Bob
Yes Bob ESP8266 can indeed do a lot of things. But I'm still not confident enough about using it as a lone warhorse. It is being used for standard digital input/output and Wi-Fi communication.
Regards
Shantanu

shantanu@india

Bob your post gave my
 thoughts a new direction. Why not transfer the 8 bytes to ESP and carry out the maths there?
Just verified that ESP8266 Micropython supports 64-bit maths by executing the following instructions by REPL:
MyInt=9223372036854775808
print(hex(MyInt))
  0x8000000000000000
MyInt -=1
print(hex(MyInt))
  0x7fffffffffffffff
Thanks for acting as a catalyst!!
Regards
Shantanu

John Drew

The PIC24 etc do 64 bit maths in Proton. In which case a simple substraction X-Y
But you may be limited to an existing 18F. I haven't read every post.
John