From f6f1a4278fd6832816b6992fe18f18d2b1d38878 Mon Sep 17 00:00:00 2001 From: Manuel F Martinez Date: Tue, 20 May 2014 22:15:54 -0700 Subject: [PATCH] Fixed tabs and added charcode tables --- CHANGELOG | 6 +++- escpos/constants.py | 43 ++++++++++++++++++------ escpos/escpos.py | 79 ++++++++++++++++++++++++++++++++++++-------- escpos/exceptions.py | 22 ++++++++++++ escpos/printer.py | 4 +-- 5 files changed, 127 insertions(+), 27 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index db4756d..6bc3eb3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,10 @@ CHANGELOG - Issue #5: Fixed vertical tab - Issue #9: Fixed identation inconsistence -* 2013-02-23 - Version 1.0.3 +* 2014-02-23 - Version 1.0.3 - Issue #18: Added quad-area characters (Sent by syncman1x@gmail.com) - Added exception for PIL import + +* 2014-05-20 - Version 1.0.4 +- Added charcode tables +- Fixed Horizontal Tab diff --git a/escpos/constants.py b/escpos/constants.py index b5fca55..ea83a6e 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -1,18 +1,19 @@ """ ESC/POS Commands (Constants) """ # Feed control sequences -CTL_LF = '\x0a' # Print and line feed -CTL_FF = '\x0c' # Form feed -CTL_CR = '\x0d' # Carriage return -CTL_HT = '\x09' # Horizontal tab -CTL_VT = '\x0b' # Vertical tab +CTL_LF = '\x0a' # Print and line feed +CTL_FF = '\x0c' # Form feed +CTL_CR = '\x0d' # Carriage return +CTL_HT = '\x09' # Horizontal tab +CTL_SET_HT = '\x1b\x44' # Set horizontal tab positions +CTL_VT = '\x1b\x64\x04' # Vertical tab # Printer hardware -HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes -HW_SELECT = '\x1b\x3d\x01' # Printer select -HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware +HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes +HW_SELECT = '\x1b\x3d\x01' # Printer select +HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware # Cash Drawer -CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 [] -CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] +CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 [] +CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] # Paper PAPER_FULL_CUT = '\x1d\x56\x00' # Full cut paper PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper @@ -20,7 +21,6 @@ PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper TXT_NORMAL = '\x1b\x21\x00' # Normal text TXT_2HEIGHT = '\x1b\x21\x10' # Double height text TXT_2WIDTH = '\x1b\x21\x20' # Double width text -TXT_4SQUARE = '\x1b\x21\x30' # Quad area text TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON @@ -31,6 +31,27 @@ TXT_FONT_B = '\x1b\x4d\x01' # Font type B TXT_ALIGN_LT = '\x1b\x61\x00' # Left justification TXT_ALIGN_CT = '\x1b\x61\x01' # Centering TXT_ALIGN_RT = '\x1b\x61\x02' # Right justification +# Char code table +CHARCODE_PC437 = '\x1b\x74\x00' # USA: Standard Europe +CHARCODE_JIS = '\x1b\x74\x01' # Japanese Katakana +CHARCODE_PC850 = '\x1b\x74\x02' # Multilingual +CHARCODE_PC860 = '\x1b\x74\x03' # Portuguese +CHARCODE_PC863 = '\x1b\x74\x04' # Canadian-French +CHARCODE_PC865 = '\x1b\x74\x05' # Nordic +CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana +CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji +CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji +CHARCODE_PC1252 = '\x1b\x74\x11' # Western European Windows Code Set +CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2 +CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2 +CHARCODE_PC858 = '\x1b\x74\x14' # Euro +CHARCODE_THAI42 = '\x1b\x74\x15' # Thai character code 42 +CHARCODE_THAI11 = '\x1b\x74\x16' # Thai character code 11 +CHARCODE_THAI13 = '\x1b\x74\x17' # Thai character code 13 +CHARCODE_THAI14 = '\x1b\x74\x18' # Thai character code 14 +CHARCODE_THAI16 = '\x1b\x74\x19' # Thai character code 16 +CHARCODE_THAI17 = '\x1b\x74\x1a' # Thai character code 17 +CHARCODE_THAI18 = '\x1b\x74\x1b' # Thai character code 18 # Barcode format BARCODE_TXT_OFF = '\x1d\x48\x00' # HRI barcode chars OFF BARCODE_TXT_ABV = '\x1d\x48\x01' # HRI barcode chars above diff --git a/escpos/escpos.py b/escpos/escpos.py index 5665603..e4f9784 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -1,10 +1,10 @@ #!/usr/bin/python -''' +""" @author: Manuel F Martinez @organization: Bashlinux @copyright: Copyright (c) 2012 Bashlinux @license: GPL -''' +""" try: import Image @@ -123,6 +123,53 @@ class Escpos: self._convert_image(im) + def charcode(self,code): + """ Set Character Code Table """ + if code.upper() == "USA": + self._raw(CHARCODE_PC437) + elif code.upper() == "JIS": + self._raw(CHARCODE_JIS) + elif code.upper() == "MULTILINGUAL": + self._raw(CHARCODE_PC850) + elif code.upper() == "PORTUGUESE": + self._raw(CHARCODE_PC860) + elif code.upper() == "CA_FRENCH": + self._raw(CHARCODE_PC863) + elif code.upper() == "NORDIC": + self._raw(CHARCODE_PC865) + elif code.upper() == "WEST_EUROPE": + self._raw(CHARCODE_WEU) + elif code.upper() == "GREEK": + self._raw(CHARCODE_GREEK) + elif code.upper() == "HEBREW": + self._raw(CHARCODE_HEBREW) + elif code.upper() == "LATVIAN": + self._raw(CHARCODE_PC755) + elif code.upper() == "WPC1252": + self._raw(CHARCODE_PC1252) + elif code.upper() == "CIRILLIC2": + self._raw(CHARCODE_PC866) + elif code.upper() == "LATIN2": + self._raw(CHARCODE_PC852) + elif code.upper() == "EURO": + self._raw(CHARCODE_PC858) + elif code.upper() == "THAI42": + self._raw(CHARCODE_THAI42) + elif code.upper() == "THAI11": + self._raw(CHARCODE_THAI11) + elif code.upper() == "THAI13": + self._raw(CHARCODE_THAI13) + elif code.upper() == "THAI14": + self._raw(CHARCODE_THAI14) + elif code.upper() == "THAI16": + self._raw(CHARCODE_THAI16) + elif code.upper() == "THAI17": + self._raw(CHARCODE_THAI17) + elif code.upper() == "THAI18": + self._raw(CHARCODE_THAI18) + else: + raise CharCode_error() + def barcode(self, code, bc, width, height, pos, font): """ Print Barcode """ # Align Bar Code() @@ -186,17 +233,17 @@ class Escpos: def set(self, align='left', font='a', type='normal', width=1, height=1): """ Set text properties """ # Width - if height != 2 and width != 2: # DEFAULT SIZE: NORMAL - self._raw(TXT_NORMAL) - - if height == 2: - self._raw(TXT_2HEIGHT) - if width == 2: - self._raw(TXT_2WIDTH) - if height == 2 and width == 2: - self._raw(TXT_4SQUARE) - + self._raw(TXT_2WIDTH) + self._raw(TXT_2HEIGHT) + elif height == 2 and width != 2: + self._raw(TXT_NORMAL) + self._raw(TXT_2HEIGHT) + elif width == 2 and height != 2: + self._raw(TXT_NORMAL) + self._raw(TXT_2WIDTH) + else: # DEFAULT SIZE: NORMAL + self._raw(TXT_NORMAL) # Type if type.upper() == "B": self._raw(TXT_BOLD_ON) @@ -264,8 +311,14 @@ class Escpos: pass - def control(self, ctl): + def control(self, ctl, pos=4): """ Feed control sequences """ + # Set tab positions + if pos < 1 or pos > 16: + raise TabError() + else: + self._raw("".join([CTL_SET_HT,hex(pos)])) + # Set position if ctl.upper() == "LF": self._raw(CTL_LF) elif ctl.upper() == "FF": diff --git a/escpos/exceptions.py b/escpos/exceptions.py index adbe648..0d482aa 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -22,6 +22,8 @@ class Error(Exception): # 40 = Image height is too large # 50 = No string supplied to be printed # 60 = Invalid pin to send Cash Drawer pulse +# 70 = Invalid number of tab positions +# 80 = Invalid char code class BarcodeTypeError(Error): @@ -78,3 +80,23 @@ class CashDrawerError(Error): def __str__(self): return "Valid pin must be set to send pulse" + + +class TabError(Error): + def __init__(self, msg=""): + Error.__init__(self, msg) + self.msg = msg + self.resultcode = 70 + + def __str__(self): + return "Valid tab positions must be in the range 0 to 16" + + +class CharCodeError(Error): + def __init__(self, msg=""): + Error.__init__(self, msg) + self.msg = msg + self.resultcode = 70 + + def __str__(self): + return "Valid char code must be set" diff --git a/escpos/printer.py b/escpos/printer.py index 7cdedf3..2bdafca 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -1,10 +1,10 @@ #!/usr/bin/python -''' +""" @author: Manuel F Martinez @organization: Bashlinux @copyright: Copyright (c) 2012 Bashlinux @license: GPL -''' +""" import usb.core import usb.util