escpos module fixed for python 3?
This commit is contained in:
parent
916de838e0
commit
b78f8c9807
|
@ -1,54 +1,56 @@
|
||||||
""" ESC/POS Commands (Constants) """
|
""" ESC/POS Commands (Constants) """
|
||||||
|
|
||||||
|
from escpos.utils import hex2bytes
|
||||||
|
|
||||||
# Feed control sequences
|
# Feed control sequences
|
||||||
CTL_LF = b'\x0a' # Print and line feed
|
CTL_LF = hex2bytes('0a') # Print and line feed
|
||||||
CTL_FF = b'\x0c' # Form feed
|
CTL_FF = hex2bytes('0c') # Form feed
|
||||||
CTL_CR = b'\x0d' # Carriage return
|
CTL_CR = hex2bytes('0d') # Carriage return
|
||||||
CTL_HT = b'\x09' # Horizontal tab
|
CTL_HT = hex2bytes('09') # Horizontal tab
|
||||||
CTL_VT = b'\x0b' # Vertical tab
|
CTL_VT = hex2bytes('0b') # Vertical tab
|
||||||
# Printer hardware
|
# Printer hardware
|
||||||
HW_INIT = b'\x1b\x40' # Clear data in buffer and reset modes
|
HW_INIT = hex2bytes('1b40') # Clear data in buffer and reset modes
|
||||||
HW_SELECT = b'\x1b\x3d\x01' # Printer select
|
HW_SELECT = hex2bytes('1b3d01') # Printer select
|
||||||
HW_RESET = b'\x1b\x3f\x0a\x00' # Reset printer hardware
|
HW_RESET = hex2bytes('1b3f0a00') # Reset printer hardware
|
||||||
# Cash Drawer
|
# Cash Drawer
|
||||||
CD_KICK_2 = b'\x1b\x70\x00' # Sends a pulse to pin 2 []
|
CD_KICK_2 = hex2bytes('1b7000') # Sends a pulse to pin 2 []
|
||||||
CD_KICK_5 = b'\x1b\x70\x01' # Sends a pulse to pin 5 []
|
CD_KICK_5 = hex2bytes('1b7001') # Sends a pulse to pin 5 []
|
||||||
# Paper
|
# Paper
|
||||||
PAPER_FULL_CUT = b'\x1d\x56\x00' # Full cut paper
|
PAPER_FULL_CUT = hex2bytes('1d5600') # Full cut paper
|
||||||
PAPER_PART_CUT = b'\x1d\x56\x01' # Partial cut paper
|
PAPER_PART_CUT = hex2bytes('1d5601') # Partial cut paper
|
||||||
# Text format
|
# Text format
|
||||||
TXT_NORMAL = b'\x1b\x21\x00' # Normal text
|
TXT_NORMAL = hex2bytes('1b2100') # Normal text
|
||||||
TXT_2HEIGHT = b'\x1b\x21\x10' # Double height text
|
TXT_2HEIGHT = hex2bytes('1b2110') # Double height text
|
||||||
TXT_2WIDTH = b'\x1b\x21\x20' # Double width text
|
TXT_2WIDTH = hex2bytes('1b2120') # Double width text
|
||||||
TXT_4SQUARE = b'\x1b\x21\x30' # Quad area text
|
TXT_4SQUARE = hex2bytes('1b2130') # Quad area text
|
||||||
TXT_UNDERL_OFF = b'\x1b\x2d\x00' # Underline font OFF
|
TXT_UNDERL_OFF = hex2bytes('1b2d00') # Underline font OFF
|
||||||
TXT_UNDERL_ON = b'\x1b\x2d\x01' # Underline font 1-dot ON
|
TXT_UNDERL_ON = hex2bytes('1b2d01') # Underline font 1-dot ON
|
||||||
TXT_UNDERL2_ON = b'\x1b\x2d\x02' # Underline font 2-dot ON
|
TXT_UNDERL2_ON = hex2bytes('1b2d02') # Underline font 2-dot ON
|
||||||
TXT_BOLD_OFF = b'\x1b\x45\x00' # Bold font OFF
|
TXT_BOLD_OFF = hex2bytes('1b4500') # Bold font OFF
|
||||||
TXT_BOLD_ON = b'\x1b\x45\x01' # Bold font ON
|
TXT_BOLD_ON = hex2bytes('1b4501') # Bold font ON
|
||||||
TXT_FONT_A = b'\x1b\x4d\x00' # Font type A
|
TXT_FONT_A = hex2bytes('1b4d00') # Font type A
|
||||||
TXT_FONT_B = b'\x1b\x4d\x01' # Font type B
|
TXT_FONT_B = hex2bytes('1b4d01') # Font type B
|
||||||
TXT_ALIGN_LT = b'\x1b\x61\x00' # Left justification
|
TXT_ALIGN_LT = hex2bytes('1b6100') # Left justification
|
||||||
TXT_ALIGN_CT = b'\x1b\x61\x01' # Centering
|
TXT_ALIGN_CT = hex2bytes('1b6101') # Centering
|
||||||
TXT_ALIGN_RT = b'\x1b\x61\x02' # Right justification
|
TXT_ALIGN_RT = hex2bytes('1b6102') # Right justification
|
||||||
# Barcode format
|
# Barcode format
|
||||||
BARCODE_TXT_OFF = b'\x1d\x48\x00' # HRI barcode chars OFF
|
BARCODE_TXT_OFF = hex2bytes('1d4800') # HRI barcode chars OFF
|
||||||
BARCODE_TXT_ABV = b'\x1d\x48\x01' # HRI barcode chars above
|
BARCODE_TXT_ABV = hex2bytes('1d4801') # HRI barcode chars above
|
||||||
BARCODE_TXT_BLW = b'\x1d\x48\x02' # HRI barcode chars below
|
BARCODE_TXT_BLW = hex2bytes('1d4802') # HRI barcode chars below
|
||||||
BARCODE_TXT_BTH = b'\x1d\x48\x03' # HRI barcode chars both above and below
|
BARCODE_TXT_BTH = hex2bytes('1d4803') # HRI barcode chars both above and below
|
||||||
BARCODE_FONT_A = b'\x1d\x66\x00' # Font type A for HRI barcode chars
|
BARCODE_FONT_A = hex2bytes('1d6600') # Font type A for HRI barcode chars
|
||||||
BARCODE_FONT_B = b'\x1d\x66\x01' # Font type B for HRI barcode chars
|
BARCODE_FONT_B = hex2bytes('1d6601') # Font type B for HRI barcode chars
|
||||||
BARCODE_HEIGHT = b'\x1d\x68\x64' # Barcode Height [1-255]
|
BARCODE_HEIGHT = hex2bytes('1d6864') # Barcode Height [1-255]
|
||||||
BARCODE_WIDTH = b'\x1d\x77\x03' # Barcode Width [2-6]
|
BARCODE_WIDTH = hex2bytes('1d7703') # Barcode Width [2-6]
|
||||||
BARCODE_UPC_A = b'\x1d\x6b\x00' # Barcode type UPC-A
|
BARCODE_UPC_A = hex2bytes('1d6b00') # Barcode type UPC-A
|
||||||
BARCODE_UPC_E = b'\x1d\x6b\x01' # Barcode type UPC-E
|
BARCODE_UPC_E = hex2bytes('1d6b01') # Barcode type UPC-E
|
||||||
BARCODE_EAN13 = b'\x1d\x6b\x02' # Barcode type EAN13
|
BARCODE_EAN13 = hex2bytes('1d6b02') # Barcode type EAN13
|
||||||
BARCODE_EAN8 = b'\x1d\x6b\x03' # Barcode type EAN8
|
BARCODE_EAN8 = hex2bytes('1d6b03') # Barcode type EAN8
|
||||||
BARCODE_CODE39 = b'\x1d\x6b\x04' # Barcode type CODE39
|
BARCODE_CODE39 = hex2bytes('1d6b04') # Barcode type CODE39
|
||||||
BARCODE_ITF = b'\x1d\x6b\x05' # Barcode type ITF
|
BARCODE_ITF = hex2bytes('1d6b05') # Barcode type ITF
|
||||||
BARCODE_NW7 = b'\x1d\x6b\x06' # Barcode type NW7
|
BARCODE_NW7 = hex2bytes('1d6b06') # Barcode type NW7
|
||||||
# Image format
|
# Image format
|
||||||
S_RASTER_N = b'\x1d\x76\x30\x00' # Set raster image normal size
|
S_RASTER_N = hex2bytes('1d763000') # Set raster image normal size
|
||||||
S_RASTER_2W = b'\x1d\x76\x30\x01' # Set raster image double width
|
S_RASTER_2W = hex2bytes('1d763001') # Set raster image double width
|
||||||
S_RASTER_2H = b'\x1d\x76\x30\x02' # Set raster image double height
|
S_RASTER_2H = hex2bytes('1d763002') # Set raster image double height
|
||||||
S_RASTER_Q = b'\x1d\x76\x30\x03' # Set raster image quadruple
|
S_RASTER_Q = hex2bytes('1d763003') # Set raster image quadruple
|
||||||
|
|
|
@ -12,10 +12,11 @@ import qrcode
|
||||||
import operator
|
import operator
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from constants import *
|
from escpos.utils import *
|
||||||
from exceptions import *
|
from escpos.constants import *
|
||||||
|
from escpos.exceptions import *
|
||||||
|
|
||||||
class Escpos:
|
class Escpos(object):
|
||||||
""" ESC/POS Printer object """
|
""" ESC/POS Printer object """
|
||||||
device = None
|
device = None
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ class Escpos:
|
||||||
|
|
||||||
self._raw(S_RASTER_N)
|
self._raw(S_RASTER_N)
|
||||||
buffer = "%02X%02X%02X%02X" % (((size[0]//size[1])//8), 0, size[1], 0)
|
buffer = "%02X%02X%02X%02X" % (((size[0]//size[1])//8), 0, size[1], 0)
|
||||||
self._raw(buffer.decode('hex'))
|
self._raw(hex2bytes(buffer))
|
||||||
buffer = ""
|
buffer = ""
|
||||||
|
|
||||||
while i < len(line):
|
while i < len(line):
|
||||||
|
@ -49,7 +50,7 @@ class Escpos:
|
||||||
i += 8
|
i += 8
|
||||||
cont += 1
|
cont += 1
|
||||||
if cont % 4 == 0:
|
if cont % 4 == 0:
|
||||||
self._raw(buffer.decode("hex"))
|
self._raw(hex2bytes(buffer))
|
||||||
buffer = ""
|
buffer = ""
|
||||||
cont = 0
|
cont = 0
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@ import usb.util
|
||||||
import serial
|
import serial
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from escpos import *
|
from escpos.escpos import *
|
||||||
from constants import *
|
from escpos.constants import *
|
||||||
from exceptions import *
|
from escpos.exceptions import *
|
||||||
|
|
||||||
class Usb(Escpos):
|
class Usb(Escpos):
|
||||||
""" Define USB printer """
|
""" Define USB printer """
|
||||||
|
@ -44,13 +44,13 @@ class Usb(Escpos):
|
||||||
try:
|
try:
|
||||||
self.device.detach_kernel_driver(0)
|
self.device.detach_kernel_driver(0)
|
||||||
except usb.core.USBError as e:
|
except usb.core.USBError as e:
|
||||||
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):
|
||||||
|
@ -88,9 +88,9 @@ class Serial(Escpos):
|
||||||
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)
|
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:
|
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):
|
||||||
|
@ -124,7 +124,7 @@ class Network(Escpos):
|
||||||
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):
|
||||||
|
@ -150,10 +150,10 @@ class File(Escpos):
|
||||||
|
|
||||||
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 _raw(self, msg):
|
def _raw(self, msg):
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
try:
|
||||||
|
bytes.fromhex
|
||||||
|
def hex2bytes(hex_string):
|
||||||
|
return bytes.fromhex(hex_string)
|
||||||
|
|
||||||
|
except:
|
||||||
|
def hex2bytes(hex_string):
|
||||||
|
return hex_string.decode('hex')
|
||||||
|
|
Loading…
Reference in New Issue