Merge branch 'master' of github.com:braveheuel/python-escpos into development
This commit is contained in:
commit
6cc325b395
|
@ -1,10 +1,9 @@
|
||||||
# python temporary files
|
# python temporary files
|
||||||
syntax: glob
|
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|
||||||
# editor autosaves
|
# editor autosaves
|
||||||
$~
|
$~
|
||||||
|
.idea/
|
||||||
|
|
||||||
# temporary data
|
# temporary data
|
||||||
syntax: regexp
|
|
||||||
temp
|
temp
|
|
@ -42,7 +42,7 @@ CHARCODE_PC865 = '\x1b\x74\x05' # Nordic
|
||||||
CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana
|
CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana
|
||||||
CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji
|
CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji
|
||||||
CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji
|
CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji
|
||||||
CHARCODE_PC1252 = '\x1b\x74\x10' # Western European Windows Code Set
|
CHARCODE_PC1252 = '\x1b\x74\x11' # Western European Windows Code Set
|
||||||
CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2
|
CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2
|
||||||
CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2
|
CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2
|
||||||
CHARCODE_PC858 = '\x1b\x74\x14' # Euro
|
CHARCODE_PC858 = '\x1b\x74\x14' # Euro
|
||||||
|
|
206
escpos/escpos.py
206
escpos/escpos.py
|
@ -13,51 +13,83 @@ except ImportError:
|
||||||
|
|
||||||
import qrcode
|
import qrcode
|
||||||
import time
|
import time
|
||||||
|
import textwrap
|
||||||
|
import binascii
|
||||||
|
|
||||||
from constants import *
|
from .constants import *
|
||||||
from exceptions import *
|
from .exceptions import *
|
||||||
|
|
||||||
class Escpos:
|
from abc import ABCMeta, abstractmethod # abstract base class support
|
||||||
|
|
||||||
|
class Escpos(object):
|
||||||
""" ESC/POS Printer object """
|
""" ESC/POS Printer object """
|
||||||
|
__metaclass__ = ABCMeta
|
||||||
device = None
|
device = None
|
||||||
|
|
||||||
|
|
||||||
def _check_image_size(self, size):
|
def __init__(self, columns=32):
|
||||||
""" Check and fix the size of the image to 32 bits """
|
""" Initialize ESCPOS Printer
|
||||||
|
|
||||||
|
:param columns: Text columns used by the printer. Defaults to 32."""
|
||||||
|
self.columns = columns
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _raw(self, msg):
|
||||||
|
""" Sends raw data to the printer
|
||||||
|
|
||||||
|
This function has to be individually implemented by the implementations.
|
||||||
|
:param msg: message string to be sent to the printer
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _check_image_size(size):
|
||||||
|
""" Check and fix the size of the image to 32 bits
|
||||||
|
|
||||||
|
:param size: size of the image
|
||||||
|
:returns: tuple of image borders
|
||||||
|
:rtype: (int, int)
|
||||||
|
"""
|
||||||
if size % 32 == 0:
|
if size % 32 == 0:
|
||||||
return (0, 0)
|
return 0, 0
|
||||||
else:
|
else:
|
||||||
image_border = 32 - (size % 32)
|
image_border = 32 - (size % 32)
|
||||||
if (image_border % 2) == 0:
|
if (image_border % 2) == 0:
|
||||||
return (image_border / 2, image_border / 2)
|
return (round(image_border / 2), round(image_border / 2))
|
||||||
else:
|
else:
|
||||||
return (image_border / 2, (image_border / 2) + 1)
|
return (round(image_border / 2), round((image_border / 2) + 1))
|
||||||
|
|
||||||
|
|
||||||
def _print_image(self, line, size):
|
def _print_image(self, line, size):
|
||||||
""" Print formatted image """
|
""" Print formatted image
|
||||||
|
|
||||||
|
:param line:
|
||||||
|
:param size:
|
||||||
|
"""
|
||||||
i = 0
|
i = 0
|
||||||
cont = 0
|
cont = 0
|
||||||
buffer = ""
|
pbuffer = ""
|
||||||
|
|
||||||
self._raw(S_RASTER_N)
|
self._raw(S_RASTER_N)
|
||||||
buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1]&0xff, size[1]>>8)
|
pbuffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8)
|
||||||
self._raw(buffer.decode('hex'))
|
self._raw(binascii.unhexlify(pbuffer))
|
||||||
buffer = ""
|
pbuffer = ""
|
||||||
|
|
||||||
while i < len(line):
|
while i < len(line):
|
||||||
hex_string = int(line[i:i+8], 2)
|
hex_string = int(line[i:i+8], 2)
|
||||||
buffer += "%02X" % hex_string
|
pbuffer += "%02X" % hex_string
|
||||||
i += 8
|
i += 8
|
||||||
cont += 1
|
cont += 1
|
||||||
if cont % 4 == 0:
|
if cont % 4 == 0:
|
||||||
self._raw(buffer.decode("hex"))
|
self._raw(binascii.unhexlify(pbuffer))
|
||||||
buffer = ""
|
pbuffer = ""
|
||||||
cont = 0
|
cont = 0
|
||||||
|
|
||||||
|
|
||||||
def _convert_image(self, im):
|
def _convert_image(self, im):
|
||||||
""" Parse image and prepare it to a printable format """
|
""" Parse image and prepare it to a printable format
|
||||||
|
|
||||||
|
:param im: image data
|
||||||
|
:raises: ImageSizeError
|
||||||
|
"""
|
||||||
pixels = []
|
pixels = []
|
||||||
pix_line = ""
|
pix_line = ""
|
||||||
im_left = ""
|
im_left = ""
|
||||||
|
@ -65,7 +97,6 @@ class Escpos:
|
||||||
switch = 0
|
switch = 0
|
||||||
img_size = [0, 0]
|
img_size = [0, 0]
|
||||||
|
|
||||||
|
|
||||||
if im.size[0] > 512:
|
if im.size[0] > 512:
|
||||||
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
||||||
if im.size[1] > 0xffff:
|
if im.size[1] > 0xffff:
|
||||||
|
@ -95,7 +126,7 @@ class Escpos:
|
||||||
else:
|
else:
|
||||||
pix_line += im_pattern[x]
|
pix_line += im_pattern[x]
|
||||||
break
|
break
|
||||||
elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3):
|
elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3):
|
||||||
pix_line += im_pattern[-1]
|
pix_line += im_pattern[-1]
|
||||||
break
|
break
|
||||||
pix_line += im_right
|
pix_line += im_right
|
||||||
|
@ -103,9 +134,11 @@ class Escpos:
|
||||||
|
|
||||||
self._print_image(pix_line, img_size)
|
self._print_image(pix_line, img_size)
|
||||||
|
|
||||||
|
|
||||||
def image(self, path_img):
|
def image(self, path_img):
|
||||||
""" Open image file """
|
""" Open image file
|
||||||
|
|
||||||
|
:param path_img: path to image
|
||||||
|
"""
|
||||||
im_open = Image.open(path_img)
|
im_open = Image.open(path_img)
|
||||||
|
|
||||||
# Remove the alpha channel on transparent images
|
# Remove the alpha channel on transparent images
|
||||||
|
@ -119,9 +152,44 @@ class Escpos:
|
||||||
# Convert the RGB image in printable image
|
# Convert the RGB image in printable image
|
||||||
self._convert_image(im)
|
self._convert_image(im)
|
||||||
|
|
||||||
|
def direct_image(self, image):
|
||||||
|
""" Send image to printer"""
|
||||||
|
mask = 0x80
|
||||||
|
i = 0
|
||||||
|
temp = 0
|
||||||
|
|
||||||
|
(width, height) = image.size
|
||||||
|
self._raw(S_RASTER_N)
|
||||||
|
headerX = int(width / 8)
|
||||||
|
headerY = height
|
||||||
|
buf = "%02X" % (headerX & 0xff)
|
||||||
|
buf += "%02X" % ((headerX >> 8) & 0xff)
|
||||||
|
buf += "%02X" % (headerY & 0xff)
|
||||||
|
buf += "%02X" % ((headerY >> 8) & 0xff)
|
||||||
|
#self._raw(binascii.unhexlify(buf))
|
||||||
|
for y in range(height):
|
||||||
|
for x in range(width):
|
||||||
|
value = image.getpixel((x,y))
|
||||||
|
value = (value << 8) | value;
|
||||||
|
if value == 0:
|
||||||
|
temp |= mask
|
||||||
|
|
||||||
|
mask = mask >> 1
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
if i == 8:
|
||||||
|
buf += ("%02X" % temp)
|
||||||
|
mask = 0x80
|
||||||
|
i = 0
|
||||||
|
temp = 0
|
||||||
|
self._raw(binascii.unhexlify(bytes(buf, "ascii")))
|
||||||
|
self._raw('\n')
|
||||||
|
|
||||||
def qr(self, text):
|
def qr(self, text):
|
||||||
""" Print QR Code for the provided string """
|
""" Print QR Code for the provided string
|
||||||
|
|
||||||
|
:param text: text to generate a QR-Code from
|
||||||
|
"""
|
||||||
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
|
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
|
||||||
qr_code.add_data(text)
|
qr_code.add_data(text)
|
||||||
qr_code.make(fit=True)
|
qr_code.make(fit=True)
|
||||||
|
@ -131,9 +199,14 @@ class Escpos:
|
||||||
# Convert the RGB image in printable image
|
# Convert the RGB image in printable image
|
||||||
self._convert_image(im)
|
self._convert_image(im)
|
||||||
|
|
||||||
|
|
||||||
def charcode(self, code):
|
def charcode(self, code):
|
||||||
""" Set Character Code Table """
|
""" Set Character Code Table
|
||||||
|
|
||||||
|
Sends the control sequence from constants.py to the printer with :py:meth:`escpos.printer._raw()`.
|
||||||
|
|
||||||
|
:param code: Name of CharCode
|
||||||
|
:raises: CharCodeError
|
||||||
|
"""
|
||||||
if code.upper() == "USA":
|
if code.upper() == "USA":
|
||||||
self._raw(CHARCODE_PC437)
|
self._raw(CHARCODE_PC437)
|
||||||
elif code.upper() == "JIS":
|
elif code.upper() == "JIS":
|
||||||
|
@ -152,8 +225,8 @@ class Escpos:
|
||||||
self._raw(CHARCODE_GREEK)
|
self._raw(CHARCODE_GREEK)
|
||||||
elif code.upper() == "HEBREW":
|
elif code.upper() == "HEBREW":
|
||||||
self._raw(CHARCODE_HEBREW)
|
self._raw(CHARCODE_HEBREW)
|
||||||
elif code.upper() == "LATVIAN":
|
# elif code.upper() == "LATVIAN": # this is not listed in the constants
|
||||||
self._raw(CHARCODE_PC755)
|
# self._raw(CHARCODE_PC755)
|
||||||
elif code.upper() == "WPC1252":
|
elif code.upper() == "WPC1252":
|
||||||
self._raw(CHARCODE_PC1252)
|
self._raw(CHARCODE_PC1252)
|
||||||
elif code.upper() == "CIRILLIC2":
|
elif code.upper() == "CIRILLIC2":
|
||||||
|
@ -180,7 +253,16 @@ class Escpos:
|
||||||
raise CharCodeError()
|
raise CharCodeError()
|
||||||
|
|
||||||
def barcode(self, code, bc, width, height, pos, font):
|
def barcode(self, code, bc, width, height, pos, font):
|
||||||
""" Print Barcode """
|
""" Print Barcode
|
||||||
|
|
||||||
|
:param code: data for barcode
|
||||||
|
:param bc: barcode format, see constants.py
|
||||||
|
:param width: barcode width, has to be between 1 and 255
|
||||||
|
:param height: barcode height, has to be between 2 and 6
|
||||||
|
:param pos: position of text in barcode, default when nothing supplied is below
|
||||||
|
:param font: select font, default is font A
|
||||||
|
:raises: BarcodeSizeError, BarcodeTypeError, BarcodeCodeError
|
||||||
|
"""
|
||||||
# Align Bar Code()
|
# Align Bar Code()
|
||||||
self._raw(TXT_ALIGN_CT)
|
self._raw(TXT_ALIGN_CT)
|
||||||
# Height
|
# Height
|
||||||
|
@ -228,19 +310,35 @@ class Escpos:
|
||||||
if code:
|
if code:
|
||||||
self._raw(code)
|
self._raw(code)
|
||||||
else:
|
else:
|
||||||
raise exception.BarcodeCodeError()
|
raise BarcodeCodeError()
|
||||||
|
|
||||||
|
|
||||||
def text(self, txt):
|
def text(self, txt):
|
||||||
""" Print alpha-numeric text """
|
""" Print alpha-numeric text
|
||||||
|
|
||||||
|
The text has to be encoded in the currently selected codepage.
|
||||||
|
:param txt: text to be printed
|
||||||
|
:raises: TextError
|
||||||
|
"""
|
||||||
if txt:
|
if txt:
|
||||||
self._raw(txt)
|
self._raw(txt)
|
||||||
else:
|
else:
|
||||||
raise TextError()
|
raise TextError()
|
||||||
|
|
||||||
|
def block_text(self, txt, columns=None):
|
||||||
|
'''Text is printed wrapped to specified columns'''
|
||||||
|
colCount = self.columns if columns == None else columns
|
||||||
|
self.text(textwrap.fill(txt, colCount))
|
||||||
|
|
||||||
def set(self, align='left', font='a', type='normal', width=1, height=1, density=9):
|
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9):
|
||||||
""" Set text properties """
|
""" Set text properties by sending them to the printer
|
||||||
|
|
||||||
|
:param align: alignment of text
|
||||||
|
:param font: font A or B
|
||||||
|
:param text_type: add bold or underlined
|
||||||
|
:param width: text width, normal or double width
|
||||||
|
:param height: text height, normal or double height
|
||||||
|
:param density: print density
|
||||||
|
"""
|
||||||
# Width
|
# Width
|
||||||
if height == 2 and width == 2:
|
if height == 2 and width == 2:
|
||||||
self._raw(TXT_NORMAL)
|
self._raw(TXT_NORMAL)
|
||||||
|
@ -254,22 +352,22 @@ class Escpos:
|
||||||
else: # DEFAULT SIZE: NORMAL
|
else: # DEFAULT SIZE: NORMAL
|
||||||
self._raw(TXT_NORMAL)
|
self._raw(TXT_NORMAL)
|
||||||
# Type
|
# Type
|
||||||
if type.upper() == "B":
|
if text_type.upper() == "B":
|
||||||
self._raw(TXT_BOLD_ON)
|
self._raw(TXT_BOLD_ON)
|
||||||
self._raw(TXT_UNDERL_OFF)
|
self._raw(TXT_UNDERL_OFF)
|
||||||
elif type.upper() == "U":
|
elif text_type.upper() == "U":
|
||||||
self._raw(TXT_BOLD_OFF)
|
self._raw(TXT_BOLD_OFF)
|
||||||
self._raw(TXT_UNDERL_ON)
|
self._raw(TXT_UNDERL_ON)
|
||||||
elif type.upper() == "U2":
|
elif text_type.upper() == "U2":
|
||||||
self._raw(TXT_BOLD_OFF)
|
self._raw(TXT_BOLD_OFF)
|
||||||
self._raw(TXT_UNDERL2_ON)
|
self._raw(TXT_UNDERL2_ON)
|
||||||
elif type.upper() == "BU":
|
elif text_type.upper() == "BU":
|
||||||
self._raw(TXT_BOLD_ON)
|
self._raw(TXT_BOLD_ON)
|
||||||
self._raw(TXT_UNDERL_ON)
|
self._raw(TXT_UNDERL_ON)
|
||||||
elif type.upper() == "BU2":
|
elif text_type.upper() == "BU2":
|
||||||
self._raw(TXT_BOLD_ON)
|
self._raw(TXT_BOLD_ON)
|
||||||
self._raw(TXT_UNDERL2_ON)
|
self._raw(TXT_UNDERL2_ON)
|
||||||
elif type.upper == "NORMAL":
|
elif text_type.upper == "NORMAL":
|
||||||
self._raw(TXT_BOLD_OFF)
|
self._raw(TXT_BOLD_OFF)
|
||||||
self._raw(TXT_UNDERL_OFF)
|
self._raw(TXT_UNDERL_OFF)
|
||||||
# Font
|
# Font
|
||||||
|
@ -306,9 +404,11 @@ class Escpos:
|
||||||
else: # DEFAULT: DOES NOTHING
|
else: # DEFAULT: DOES NOTHING
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def cut(self, mode=''):
|
def cut(self, mode=''):
|
||||||
""" Cut paper """
|
""" Cut paper
|
||||||
|
|
||||||
|
:param mode: set to 'PART' for a partial cut
|
||||||
|
"""
|
||||||
# Fix the size between last line and cut
|
# Fix the size between last line and cut
|
||||||
# TODO: handle this with a line feed
|
# TODO: handle this with a line feed
|
||||||
self._raw("\n\n\n\n\n\n")
|
self._raw("\n\n\n\n\n\n")
|
||||||
|
@ -317,9 +417,13 @@ class Escpos:
|
||||||
else: # DEFAULT MODE: FULL CUT
|
else: # DEFAULT MODE: FULL CUT
|
||||||
self._raw(PAPER_FULL_CUT)
|
self._raw(PAPER_FULL_CUT)
|
||||||
|
|
||||||
|
|
||||||
def cashdraw(self, pin):
|
def cashdraw(self, pin):
|
||||||
""" Send pulse to kick the cash drawer """
|
""" Send pulse to kick the cash drawer
|
||||||
|
|
||||||
|
Kick cash drawer on pin 2 or pin 5.
|
||||||
|
:param pin: pin number
|
||||||
|
:raises: CashDrawerError
|
||||||
|
"""
|
||||||
if pin == 2:
|
if pin == 2:
|
||||||
self._raw(CD_KICK_2)
|
self._raw(CD_KICK_2)
|
||||||
elif pin == 5:
|
elif pin == 5:
|
||||||
|
@ -327,9 +431,11 @@ class Escpos:
|
||||||
else:
|
else:
|
||||||
raise CashDrawerError()
|
raise CashDrawerError()
|
||||||
|
|
||||||
|
|
||||||
def hw(self, hw):
|
def hw(self, hw):
|
||||||
""" Hardware operations """
|
""" Hardware operations
|
||||||
|
|
||||||
|
:param hw: hardware action
|
||||||
|
"""
|
||||||
if hw.upper() == "INIT":
|
if hw.upper() == "INIT":
|
||||||
self._raw(HW_INIT)
|
self._raw(HW_INIT)
|
||||||
elif hw.upper() == "SELECT":
|
elif hw.upper() == "SELECT":
|
||||||
|
@ -339,12 +445,14 @@ class Escpos:
|
||||||
else: # DEFAULT: DOES NOTHING
|
else: # DEFAULT: DOES NOTHING
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def control(self, ctl, pos=4):
|
def control(self, ctl, pos=4):
|
||||||
""" Feed control sequences """
|
""" Feed control sequences
|
||||||
|
|
||||||
|
:raises: TabPosError
|
||||||
|
"""
|
||||||
# Set tab positions
|
# Set tab positions
|
||||||
if pos < 1 or pos > 16:
|
if pos < 1 or pos > 16:
|
||||||
raise TabError()
|
raise TabPosError()
|
||||||
else:
|
else:
|
||||||
self._raw("".join([CTL_SET_HT, hex(pos)]))
|
self._raw("".join([CTL_SET_HT, hex(pos)]))
|
||||||
# Set position
|
# Set position
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
""" ESC/POS Exceptions classes """
|
""" ESC/POS Exceptions classes """
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
""" Base class for ESC/POS errors """
|
""" Base class for ESC/POS errors """
|
||||||
|
@ -27,6 +26,7 @@ class Error(Exception):
|
||||||
|
|
||||||
|
|
||||||
class BarcodeTypeError(Error):
|
class BarcodeTypeError(Error):
|
||||||
|
"""No Barcode type defined """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -35,7 +35,9 @@ class BarcodeTypeError(Error):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "No Barcode type is defined"
|
return "No Barcode type is defined"
|
||||||
|
|
||||||
|
|
||||||
class BarcodeSizeError(Error):
|
class BarcodeSizeError(Error):
|
||||||
|
""" Barcode size is out of range """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -44,16 +46,20 @@ class BarcodeSizeError(Error):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Barcode size is out of range"
|
return "Barcode size is out of range"
|
||||||
|
|
||||||
|
|
||||||
class BarcodeCodeError(Error):
|
class BarcodeCodeError(Error):
|
||||||
|
""" No Barcode code was supplied """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
self.resultcode = 30
|
self.resultcode = 30
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Code was not supplied"
|
return "No Barcode code was supplied"
|
||||||
|
|
||||||
|
|
||||||
class ImageSizeError(Error):
|
class ImageSizeError(Error):
|
||||||
|
""" Image height is longer than 255px and can't be printed """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -62,7 +68,9 @@ class ImageSizeError(Error):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Image height is longer than 255px and can't be printed"
|
return "Image height is longer than 255px and can't be printed"
|
||||||
|
|
||||||
|
|
||||||
class TextError(Error):
|
class TextError(Error):
|
||||||
|
""" Test sting must be supplied to the text() method """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -73,6 +81,7 @@ class TextError(Error):
|
||||||
|
|
||||||
|
|
||||||
class CashDrawerError(Error):
|
class CashDrawerError(Error):
|
||||||
|
""" Valid pin must be set to send pulse """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -82,7 +91,8 @@ class CashDrawerError(Error):
|
||||||
return "Valid pin must be set to send pulse"
|
return "Valid pin must be set to send pulse"
|
||||||
|
|
||||||
|
|
||||||
class TabError(Error):
|
class TabPosError(Error):
|
||||||
|
""" Valid tab positions must be in the range 0 to 16 """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
@ -93,6 +103,7 @@ class TabError(Error):
|
||||||
|
|
||||||
|
|
||||||
class CharCodeError(Error):
|
class CharCodeError(Error):
|
||||||
|
""" Valid char code must be set """
|
||||||
def __init__(self, msg=""):
|
def __init__(self, msg=""):
|
||||||
Error.__init__(self, msg)
|
Error.__init__(self, msg)
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
|
|
@ -11,21 +11,23 @@ import usb.util
|
||||||
import serial
|
import serial
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from escpos import *
|
from .escpos import *
|
||||||
from constants import *
|
from .constants import *
|
||||||
from exceptions import *
|
from .exceptions import *
|
||||||
|
|
||||||
|
|
||||||
class Usb(Escpos):
|
class Usb(Escpos):
|
||||||
""" Define USB printer """
|
""" Define USB printer """
|
||||||
|
|
||||||
def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01):
|
def __init__(self, idVendor, idProduct, interface=0, in_ep=0x82, out_ep=0x01, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@param idVendor : Vendor ID
|
:param idVendor: Vendor ID
|
||||||
@param idProduct : Product ID
|
:param idProduct: Product ID
|
||||||
@param interface : USB device interface
|
:param interface: USB device interface
|
||||||
@param in_ep : Input end point
|
:param in_ep: Input end point
|
||||||
@param out_ep : Output end point
|
:param out_ep: Output end point
|
||||||
"""
|
"""
|
||||||
|
Escpos.__init__(self, *args, **kwargs)
|
||||||
self.idVendor = idVendor
|
self.idVendor = idVendor
|
||||||
self.idProduct = idProduct
|
self.idProduct = idProduct
|
||||||
self.interface = interface
|
self.interface = interface
|
||||||
|
@ -33,12 +35,11 @@ class Usb(Escpos):
|
||||||
self.out_ep = out_ep
|
self.out_ep = out_ep
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
""" Search device on USB tree and set is as escpos device """
|
""" Search device on USB tree and set it as escpos device """
|
||||||
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
||||||
if self.device is None:
|
if self.device is None:
|
||||||
print "Cable isn't plugged in"
|
print("Cable isn't plugged in")
|
||||||
|
|
||||||
check_driver = None
|
check_driver = None
|
||||||
|
|
||||||
|
@ -52,20 +53,18 @@ class Usb(Escpos):
|
||||||
self.device.detach_kernel_driver(0)
|
self.device.detach_kernel_driver(0)
|
||||||
except usb.core.USBError as e:
|
except usb.core.USBError as e:
|
||||||
if check_driver is not None:
|
if check_driver is not None:
|
||||||
print "Could not detatch kernel driver: %s" % str(e)
|
print("Could not detatch kernel driver: %s" % str(e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.device.set_configuration()
|
self.device.set_configuration()
|
||||||
self.device.reset()
|
self.device.reset()
|
||||||
except usb.core.USBError as e:
|
except usb.core.USBError as e:
|
||||||
print "Could not set configuration: %s" % str(e)
|
print("Could not set configuration: %s" % str(e))
|
||||||
|
|
||||||
|
|
||||||
def _raw(self, msg):
|
def _raw(self, msg):
|
||||||
""" Print any command sent in raw format """
|
""" Print any command sent in raw format """
|
||||||
self.device.write(self.out_ep, msg, self.interface)
|
self.device.write(self.out_ep, msg, self.interface)
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
""" Release USB interface """
|
""" Release USB interface """
|
||||||
if self.device:
|
if self.device:
|
||||||
|
@ -73,13 +72,12 @@ class Usb(Escpos):
|
||||||
self.device = None
|
self.device = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Serial(Escpos):
|
class Serial(Escpos):
|
||||||
""" Define Serial printer """
|
""" Define Serial printer """
|
||||||
|
|
||||||
def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1,
|
def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1,
|
||||||
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
|
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
|
||||||
xonxoff=False , dsrdtr=True):
|
xonxoff=False , dsrdtr=True, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@param devfile : Device file under dev filesystem
|
@param devfile : Device file under dev filesystem
|
||||||
@param baudrate : Baud rate for serial transmission
|
@param baudrate : Baud rate for serial transmission
|
||||||
|
@ -91,11 +89,11 @@ class Serial(Escpos):
|
||||||
@param xonxoff : Software flow control
|
@param xonxoff : Software flow control
|
||||||
@param dsrdtr : Hardware flow control (False to enable RTS/CTS)
|
@param dsrdtr : Hardware flow control (False to enable RTS/CTS)
|
||||||
"""
|
"""
|
||||||
|
Escpos.__init__(self, *args, **kwargs)
|
||||||
self.devfile = devfile
|
self.devfile = devfile
|
||||||
self.baudrate = baudrate
|
self.baudrate = baudrate
|
||||||
self.bytesize = bytesize
|
self.bytesize = bytesize
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
self.parity = parity
|
self.parity = parity
|
||||||
self.stopbits = stopbits
|
self.stopbits = stopbits
|
||||||
self.xonxoff = xonxoff
|
self.xonxoff = xonxoff
|
||||||
|
@ -103,7 +101,6 @@ class Serial(Escpos):
|
||||||
|
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
""" Setup serial port and set is as escpos device """
|
""" Setup serial port and set is as escpos device """
|
||||||
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate,
|
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate,
|
||||||
|
@ -112,80 +109,79 @@ class Serial(Escpos):
|
||||||
xonxoff=self.xonxoff, dsrdtr=self.dsrdtr)
|
xonxoff=self.xonxoff, dsrdtr=self.dsrdtr)
|
||||||
|
|
||||||
if self.device is not None:
|
if self.device is not None:
|
||||||
print "Serial printer enabled"
|
print("Serial printer enabled")
|
||||||
else:
|
else:
|
||||||
print "Unable to open serial printer on: %s" % self.devfile
|
print("Unable to open serial printer on: %s" % self.devfile)
|
||||||
|
|
||||||
|
|
||||||
def _raw(self, msg):
|
def _raw(self, msg):
|
||||||
""" Print any command sent in raw format """
|
""" Print any command sent in raw format """
|
||||||
self.device.write(msg)
|
self.device.write(msg)
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
""" Close Serial interface """
|
""" Close Serial interface """
|
||||||
if self.device is not None:
|
if self.device is not None:
|
||||||
self.device.close()
|
self.device.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Network(Escpos):
|
class Network(Escpos):
|
||||||
""" Define Network printer """
|
""" Define Network printer """
|
||||||
|
|
||||||
def __init__(self,host,port=9100):
|
def __init__(self,host,port=9100, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@param host : Printer's hostname or IP address
|
@param host : Printer's hostname or IP address
|
||||||
@param port : Port to write to
|
@param port : Port to write to
|
||||||
"""
|
"""
|
||||||
|
Escpos.__init__(self, *args, **kwargs)
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
""" Open TCP socket and set it as escpos device """
|
""" Open TCP socket and set it as escpos device """
|
||||||
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.device.connect((self.host, self.port))
|
self.device.connect((self.host, self.port))
|
||||||
|
|
||||||
if self.device is None:
|
if self.device is None:
|
||||||
print "Could not open socket for %s" % self.host
|
print("Could not open socket for %s" % self.host)
|
||||||
|
|
||||||
|
|
||||||
def _raw(self, msg):
|
def _raw(self, msg):
|
||||||
""" Print any command sent in raw format """
|
""" Print any command sent in raw format """
|
||||||
self.device.send(msg)
|
self.device.send(msg)
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
""" Close TCP connection """
|
""" Close TCP connection """
|
||||||
self.device.close()
|
self.device.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class File(Escpos):
|
class File(Escpos):
|
||||||
""" Define Generic file printer """
|
""" Define Generic file printer """
|
||||||
|
|
||||||
def __init__(self, devfile="/dev/usb/lp0"):
|
def __init__(self, devfile="/dev/usb/lp0", *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@param devfile : Device file under dev filesystem
|
@param devfile : Device file under dev filesystem
|
||||||
"""
|
"""
|
||||||
|
Escpos.__init__(self, *args, **kwargs)
|
||||||
self.devfile = devfile
|
self.devfile = devfile
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
""" Open system file """
|
""" Open system file """
|
||||||
self.device = open(self.devfile, "wb")
|
self.device = open(self.devfile, "wb")
|
||||||
|
|
||||||
if self.device is None:
|
if self.device is None:
|
||||||
print "Could not open the specified file %s" % self.devfile
|
print("Could not open the specified file %s" % self.devfile)
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
"""Flush printing content"""
|
||||||
|
self.device.flush()
|
||||||
|
|
||||||
def _raw(self, msg):
|
def _raw(self, msg):
|
||||||
""" Print any command sent in raw format """
|
""" Print any command sent in raw format """
|
||||||
|
if type(msg) is str:
|
||||||
|
self.device.write(msg.encode());
|
||||||
|
else:
|
||||||
self.device.write(msg);
|
self.device.write(msg);
|
||||||
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
""" Close system file """
|
""" Close system file """
|
||||||
self.device.close()
|
self.device.close()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
-e .
|
8
setup.py
8
setup.py
|
@ -23,7 +23,13 @@ setup(
|
||||||
'Operating System :: GNU/Linux',
|
'Operating System :: GNU/Linux',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
'Programming Language :: Python',
|
'Programming Language :: Python',
|
||||||
'Topic :: System :: Pheripherals',
|
'Topic :: System :: Peripherals',
|
||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||||
],
|
],
|
||||||
|
install_requires=[
|
||||||
|
'pyusb',
|
||||||
|
'Pillow>=2.0',
|
||||||
|
'qrcode>=4.0',
|
||||||
|
'pyserial',
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue