Printers

Last Reviewed

2017-01-25

As of now there are 5 different type of printer implementations.

USB

The USB-class uses pyusb and libusb to communicate with USB-based printers. Note that this driver is not suited for USB-to-Serial-adapters and similiar devices, but only for those implementing native USB.

class escpos.printer.Usb(idVendor, idProduct, usb_args=None, timeout=0, in_ep=130, out_ep=1, *args, **kwargs)[source]

USB printer

This class describes a printer that natively speaks USB.

inheritance:

Inheritance diagram of escpos.printer.Usb
__init__(idVendor, idProduct, usb_args=None, timeout=0, in_ep=130, out_ep=1, *args, **kwargs)[source]
Parameters
  • idVendor – Vendor ID

  • idProduct – Product ID

  • usb_args – Optional USB arguments (e.g. custom_match)

  • timeout – Is the time limit of the USB operation. Default without timeout.

  • in_ep – Input end point

  • out_ep – Output end point

open(usb_args)[source]

Search device on USB tree and set it as escpos device.

Parameters

usb_args – USB arguments

close()[source]

Release USB interface

Serial

This driver uses pyserial in order to communicate with serial devices. If you are using an USB-based adapter to connect to the serial port, then you should also use this driver. The configuration is often based on DIP-switches that you can set on your printer. For the hardware-configuration please refer to your printer’s manual.

class escpos.printer.Serial(devfile='/dev/ttyS0', baudrate=9600, bytesize=8, timeout=1, parity='N', stopbits=1, xonxoff=False, dsrdtr=True, *args, **kwargs)[source]

Serial printer

This class describes a printer that is connected by serial interface.

inheritance:

Inheritance diagram of escpos.printer.Serial
__init__(devfile='/dev/ttyS0', baudrate=9600, bytesize=8, timeout=1, parity='N', stopbits=1, xonxoff=False, dsrdtr=True, *args, **kwargs)[source]
Parameters
  • devfile – Device file under dev filesystem

  • baudrate – Baud rate for serial transmission

  • bytesize – Serial buffer size

  • timeout – Read/Write timeout

  • parity – Parity checking

  • stopbits – Number of stop bits

  • xonxoff – Software flow control

  • dsrdtr – Hardware flow control (False to enable RTS/CTS)

open()[source]

Setup serial port and set is as escpos device

close()[source]

Close Serial interface

Network

This driver is based on the socket class.

class escpos.printer.Network(host, port=9100, timeout=60, *args, **kwargs)[source]

Network printer

This class is used to attach to a networked printer. You can also use this in order to attach to a printer that is forwarded with socat.

If you have a local printer on parallel port /dev/usb/lp0 then you could start socat with:

socat -u TCP4-LISTEN:4242,reuseaddr,fork OPEN:/dev/usb/lp0

Then you should be able to attach to port 4242 with this class. Otherwise the normal usecase would be to have a printer with ethernet interface. This type of printer should work the same with this class. For the address of the printer check its manuals.

inheritance:

Inheritance diagram of escpos.printer.Network
__init__(host, port=9100, timeout=60, *args, **kwargs)[source]
Parameters
  • host – Printer’s hostname or IP address

  • port – Port to write to

  • timeout – timeout in seconds for the socket-library

open()[source]

Open TCP socket with socket-library and set it as escpos device

close()[source]

Close TCP connection

Troubleshooting

Problems with a network-attached printer can have numerous causes. Make sure that your device has a proper IP address. Often you can check the IP address by triggering the self-test of the device. As a next step try to send text manually to the device. You could use for example:

echo "OK\n" | nc IPADDRESS 9100
# the port number is often 9100

As a last resort try to reset the interface of the printer. This should be described in its manual.

File

This printer “prints” just into a file-handle. Especially on *nix-systems this comes very handy.

class escpos.printer.File(devfile='/dev/usb/lp0', auto_flush=True, *args, **kwargs)[source]

Generic file printer

This class is used for parallel port printer or other printers that are directly attached to the filesystem. Note that you should stay away from using USB-to-Parallel-Adapter since they are unreliable and produce arbitrary errors.

inheritance:

Inheritance diagram of escpos.printer.File
__init__(devfile='/dev/usb/lp0', auto_flush=True, *args, **kwargs)[source]
Parameters
  • devfile – Device file under dev filesystem

  • auto_flush – automatically call flush after every call of _raw()

open()[source]

Open system file

flush()[source]

Flush printing content

close()[source]

Close system file

Dummy

The Dummy-printer is mainly for testing- and debugging-purposes. It stores all of the “output” as raw ESC/POS in a string and returns that.

class escpos.printer.Dummy(*args, **kwargs)[source]

Dummy printer

This class is used for saving commands to a variable, for use in situations where there is no need to send commands to an actual printer. This includes generating print jobs for later use, or testing output.

inheritance:

Inheritance diagram of escpos.printer.Dummy
property output

Get the data that was sent to this printer

clear()[source]

Clear the buffer of the printer

This method can be called if you send the contents to a physical printer and want to use the Dummy printer for new output.