Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

AttributeError: 'str' object has no attribute 'encode' #9

Closed
Arukaito opened this issue Jan 23, 2019 · 10 comments
Closed

AttributeError: 'str' object has no attribute 'encode' #9

Arukaito opened this issue Jan 23, 2019 · 10 comments

Comments

@Arukaito
Copy link

Circuit Python Version : Adafruit CircuitPython 4.0.0-alpha.5 on 2018-12-10; Adafruit Metro M0 Express with samd21g18
Thermal Printer Version : circuitpython-thermal-printer-4.x-mpy-1.1.2.zip

Im trying to run the example printer code & my code without luck

Example
and it returns this

main.py output:
Traceback (most recent call last):
  File "main.py", line 32, in <module>
  File "adafruit_thermal_printer/thermal_printer.py", line 187, in __init__
  File "adafruit_thermal_printer/thermal_printer.py", line 289, in reset
  File "adafruit_thermal_printer/thermal_printer.py", line 241, in send_command
AttributeError: 'str' object has no attribute 'encode'

The same error occurs with my code and the example code.
Im doing something wrong?

@caternuson
Copy link
Contributor

I couldn't recreate this.

Can you try running this interactively at the REPL as shown below and post results of that.

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-alpha.5 on 2018-12-10; Adafruit Feather M4 Express with samd51j19
>>> import board
>>> import busio
>>> import adafruit_thermal_printer
>>> ThermalPrinter = adafruit_thermal_printer.get_printer_class(2.69)
>>> RX = board.RX
>>> TX = board.TX
>>> uart = busio.UART(TX, RX, baudrate=19200)
>>> printer = ThermalPrinter(uart, auto_warm_up=False)
>>> 

@Arukaito
Copy link
Author

Arukaito commented Jan 23, 2019

Ran in REPL
This is the outcome in MU
Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-alpha.5 on 2018-12-10; Adafruit Metro M0 Express with samd21g18

>>> import board
>>> import busio
>>> import adafruit_thermal_printer
>>> ThermalPrinter = adafruit_thermal_printer.get_printer_class(2.69)
>>> RX = board.RX
>>> TX = board.TX
>>> uart = busio.UART(TX, RX, baudrate=19200)
>>> printer = ThermalPrinter(uart, auto_warm_up=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "adafruit_thermal_printer/thermal_printer.py", line 187, in __init__
  File "adafruit_thermal_printer/thermal_printer.py", line 289, in reset
  File "adafruit_thermal_printer/thermal_printer.py", line 241, in send_command
AttributeError: 'str' object has no attribute 'encode'

Just for extra info.

Thermal Printer Connections
RX is connected to TX in Metro M0 Express (D1)
TX in Left Unconnected (D0)
GD to Board GD

@ladyada
Copy link
Member

ladyada commented Jan 23, 2019

@dhalbert @caternuson i wonder if this is a CPython compatibility thing? M4's have it. we can use some other function

@caternuson
Copy link
Contributor

Hmmm. Yep. Just verifying it's not on M0. Left out for space saving? Will let @dhalbert answer as to why and what's a good substitute.

Adafruit CircuitPython 4.0.0-alpha.5 on 2018-12-10; Adafruit Feather M0 Express with samd21g18
>>> 'encode' in dir("hello")
False
Adafruit CircuitPython 4.0.0-alpha.5 on 2018-12-10; Adafruit Feather M4 Express with samd51j19
>>> 'encode' in dir("hello")
True
>>> 

This may be a related discussion.
adafruit/circuitpython#384
but I defer to the pros.....

@Arukaito
Copy link
Author

Arukaito commented Jan 23, 2019

Is there any workaround for encode with M0 ?
i really wanted to try out my recently termal printer kit, so termal printers are not compatible with M0 with this current build?
Thanks!

Fixed the issue by using Bytes(obj, 'ascii') in thermal_printer.py and converting it to .mpy

@caternuson
Copy link
Contributor

Closing. encode is now supported in M0 boards.

Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit Metro M0 Express with samd21g18
>>> 'encode' in dir("hello")
True
>>> "hello".encode('ascii')
b'hello'
>>> 

@caternuson
Copy link
Contributor

Oops. Except for non-Express M0's (Trinket, Gemma, etc.):

Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit Trinket M0 with samd21e18
>>> 'encode' in dir("hello")
False
Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit Gemma M0 with samd21e18
>>> 'encode' in dir("hello")
False 

Leaving closed since using a thermal printer with a non-Express board should be petty rare and generally not a recommended combo.

@ladyada
Copy link
Member

ladyada commented Aug 16, 2019

@caternuson yep thats ok - please update the readme to let people know?

@caternuson
Copy link
Contributor

okie dokie, here you go: #10

@ladyada
Copy link
Member

ladyada commented Aug 16, 2019

huzzah

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants