u9n / dlms-cosem

A Python library for DLMS/COSEM
Other
79 stars 39 forks source link

Bug: Extend COSEM DataArray in to_bytes #73

Closed real-tintin closed 5 months ago

real-tintin commented 5 months ago

Hi Henrik,

Thanks for your DLMS package, it has been proved very useful.

Issue I found a bug in dlms_cosem/dlms_data.py when converting the COSEM DataArray object to bytes. Atm, it appends to its bytearray from encode_variable_integer which returns bytes, this raises:

TypeError: 'bytes' object cannot be interpreted as an integer

I guess the intention is to extend as done in DataStructure.

Proposal

Change:

class DataArray(BaseDlmsData):
    """Sequence of Data"""

    TAG = 1
    LENGTH = VARIABLE_LENGTH

    def to_bytes(self) -> bytes:
        out = bytearray()
        out.append(self.TAG)
        out.append(encode_variable_integer(len(self.value)))

to:

class DataArray(BaseDlmsData):
    """Sequence of Data"""

    TAG = 1
    LENGTH = VARIABLE_LENGTH

    def to_bytes(self) -> bytes:
        out = bytearray()
        out.append(self.TAG)
        out.extend(encode_variable_integer(len(self.value)))
Krolken commented 5 months ago

Thanks for spotting that. I will make a change in the upcoming release.

real-tintin commented 5 months ago

Thanks for spotting that. I will make a change in the upcoming release.

Tack för den super snabba fixen!