2013-08-22 15:16:57 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
'''
|
|
|
|
@author: Manuel F Martinez <manpaz@bashlinux.com>
|
|
|
|
@organization: Bashlinux
|
|
|
|
@copyright: Copyright (c) 2012 Bashlinux
|
|
|
|
@license: GPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
import usb.core
|
|
|
|
import usb.util
|
|
|
|
import serial
|
|
|
|
import socket
|
|
|
|
|
2014-05-31 14:36:00 +00:00
|
|
|
from escpos.escpos import *
|
|
|
|
from escpos.constants import *
|
|
|
|
from escpos.exceptions import *
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
class Usb(Escpos):
|
|
|
|
""" Define USB printer """
|
|
|
|
|
|
|
|
def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01):
|
|
|
|
"""
|
|
|
|
@param idVendor : Vendor ID
|
|
|
|
@param idProduct : Product ID
|
|
|
|
@param interface : USB device interface
|
|
|
|
@param in_ep : Input end point
|
|
|
|
@param out_ep : Output end point
|
|
|
|
"""
|
|
|
|
self.idVendor = idVendor
|
|
|
|
self.idProduct = idProduct
|
|
|
|
self.interface = interface
|
|
|
|
self.in_ep = in_ep
|
|
|
|
self.out_ep = out_ep
|
2014-03-13 17:46:24 +00:00
|
|
|
self.open()
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
""" Search device on USB tree and set is as escpos device """
|
|
|
|
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
|
|
|
if self.device is None:
|
2013-02-27 14:10:34 +00:00
|
|
|
raise NotFoundError("Device not found or cable not plugged in.")
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
if self.device.is_kernel_driver_active(0):
|
|
|
|
try:
|
|
|
|
self.device.detach_kernel_driver(0)
|
|
|
|
except usb.core.USBError as e:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Could not detatch kernel driver: %s" % str(e))
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.device.set_configuration()
|
|
|
|
self.device.reset()
|
|
|
|
except usb.core.USBError as e:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Could not set configuration: %s" % str(e))
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _raw(self, msg):
|
|
|
|
""" Print any command sent in raw format """
|
|
|
|
self.device.write(self.out_ep, msg, self.interface)
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
""" Release USB interface """
|
|
|
|
if self.device:
|
|
|
|
usb.util.dispose_resources(self.device)
|
|
|
|
self.device = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Serial(Escpos):
|
|
|
|
""" Define Serial printer """
|
|
|
|
|
|
|
|
def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1):
|
|
|
|
"""
|
|
|
|
@param devfile : Device file under dev filesystem
|
|
|
|
@param baudrate : Baud rate for serial transmission
|
|
|
|
@param bytesize : Serial buffer size
|
|
|
|
@param timeout : Read/Write timeout
|
|
|
|
"""
|
|
|
|
self.devfile = devfile
|
|
|
|
self.baudrate = baudrate
|
|
|
|
self.bytesize = bytesize
|
|
|
|
self.timeout = timeout
|
|
|
|
self.open()
|
|
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
""" Setup serial port and set is as escpos device """
|
|
|
|
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate, bytesize=self.bytesize, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=self.timeout, dsrdtr=True)
|
|
|
|
|
|
|
|
if self.device is not None:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Serial printer enabled")
|
2013-08-22 15:16:57 +00:00
|
|
|
else:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Unable to open serial printer on: %s" % self.devfile)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _raw(self, msg):
|
|
|
|
""" Print any command sent in raw format """
|
|
|
|
self.device.write(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
""" Close Serial interface """
|
|
|
|
if self.device is not None:
|
|
|
|
self.device.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Network(Escpos):
|
|
|
|
""" Define Network printer """
|
|
|
|
|
|
|
|
def __init__(self,host,port=9100):
|
|
|
|
"""
|
|
|
|
@param host : Printer's hostname or IP address
|
|
|
|
@param port : Port to write to
|
|
|
|
"""
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.open()
|
|
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
""" Open TCP socket and set it as escpos device """
|
|
|
|
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.device.connect((self.host, self.port))
|
|
|
|
|
|
|
|
if self.device is None:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Could not open socket for %s" % self.host)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _raw(self, msg):
|
|
|
|
""" Print any command sent in raw format """
|
2014-03-14 16:05:47 +00:00
|
|
|
self.device.sendall(msg)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
""" Close TCP connection """
|
|
|
|
self.device.close()
|
|
|
|
|
|
|
|
|
|
|
|
class File(Escpos):
|
|
|
|
""" Define Generic file printer """
|
|
|
|
|
|
|
|
def __init__(self, devfile="/dev/usb/lp0"):
|
|
|
|
"""
|
|
|
|
@param devfile : Device file under dev filesystem
|
|
|
|
"""
|
|
|
|
self.devfile = devfile
|
|
|
|
self.open()
|
|
|
|
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
""" Open system file """
|
2014-05-31 14:36:00 +00:00
|
|
|
self.device = open(self.devfile, "wb")
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
if self.device is None:
|
2014-05-31 14:36:00 +00:00
|
|
|
print("Could not open the specified file %s" % self.devfile)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _raw(self, msg):
|
|
|
|
""" Print any command sent in raw format """
|
|
|
|
self.device.write(msg);
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
""" Close system file """
|
|
|
|
self.device.close()
|