TypeError: can't convert 'str' object to bytes implicitly

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

TypeError: can't convert 'str' object to bytes implicitly

Post by Rebtop »

I took the BLE code and brought it into my scrolling MatrixPortal M4 code.
I am getting a conversion error and do not know how to address it.

def read_ble():
while ble.connected:
# Returns b'' if nothing was read.
one_byte = uart.read(1)
if one_byte:
uart.write(one_byte)
print(one_byte,"during read function")
return one_byte

adv_ble() >>>>>>>>>>>>>>>>>>>>>> advertise
print("next is read-ble")
bytes_in = read_ble() >>>>>>>>>>>>>>>>>>>>>>>>read

line1 = adafruit_display_text.label.Label(
terminalio.FONT,
color=0xff0000,
text= bytes_in) #one_byte) >>>>>>>>>>>>>>>>>>>> line 94
line1.x = display.width
line1.y = 8

I get this detail in terminal mode.

b'a' during read function
b'a' after function >>>>>>>>>>>> this is from my iphone Connect app
Traceback (most recent call last):
File "code.py", line 92, in <module>
File "adafruit_display_text/label.py", line 90, in __init__
File "adafruit_display_text/__init__.py", line 270, in __init__
File "adafruit_display_text/__init__.py", line 345, in background_color
File "adafruit_display_text/label.py", line 189, in _set_background_color
TypeError: can't convert 'str' object to bytes implicitly

Can someone advise me what is causing the error?

Bob

User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by Rebtop »

I want to delete the original post.

After a lot of searching, i found what i needed. It is the encode statement in the sample code.
The problem now is it will only allow numberics. Error SyntaxError('invalid syntax',)

Can i get an idea how to fix this?

def read_ble():
while ble.connected:
s = uart.readline()
if s:
try:
result = str(eval(s))
except Exception as e:
result = repr(e)
uart.write(result.encode("utf-8"))
return result

User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by Rebtop »

K
This is the BLE read function
def read_ble():
while ble.connected:
s = uart.readline()
if s:
try:
result = str(eval(s))
except Exception as e:
result = repr(e)
uart.write(result.encode("utf-8"))
return result
When i enter numerics without spaces, it works. The display scrolls the numbers.
But when i enter alpha characters, i get this error
>>>>>>>>>>>>>>>>>> NameError("name 'a' is not defined",) after function >>>>>>>>>>>> this is from my Iphone with Connect
Different then first error.

User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by Rebtop »

OK
How do i receive Alpha characters? Not just numeric?
The code is your sample, slightly modified, BLE UART on your site.



Than

User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by Rebtop »

eval() is numeric only...............

Got it!

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by neradoc »

Hi,
It's more that "eval" evaluates the string as code. So if the string is "hello", it will try to evaluate a variable named hello.
I believe what you want is to convert the bytes into a string with decode().

Code: Select all

def read_ble():
    while ble.connected:
        s = uart.readline()
        if s:
            result = s.decode("utf8")
            uart.write(s)
            return result
    return ""
You can show your full code too if you want someone to look at it.
(note: Format your code with the "code" button/tags or all indentation is lost).

User avatar
Rebtop
 
Posts: 148
Joined: Fri Jan 31, 2020 9:25 pm

Re: TypeError: can't convert 'str' object to bytes implicitl

Post by Rebtop »

It worked.
I spent too much time trying different code and it turns out this was a simple solution..


Thank you very much for your help.

Bob

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”