-
Notifications
You must be signed in to change notification settings - Fork 10
Unpack isn't working #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
python3 is a little pickier than circuitpython on what you can pass into unpack @kattni @brennen @caternuson can one of y'all check this out and fix |
I can take a look at this after work. |
thanks! yah plz stick to unpack, but i think you'll need to either extend or shrink the bytearray - circuitpython is blase' about the # of bytes matching the unpack string :) |
Does this mean that Circuitpython (or micropython?) had its own "unpack" implementation that is now replaced by the fact that it's running under Python3 on the RPi? Does that explain the odd (to me) syntax, i.e. it's not vanilla unpack? |
nope, struct.unpack has the same syntax, we just have more errorchecking in
python3 - so we just have to be more strict in circuitpython :)
…On Tue, Jan 8, 2019 at 2:40 PM fakecrusader ***@***.***> wrote:
python3 is a little pickier than circuitpython on what you can pass into
unpack
Does this mean that Circuitpython (or micropython?) had its own "unpack"
implementation that is now replaced by the fact that it's running under
Python3 on the RPi? Does that explain the odd (to me) syntax, i.e. it's not
vanilla unpack?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABKG0a65cnF3dldwUaeuy4KOwWL_kWenks5vBPQmgaJpZM4Z19UR>
.
|
@siddacious it's as @ladyada says, so will need to pad the bytearray somehow before calling unpack. Do you want to PR that? Python 3 struct.unpack Issue recreated: pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board, busio
>>> import digitalio
>>> import adafruit_max31856
>>> spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
>>> cs = digitalio.DigitalInOut(board.D21)
>>> cs.direction = digitalio.Direction.OUTPUT
>>> tc = adafruit_max31856.MAX31856(spi, cs)
>>> tc.temperature
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/adafruit_max31856.py", line 159, in temperature
raw_temp = unpack(">i", self._read_register(_MAX31856_LTCBH_REG, 3))[0]
struct.error: unpack requires a bytes object of length 4
>>> Played with a nominal fix: >>> raw = tc._read_register(0x0c, 3)
>>> len(raw)
3
>>> unpack(">i", raw)[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: unpack requires a bytes object of length 4
>>> raw[0:0] = b'\x00'
>>> len(raw)
4
>>> unpack(">i", raw)[0]
92640
>>> NOTE be careful not to redefine your class level >>> len(tc._BUFFER)
4 yeah python |
@siddacious Yup, I can PR it but I'll have to test on a metro/feather and get you or someone else to test on a PI because I don't have one. |
Cool. I can test on RPi setup. And good idea to make sure it works on CP - maybe CP requires things t be blasé and won't like you being serious. :) |
fixes #4 caused by CP's liberal unpack
This problem still seems to exist when using the reference.temperature property. I get the following error: File "ReadMax31856Fault4.py", line 35, in I can read the value using a simple "_read_register" request, but the property is apparently nonworking. I found similar results with the reference_temperature_thresholds property, though the error is slightly different: File "ReadMax31856Fault4.py", line 38, in I'm barely python literate so I can't help with that but I'm working with a PI and a MAX31856 so I'd be happy to do some hardware testing. |
@coffeegoat Sorry to hear that! I'll take a look. |
Have the return be of specified |
@caternuson That's a good idea. I can take a look at it in a while but if you want to give it a go, by all means do so. |
Nah, don't let me hold you up. Go for it. I was just passing by and took a looksie. Might be as simple as: return self._BUFFER[:length] but not sure what kind of thread that might pull. So worth testing. |
I poked at it a bit more, and this snippet worked: ` def reference_temperature(self):
I'm not super clear on the syntax, but I modeled it after the read function with the key difference being the data type and made the assumption that we should only be seeing a couple of bits. It seems to give reasonable values. |
Maybe this should've been a new/separate issue, but hopefully this is an easy fix, so reopening. PR submitted - checkout #6 |
@coffeegoat Please try the 0.8.3 version of the library when it becomes available: |
@caternuson I'll check version 0.8.3 tomorrow morning, I've got a test running right now and don't want to swap libraries in the middle. |
We have success, that fixed it, was it only the minor change in to add: |
Yep. That was the main change. Then had to add some padding to a call that requested 3 bytes that needed to get unpacked with an Glad that fixed it. Thanks for letting us know. |
I am using this library with RPi3 but encounter runtime errors when reading the temperature or reference temperature. The temperature functions use "unpack" when returning the results of register reads. The read_register function returns 3 bytes and although the comments say the unpack will adjust it to the right size I get a runtime error instead saying 4 bytes are expected.
A bit of diagnostic work showed that the bytearray was there and contained 3 bytes. I don't understand the unpack syntax to the point where it should be adding a byte :
raw_temp = unpack(">i", self._read_register(_MAX31856_LTCBH_REG, 3))[0]
There just seems to be a null byte declaration stuck on the end with no link symbol such as '+' as you might expect. It doesn't throw a syntax error but also doesn't seem to work.
Researching similar unpack errors I came across "int.from_bytes()" as a newer Python feature. I gave it a try instead of the "unpack" code and it worked.
`def temperature(self):
This gave me what look like accurate temperatures, pending further calibration.
Any idea why the original code failed for me? It seems the code isn't as portable as it could be?
The text was updated successfully, but these errors were encountered: