Hi all.
Im receving a number through serial.
b'34\n'
I do this to remove the end of line
data=data.rstrip()
Then I do this to remove the b'
data=data.decode('utf-8')
Then I try to use what seems to be an integer but I get an error.
Is something evidently wrong?
It would help to see the code that's causing the error and what exactly the error is. Remember that we can't see your screen.
Note that "removing the b'" is really the wrong terminology - decode
converts a bytes object into a string.
Given the above, are you actually converting the string into an integer before using it?
(Dec-25-2020, 06:01 PM)ndc85430 Wrote: [ -> ]It would help to see the code that's causing the error and what exactly the error is. Remember that we can't see your screen.
Note that "removing the b'" is really the wrong terminology - decode
converts a bytes object into a string.
Given the above, are you actually converting the string into an integer before using it?
Thanks for the answer.
I did not convert the string to integer. I thought decode converted to integer.
Can I converte directly from bytes to int?
(Dec-25-2020, 07:31 PM)ndc85430 Wrote: [ -> ]Per the docs, int
does accept bytes.
Doesnt seem to be it.
Im trying to light a led on a leds panel, using x and y coordinates
if I do this:
While True:
data=ser.read()
trellis.color(3,5, RED) # these are the x and y coordinates
It works.
But if I do this:
While True:
data=ser.read()
trellis.color(data,5, RED)
I get
Error:
File "this.py", line 102, in <moduel>
trellis.color(data,3,RED)
File "/usr/local/lib/python3.7/dist-packages/adafruit_neotrellis/multitrellis.py", line 71, in color
xkey =x%4
TypeError: not all arguments converted during bytes formatting
I can print the serial read with no problem.
I get
b'4'
So data
is a bytes object? You need to convert it to an int then. Again, the int
function accepts bytes as the documentation says.
(Dec-25-2020, 10:01 PM)ndc85430 Wrote: [ -> ]So data
is a bytes object? You need to convert it to an int then. Again, the int
function accepts bytes as the documentation says.
Data is sent from PC (max/msp) via Serial. Either as an integer or ascii (with same results)
I try:
integer=int.from_bytes(data,¨little¨)
and
integer=int.from_bytes(data, "big")
either way a get
255 or 254
It's not immediately obviously why from_bytes
doesn't do what you expect (but my excuse is that it's early and I'm not fully awake!). What about just calling int
directly?
(Dec-26-2020, 05:58 AM)ndc85430 Wrote: [ -> ]It's not immediately obviously why from_bytes
doesn't do what you expect (but my excuse is that it's early and I'm not fully awake!). What about just calling int
directly?
Sorry. IT WORKS!!
My bad. I had the serial ports with different baud rates.
Thank you !!
Hope you had a good sleep