CRC16 for MODBUS

Place code snippets and demo code here
Post Reply
User avatar
Electroguard
Posts: 179
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 40 times
Been thanked: 102 times

CRC16 for MODBUS

Post by Electroguard »

(from ACarvalo)
Just received a PZEM004T (v3) and had to adapt my older monitor for the new MODBUS interface (this interface uses CRC16 checksum).
This is a routine I've made to calculate the checksum based on the specs guide at [External Link Removed for Guests].

Code: [Local Link Removed for Guests]

' Calc CRC16 for MODBUS
' Parameters:
'    byte     Array of bytes (1 for each occurence) (23 bytes for PZEM004T)
'    size     Size of array
'    poly     Polynomial to use (&hA001 for PZEM004T)
'    crc16    Result (use 2 bytes of this value)
'                   First byte with hex$(crc16 mod 256)
'                   Second byte with hex$(crc16 >> 8)
sub MODBUS_crc16(byte(), size, poly, crc16)
local f, g
crc16 = &hFFFF
for f = 1 to size
  crc16 = crc16 xor byte(f-1)
  for g=1 to 8
    if ((crc16 and &h01) <> 0) then
      crc16 = crc16 >> 1
      crc16 = crc16 xor poly
    else
      crc16 = crc16 >> 1
    end if
  next g
next f
end sub
The PZEM004T is a low cost AC 220v energy monitor with reasonable error margin, made by Ningbo Peacefair. Is has two versions: one with an internal shunt (10A) and another with an external coil (100A). I use the 100A external coil.
The new version (v3), reports voltage (80v~260v), current (0~100A), frequency (45~60Hz), power factor (0~1 only on v3), power (0~22Kw) and active energy (0~9999Kwh).
By using MODBUS, the new version (v3) should allow more than device to be connected to ESP8266 on the same serial line.

Here you can see specs: [External Link Removed for Guests]

I've been using it (v1) for three years without any relevant problems (some inacurate values returned but easily eliminated and very rare).
I'm gathering data that I store in a database and analyze it later.
The first goal was to monitor my energy use in order help me define my energy supplier and energy rate to use (I review rates every year and change accordingly). I estimate that it helped me to reduce my energy bill in 15%, at least.
This year I used the collected data to profile my energy use in daylight (solar photovoltaic panel usable time) and decide (ROI based) the optimal size of my solar installation (still in plan).

A note for the fact that PZEM004T doesn't report current direction, so, if you have a grid-tied solar installation, readings will be inaccurate.
A MOD that allows the detection of current direction has been released, for v1 devices, by Bruno Horta [External Link Removed for Guests] nevertheless, in my opinion, the values of energy will still be inaccurate.
Khz
Posts: 1
Joined: Wed Jun 30, 2021 9:05 pm

Re: CRC16 for MODBUS

Post by Khz »

Danke, hat mir sehr geholfen.
gerdF
Posts: 9
Joined: Sun Mar 28, 2021 10:33 am
Has thanked: 5 times

Re: CRC16 for MODBUS

Post by gerdF »

Hello Electroguard,
do you still have the code for the PZEM-004T V1.0?
Would be nice if you could post it here or send it as a PM.
If not, what is the difference in the software between V1.0 and V3.0?
Certainly not just CRC16 checksum
I can only find information about version 3.0

Thank you, Gerd
User avatar
Electroguard
Posts: 179
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 40 times
Been thanked: 102 times

Re: CRC16 for MODBUS

Post by Electroguard »

Hi Gerd, I never had any code, I simply re-posted the original post by ACarvalo from the old forum on his behalf to the new forum (same as I did for others) so it wouldn't be lost in the change-over ... your best bet might be to try to PM or gmail ACarvalo.
gerdF
Posts: 9
Joined: Sun Mar 28, 2021 10:33 am
Has thanked: 5 times

Re: CRC16 for MODBUS

Post by gerdF »

Hi Electroguard,
thanks for the quick reply and sorry for the misunderstanding.
In the old forum, Mario L. wrote a post on August 10, 2019, 10:43:29 PM and offered a file ESP01-PZEM004T.bas
and some more infomation for download.
I'll keep looking.

Thank you, Gerd
User avatar
Electroguard
Posts: 179
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 40 times
Been thanked: 102 times

Re: CRC16 for MODBUS

Post by Electroguard »

Good luck Gerd. In the last 2 years I have moved, had 2 major 2TB hdd failures, had every Win10 system crash and become unusable, migrated completely to linux, and barely clung on to any sanity let alone any previous data... so everything I had 2 years ago is ancient history - I even have to rely on the Annex website for my own scripts which I've previously posted, else start over.
@t vW
Posts: 8
Joined: Tue Feb 23, 2021 8:00 pm
Location: Hoorn Holland (Netherlands)
Has thanked: 30 times
Been thanked: 2 times

Re: CRC16 for MODBUS

Post by @t vW »

Hi Robin, I know how it feels, and I feel sorry for you. I too lost several years of software because of catastophic events.
But life goes on, and still we are here (on this planet) looking into the future. Greetings, At.
Post Reply