MichaluxPL / Sofar_LSW3

Get MODBUS data from Sofar (K-TLX) inverter through LSW-3 or LSE module
GNU General Public License v3.0
94 stars 45 forks source link

UnicodeDecodeError: 'charmap' codec can't decode #30

Closed MartinJSa closed 2 years ago

MartinJSa commented 2 years ago

Hi, firstly thanks very much for your hard work on this, very keen to get it working on my ME3000SP. I'm using Python in Windows 11

When I run the HWData script everything works fine, but when I run InverterData.py I get:

PS C:\Users\marti\OneDrive\Documents\Sofar_LSW3-main-v1-8\Sofar_LSW3-main> python3 .\InverterData.py
Connecting to logger... connected successfully !
*** Chunk no:  0
Sent data:  bytearray(b'\xa5\x17\x00\x10E\x00\x00?\xf1\x13k\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x00\x00\x00(E\xd4\x00\x15')
Received data:  b'\xa5\x10\x00\x10\x15\x00/?\xf1\x13k\x02\x01w\x88L7M\r\x00\x00;p\xd0*\x05\x00\x9b\x15'
Traceback (most recent call last):
  File "C:\Users\marti\OneDrive\Documents\Sofar_LSW3-main-v1-8\Sofar_LSW3-main\InverterData.py", line 186, in <module>
    parameters=json.loads(txtfile.read())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 7995: character maps to <undefined>

I presume there is a character map issue going on here? Any suggestions please?

MichaluxPL commented 2 years ago

Open SOFARMap.xml in notepad (or other text editor) and remove/change all Polish characters, or those that does not look OK. That should do it :) BTW: actually error message tells which character it has problem with: "codec can't decode byte 0x81 in position 7995" - looking at SOFARMap.xml file most probably it complains about this one: BŁĄD :)

MartinJSa commented 2 years ago

I think I may have made a bit of progress, I changed line 185 to be the following:

with open("./SOFARMap.xml", encoding="utf-8") as txtfile:

I now get:

PS C:\Users\marti\OneDrive\Documents\Sofar_LSW3-main-v1-8\Sofar_LSW3-main> python3 .\InverterData.py
Connecting to logger... connected successfully !
*** Chunk no:  0
Sent data:  bytearray(b'\xa5\x17\x00\x10E\x00\x00?\xf1\x13k\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x00\x00\x00(E\xd4\x00\x15')
Received data:  b'\xa5\x10\x00\x10\x15\x00L?\xf1\x13k\x02\x01\xc8\x91L7\x9f\x16\x00\x00;p\xd0*\x05\x00m\x15'
0x0000 - Inverter status: 21
No value in response for register 0x0001
Check register start/end values in config.cfg
MichaluxPL commented 2 years ago

Glad You've made it :) Now, most probably, You have incorrect register ranges configured in config.cfg. You have two options:

  1. Find MODBUS registers specification for You inverter and use it to define the correct register's range.
  2. Play with different register ranges to find the correct one. Unfortunately I wont be able to help You with either of this :(
MartinJSa commented 2 years ago

Thanks for the quick replies!

What's confusing me now is 0x0001 is a valid register, it seems to be a fault register:

image

If I skip that section and start at 0x0006 I get:

PS C:\Users\marti\OneDrive\Documents\Sofar_LSW3-main-v1-8\Sofar_LSW3-main> python3 .\InverterData.py
Connecting to logger... connected successfully !
*** Chunk no:  0
Sent data:  bytearray(b'\xa5\x17\x00\x10E\x00\x00?\xf1\x13k\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x00\x06\x00"%\xd2\x00\x15')
Received data:  b'\xa5\x10\x00\x10\x15\x00]?\xf1\x13k\x02\x01\xa7\x95L7~\x1a\x00\x00;p\xd0*\x05\x00D\x15'
0x0006 - PV1 Voltage: 2.1V
No value in response for register 0x0007
Check register start/end values in config.cfg

I guess my inverter is returning empty values when the script is expecting every register to be populated?

Registry_Details.pdf

MichaluxPL commented 2 years ago

Currently You don't get any valid info from the inverter - it is simply too short (does not contain any inverter's data - only some error code probably). So don't look for 0x0006 value since it is totally misleading now (it's just a coincidence it decodes to some value). Check if this is a correct MODBUS specification for Your type of inverter (as far as I can see You have a hybrid one, while attached documentation seams to be for a normal one - like mine: K-TLX). So first try to play with register ranges to get much longer "Received data" byte string. As long as You get such a short one, it means You don't get any valid production data, so don't try find any readable values, because it's simply not there :)

MichaluxPL commented 2 years ago

For a correct response example string look at closed issue #17 (it's in polish, but first message contains a correct response byte string).

MartinJSa commented 2 years ago

Ah great, thanks very much I'll do more digging!

MartinJSa commented 2 years ago

Just following up on this, I've had this link passed to me which looks more promising as MODBUS spec for the ME3000SP. https://github.com/greentangerine/ME3000/blob/master/SOFARSOLAR%20ModBus-RTU%20Communication%20Protocol.pdf

I'm trying out a few register ranges, and depending on what I go for, I'm getting this error:

Connecting to logger... connected successfully !
Traceback (most recent call last):
  File "C:\Users\marti\OneDrive\Documents\Sofar_LSW3-main-v1-8\Sofar_LSW3-main\InverterData.py", line 141, in <module>
    crc=binascii.unhexlify(str(hex(libscrc.modbus(businessfield))[4:6])+str(hex(libscrc.modbus(businessfield))[2:4])) # CRC16modbus
binascii.Error: Odd-length string

Could you offer any ideas on what's going wrong here please?

MichaluxPL commented 2 years ago

Most likely You have a wrong register's range configured - try to increase/decrease register's range by 1.