escpos package

Submodules

escpos.connections module

escpos.connections.getFilePrinter(commandSet='Generic')[source]

This method does not allow two way communication with the printer and status retiieval and read commands will not be available when using this connection format, so only use FilePrinter class if you are unable to use other printer conection classes.

Parameters:commandSet (str) – Command set to load from escpos.commandset.* namespace (default: ‘Generic’)
Returns:FilePrinter Class
class escpos.connections.FilePrinter
Parameters:dev (str) – A string representing the device. For Unix like systems this would be a serial or USB devicefile

pointing to the printer like /dev/ttyS0. (default: /dev/ttyS0) :param bool initialize: Call initialize() function to reset the printer to default status.(default: True) :returns: FilePrinter object

Return FilePrinter Class with the specified command set in the escpos.commandset.* namespace.

Usage

printer = getFilePrinter(commandSet='Generic')(dev='/dev/ttys2')
printer.text('Hello World')
printer.lf()
escpos.connections.getNetworkPrinter(commandSet='Generic')[source]
Parameters:commandSet (str) – Command set to load from escpos.commandset.* namespace (default: ‘Generic’)
Returns:NetworkPrinter Class
class escpos.connections.NetworkPrinter
Parameters:
  • host (str) – IP address or FQDN for the network escpos printer.
  • port (str) – Network port number for your network escpos printer.(default: 9100)
  • initialize (bool) – Call initialize() function to reset the printer to default status.(default: True)
Returns:

NetworkPrinter object

Return NetworkPrinter Class with the specified command set in the escpos.commandset.* namespace.

Usage

printer = getNetworkPrinter(commandSet='Generic')(host='192.168.168.20', port=9200)
printer.text('Hello World')
printer.lf()
escpos.connections.getSerialPrinter(commandSet='Generic')[source]
Parameters:commandSet (str) – Command set to load from escpos.commandset.* namespace (default: ‘Generic’)
Returns:SerialPrinter Class
class escpos.connections.SerialPrinter
Parameters:
  • dev (str) – A string representing the device. For Unix like systems this would be a devicefile pointing to the printer like /dev/ttyS0. For windows systems this would be a windows serial port address like COM1, COM2, COM3. (default: /dev/ttyS0)
  • baudrate (int) – Baudrate for your printer’s serial port.(default: 9600)
  • bytesize (int) – bytesize for each chunk of data sent over the serial port.
  • timeout (int) – Timeout for the serial port.
  • initialize (bool) – Call initialize() function to reset the printer to default status.(default: True)
Returns:

SerialPrinter object

Return SerialPrinter Class with the specified command set in the escpos.commandset.* namespace.

Usage

printer = getSerialPrinter(commandSet='Generic')(dev='/dev/ttys2', baudrate=19200)
printer.text('Hello World')
printer.lf()
escpos.connections.getUSBPrinter(commandSet='Generic')[source]
Parameters:commandSet (str) – Command set to load from escpos.commandset.* namespace (default: ‘Generic’)
Returns:USBPrinter Class
class escpos.connections.USBPrinter
Parameters:
  • idVendor (int) – 2 byte int(Can be provided in hex representation like 0x1504). Vendor Id for the USB Device.
  • idProduct (int) – 2 byte int(Can be provided in hex representation like 0x0006). Product Id for the USB Device.
  • interface (int) –

    number(hex), USB Input end point Retrieve this value with the following command on UNIX like OSes (default: 0)

    lsusb -vvv -d <vendorId in hex>:<productId in hex> | grep iInterface

  • inputEndPoint (int) –

    1 byte int(Can be provided in hex representation like 0x82), USB Input end point. Retrieve this value with the following command on UNIX like OSes (default: 0x82)

    lsusb -vvv -d <vendorId in hex>:<productId in hex> | grep bEndpointAddress | grep IN

  • outputEndPoint (int) –

    1 byte int(Can be provided in hex representation like 0x01), USB Output end point. Retrieve this value with the following command on UNIX like OSes (default: 0x01)

    lsusb -vvv -d <vendorId in hex>:<productId in hex> | grep bEndpointAddress | grep OUT

  • initialize (bool) – Call initialize() function to reset the printer to default status.(default: True)
Returns:

USBPrinter object

Return USBPrinter Class with the specified command set in the escpos.commandset.* namespace.

Usage

printer = getUSBPrinter(commandSet='Generic')(idVendor=0x1504, idProduct=0x0006)
printer.text('Hello World')
printer.lf()

Module contents

Installation

pip install python-printer-escpos

ESCPOS printer function reference

To try the complete set of default functions available for generic ESCPOS printers, refer to escpos.commandset

USB printers

USB Printers require USB vendorID, productID, interface number, input endpoint id, output endpoint id to create a connection. Read on to see how to get these values for your printer and create a printer object for a USB printer

Getting vendor id and product id

On Unix like OSes, run lsusb command to see a list of devices with their product id and vendor id:

$ lsusb
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 003: ID 1504:0006
Bus 003 Device 008: ID 0cf3:0036 Atheros Communications, Inc.

The second device in this list is the thermal printer. I know this because if I switch off the printer and run lsusb again that line will disappear. That tells me that the vendor id for my printer is 0x1504 and product id is 0x0006

Getting interface id

Following this you can get the interface number for your printer with the following command (Note that I used the vendorid 1504 and productid 0006 for my printer that I discovered in the previous section):

$ lsusb -vvv -d 1504:0006 | grep iInterface
      iInterface              0

So the interface number for my printer is 0, since the module uses a default interface number 0, I do not need to specify this value in my constructor

Getting input endpoint

Again you can use the lsusb to get the input endpoint as follows:

$ lsusb -vvv -d 1504:0006 | grep bEndpointAddress | grep IN
bEndpointAddress     0x81  EP 1 IN

That gives me the input endpoint number 0x81, this is also the default for the module.

Getting output endpoint

You can now use the lsusb to get the output endpoint as follows:

$ lsusb -vvv -d 1504:0006 | grep bEndpointAddress | grep OUT
bEndpointAddress     0x01  EP 1 OUT

That gives me the output endpoint number 0x01, this is also the default for the module.

Get Printer Object

Now that we have the connection parameters for our printer we can create the printer object as follows. We will use the Generic commandset to print output to the printer:

from escpos.connections import getUSBPrinter


printer = getUSBPrinter()(idVendor=0x1504, idProduct=0x0006)  # USB vendor and product Ids for Bixolon SRP-350plus
                                                              # printer
printer.text("Hello World")
printer.lf()

To see the full range of printer functions available for Generic ESCPOS printers see escpos.commandset.generic