stockton
Posts: 136
Joined: Mon Aug 24, 2015 6:06 am
Location: South Africa
Contact: Website

Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 10:50 am

I am attempting to implement python scrip to read my solar inverter.
I am not a python programmer so please forgive silly questions.
When I attempt code I get the following replies:-

Code: Select all

Traceback (most recent call last):
  File "solar2.py", line 28, in <module>
    sendCommand(getCommand('QPI'))
  File "solar2.py", line 4, in getCommand
    crc = crc16.crc16xmodem(cmd).to_bytes(2,'big')
AttributeError: 'int' object has no attribute 'to_bytes'
My code looks like:-

Code: Select all

import crc16
def getCommand(cmd):
    cmd = cmd.encode('utf-8')
    crc = crc16.crc16xmodem(cmd).to_bytes(2,'big')
    cmd = cmd+crc
    cmd = cmd+b'\r'
    while len(cmd)<8:
        cmd = cmd+b'\0'
    return cmd

def sendCommand(cmd):
    dev.ctrl_transfer(0x21, 0x9, 0x200, 0, cmd)

def getResult(timeout=100):
    res=""
    i=0
    while '\r' not in res and i<20:
        try:
            res+="".join([chr(i) for i in dev.read(0x81, 8, timeout) if
i!=0x00])
        except usb.core.USBError as e:
            if e.errno == 110:
                pass
            else:
                raise
        i+=1
    return res

sendCommand(getCommand('QPI'))
res = getResult()
This is on a Raspberry Pi 4 using latest Raspbian with Python 2.7.13

Please tell me where I went wrong.

hippy
Posts: 7415
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 11:14 am

The problem is in using Python 2 not Python 3. There is no '.to_bytes()' attribute for integers in Python 2 but there is in Python 3 -

Code: Select all

integerObject = 123
bytesObject = integerObject.to_bytes(2,'big')
print("Success")

Code: Select all

pi@Pi3B:~/tmp $ python2 tobytes.py 
Traceback (most recent call last):
  File "tobytes.py", line 2, in <module>
    bytesObject = integerObject.to_bytes(2,'big')
AttributeError: 'int' object has no attribute 'to_bytes'

Code: Select all

pi@Pi3B:~/tmp $ python3 tobytes.py 
Success
Last edited by hippy on Tue Feb 25, 2020 11:18 am, edited 1 time in total.

User avatar
B.Goode
Posts: 10144
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 11:18 am

stockton wrote:
Tue Feb 25, 2020 10:50 am
I am attempting to implement python scrip to read my solar inverter.
I am not a python programmer so please forgive silly questions.
When I attempt code I get the following replies:-

Code: Select all

Traceback (most recent call last):
  File "solar2.py", line 28, in <module>
    sendCommand(getCommand('QPI'))
  File "solar2.py", line 4, in getCommand
    crc = crc16.crc16xmodem(cmd).to_bytes(2,'big')
AttributeError: 'int' object has no attribute 'to_bytes'
My code looks like:-

Code: Select all

import crc16
def getCommand(cmd):
    cmd = cmd.encode('utf-8')
    crc = crc16.crc16xmodem(cmd).to_bytes(2,'big')
    cmd = cmd+crc
    cmd = cmd+b'\r'
    while len(cmd)<8:
        cmd = cmd+b'\0'
    return cmd

def sendCommand(cmd):
    dev.ctrl_transfer(0x21, 0x9, 0x200, 0, cmd)

def getResult(timeout=100):
    res=""
    i=0
    while '\r' not in res and i<20:
        try:
            res+="".join([chr(i) for i in dev.read(0x81, 8, timeout) if
i!=0x00])
        except usb.core.USBError as e:
            if e.errno == 110:
                pass
            else:
                raise
        i+=1
    return res

sendCommand(getCommand('QPI'))
res = getResult()
This is on a Raspberry Pi 4 using latest Raspbian with Python 2.7.13

Please tell me where I went wrong.


Do you have any documentation for the crc16 python library module that indicates that to_bytes() is a supported/implemented method for that library?


The examples in ther documentation here do not mention it - but they are only examples and may not be exhaustive - https://pypi.org/project/crc16/



Edit: to acknowledge the pragmatic advice already offered as I was researching this... And subsequently to acknowledge that my response could have been better worded...
Last edited by B.Goode on Tue Feb 25, 2020 11:43 am, edited 2 times in total.

hippy
Posts: 7415
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 11:21 am

B.Goode wrote:
Tue Feb 25, 2020 11:18 am
Do you have any documentation for the crc16 python library module that indicates that to_bytes() is a supported/implemented method for that library?
My reading of the error message is that "crc16.crc16xmodem(cmd)" returns an "int" and ".to_bytes()" applied on an "int" doesn't work in Python 2. It's not actually a 'crc16' library issue AFAICS.

Edit : Also cross-posted while previous being updated.

stockton
Posts: 136
Joined: Mon Aug 24, 2015 6:06 am
Location: South Africa
Contact: Website

Re: Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 12:50 pm

After reinstalling crc16 as python3 when I python3 solar2.py I get

Code: Select all

#!/usr/bin/env python3Traceback (most recent call last):
  File "solar2.py", line 28, in <module>
    sendCommand(getCommand('QPI'))
  File "solar2.py", line 12, in sendCommand
    dev.ctrl_transfer(0x21, 0x9, 0x200, 0, cmd)
NameError: name 'dev' is not defined
[/code
]

User avatar
B.Goode
Posts: 10144
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python crc16 AttributeError: 'int' object has no attribute 'to_bytes'

Tue Feb 25, 2020 1:09 pm

stockton wrote:
Tue Feb 25, 2020 12:50 pm
After reinstalling crc16 as python3 when I python3 solar2.py I get

Code: Select all

#!/usr/bin/env python3Traceback (most recent call last):
  File "solar2.py", line 28, in <module>
    sendCommand(getCommand('QPI'))
  File "solar2.py", line 12, in sendCommand
    dev.ctrl_transfer(0x21, 0x9, 0x200, 0, cmd)
NameError: name 'dev' is not defined



Probably that issue was always there, but was 'masked' by the previous one...

This is the only line in the script that mentions dev

So the error message is succinct and accurate. You haven't specified what this 'dev' object is supposed to refer to.

Return to “Python”