shantanubhadoria / python-printer-escpos

Talk to Dot Matrix, Thermal POS printers supporting ESCPOS Specification
42 stars 11 forks source link

TypeError: a bytes-like object is required, not 'str' #6

Open zm08 opened 4 years ago

zm08 commented 4 years ago
from escpos.connections import getNetworkPrinter

printer = getNetworkPrinter()(host='***.***.*.*', port=9100)

printer.text("Hello World")
printer.lf()
printer.cut()

(I've blanked out my IP address) produces the following error

Traceback (most recent call last):
  File "E:\Users\Zunyou\Desktop\僔佑\Programming\Tests\test6.py", line 3, in <module>
    printer = getNetworkPrinter()(host='192.168.2.1', port=9100)
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\connections.py", line 306, in __init__
    self.initialize()
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\commandset\generic.py", line 463, in initialize
    self._write(self.__class__.__ESC + '@')
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\connections.py", line 328, in _write
    self._device.send(msg)
TypeError: a bytes-like object is required, not 'str'

I have made the necessary amendments from the NetworkPrinter instance has no attribute '_Generic__write' issue, and was subsequently hit with this TypeError

zhaowei699 commented 3 years ago

I have the same issue and please kindly tell me if you have solved this issue.

Lion3000 commented 2 years ago

Hi, I was stuck with the same issue but I created a fix to it 👍 Now my printer works in my network: Solution is : Edit the following file -> python-printer-escpos/src/main/python/escpos/connections.py at the line 324 add

if(type(msg) == str) :
   msg = str.encode(msg)

Final result should look like :

def _write(self, msg):
    """
    Print any command sent in raw format
    """
    if(type(msg) == str) :
       msg = str.encode(msg)

    self._device.send(msg)