reaper7/SDM_Energy_Meter

Getting SDM_ERR_WRONG_BYTES error

re3lex opened this issue · 11 comments

Hello

I have ESP32 that reads SDM220 via Modbus using this library.
It worked long time without issues. Last time I update firmware of my ESP32 around December 2020 (though not sure what version of SDM library was used that time).

Today I updated SDM library version to last one and loaded new firmware to ESP32. As result I have started getting SDM_ERR_WRONG_BYTES error as result of values read.

As per comment next to erro code definition this error happens because bytes b0,b1 or b2 wrong, but I'm not big expert in Modbus protocol and not sure what that mean and how to fix it.

Can you please shed some light on what can cause this error and how to fix it?


Upd 1. Just added some info
Cable length between RS485 converter and SDM220 is about 30cm.
Checked another issue (#53) about error 2, but looks like that is not my case.


Upd 2 Found, that issue is happening because b2 is not matching with SDM_REPLY_BYTE_COUNT.

try somehow to diagnose what response you are getting,

maybe these two issues will help you:
#55
#51

Remember also, that the library has evolved over time, the names that define registers have changed.

hi @reaper7
Thanks for reply.

Checked those two issues, but, unfortunately, cannot find anything specific for my one.

Checked register addresses: they looks to be correct.

I was able to get response for some registers (digits before colon is index in uint8_t response array, value is following after colon):

Data for register address: 0
{0:1, 1:4, 2:0, 3:0, 4:0, 5:2, 6:113, 7:203, 8:1, }
Data for register address: 6
{0:1, 1:4, 2:0, 3:6, 4:0, 5:2, 6:145, 7:202, 8:1, }
Data for register address: C
{0:1, 1:4, 2:0, 3:12, 4:0, 5:2, 6:177, 7:200, 8:1, }
Data for register address: 1E
{0:1, 1:4, 2:0, 3:30, 4:0, 5:2, 6:17, 7:205, 8:1, }
Data for register address: 46
{0:1, 1:4, 2:0, 3:70, 4:0, 5:2, 6:144, 7:30, 8:1, }
Data for register address: 12
{0:1, 1:4, 2:0, 3:18, 4:0, 5:2, 6:209, 7:206, 8:1, }
Data for register address: 18
{0:1, 1:4, 2:0, 3:24, 4:0, 5:2, 6:241, 7:204, 8:1, }
Data for register address: 24
{0:1, 1:4, 2:0, 3:36, 4:0, 5:2, 6:49, 7:192, 8:1, }
Data for register address: 48
{0:1, 1:4, 2:0, 3:72, 4:0, 5:2, 6:241, 7:221, 8:1, }
Data for register address: 4A
{0:1, 1:4, 2:0, 3:74, 4:0, 5:2, 6:80, 7:29, 8:1, }
Data for register address: 4C
{0:1, 1:4, 2:0, 3:76, 4:0, 5:2, 6:176, 7:28, 8:1, }
Data for register address: 4E
{0:1, 1:4, 2:0, 3:78, 4:0, 5:2, 6:17, 7:220, 8:1, }
Data for register address: 156
{0:1, 1:4, 2:1, 3:86, 4:0, 5:2, 6:144, 7:39, 8:1, }
Data for register address: 158
{0:1, 1:4, 2:1, 3:88, 4:0, 5:2, 6:241, 7:228, 8:1, }
Data for register address: 56
{0:1, 1:4, 2:0, 3:86, 4:0, 5:2, 6:145, 7:219, 8:1, }
Data for register address: 58
{0:1, 1:4, 2:0, 3:88, 4:0, 5:2, 6:240, 7:24, 8:1, }

strange...
are you sure you have everything well set in SDM_Config_User.h

I understand you are using the hardware serial option ? so is it uncommented?
same for uart speed and other uart config?

yes, #define USE_HARDWARESERIAL is uncommented (otherwise build fails :) )

I see read activity on my SDM220, but result is the same: error 2.

I have connected ESP32 via usb2ttl converter to my PC and sent test value to emulate SDM response using hardware Serial1.
Test value is processed correctly.

Can you please suggest what can be an issue then?

I have 2 spare rs485-to-ttl converters. Tried both, but the result is the same.
Also I have 2 SDM220, tested on both, but no luck: b2 is zero instead of 0x04

Continue analyzing.

This is content of sdmarr array before sdmSer.write() operation:

[0:1, 1:4, 2:0, 3:0, 4:0, 5:2, 6:113, 7:203, 8:0 ]

This is what is returned by sdmSer.read() operation:

(0:1, 1:4, 2:0, 3:0, 4:0, 5:2, 6:113, 7:203, 8:1 }

It appears SDM220 is just echoing.

Finally managed to get it to work.
I used NO_PIN for DE_RE pin in SDM class instance setup (DE_RE physical pin sat on GND).
Setting DE_RE pin to IO pin and defining it in class instance setup fixed the problem.

@reaper7 thanks for your help!

last question: any idea why DE_RE pin on the GND has stopped to work correctly?

Personally I don't use a DE_RE pin, default value NOT_A_PIN is used.
My converter doesn't need this signal because it controls the flow by itself.
Note that readVal function does not clear the sdmarr buffer after sending request...
...so if there is no answer the buffer still contains first request.

But in your case something is received (any loop in the converter?)
because there are no errors that should have happened sooner (if there is no reverse transmission)
like SDM_ERR_TIMEOUT or SDM_ERR_NOT_ENOUGHT_BYTES

Ok, thanks for reply and help.