I2C readfrom_mem - can't convert bytearray to int

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

I2C readfrom_mem - can't convert bytearray to int

Post by devnull » Sat Apr 08, 2017 12:33 am

I seem to be unable to get past this error:

Code: Select all

import machine as mc
import ustruct
timebuf = bytearray(7)
i2c = mc.I2C(scl=mc.Pin(5), sda=mc.Pin(4), freq=100000)
ustruct.unpack("<h", i2c.readfrom_mem(104,timebuf,0))[0]

>> TypeError: can't convert bytearray to int
Any idea why ustruct is unable to convert the data to an integer ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: I2C readfrom_mem - can't convert bytearray to int

Post by dhylands » Sat Apr 08, 2017 12:57 am

According to the docs:
http://docs.micropython.org/en/latest/p ... adfrom_mem

The second argument is supposed to be an int and you're passing in timebuf (which is a bytearray). readfrom_mem doesn't even take a bytearray as an argument. readfrom_mem_into takes a buf argument, but its the 3rd argument, not the second.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C readfrom_mem - can't convert bytearray to int

Post by devnull » Sat Apr 08, 2017 1:24 am

Thanks dave, but that link is for pyboard mem_read() and I am using the standard readfrom_mem() on esp8266 device.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: I2C readfrom_mem - can't convert bytearray to int

Post by dhylands » Sat Apr 08, 2017 1:34 am

The docs for the esp8266 look to be the same to me:
http://docs.micropython.org/en/latest/e ... adfrom_mem

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C readfrom_mem - can't convert bytearray to int

Post by devnull » Sat Apr 08, 2017 1:41 am

Code: Select all

readfrom_mem(addr, memaddr, nbytes, *, addrsize=8)
I think the 3rd param (nbytes) is number of bytes, but I am now looking at readfrom_mem_into() which does support a bytearray()

Post Reply