From 54e717696e5dfe4e56b27e230636859fa0147e42 Mon Sep 17 00:00:00 2001 From: Dennis Jung Date: Sun, 23 Oct 2016 09:00:09 +0200 Subject: [PATCH 1/4] Python3 and Raspberry Pi 3 Adaptation - Change default terminal to ttyS0 for Raspberry Pi 3 - Bug fix of write function to estimate the correct count of characters - Use of ord() to determine the correct byte code for a character - Encode character to convert it to a byte array for terminal output --- Adafruit_Thermal.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index f5e5162..f057121 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -32,6 +32,12 @@ # - Trap errors properly. Some stuff just falls through right now. # - Add docstrings throughout! +#=============================================================================== +# Modification by Dennis Jung +# +# - Make module usable for Python3 and Raspberry Pi 3 +#=============================================================================== + # Python 2.X code using the library usu. needs to include the next line: from __future__ import print_function from serial import Serial @@ -58,7 +64,7 @@ def __init__(self, *args, **kwargs): # If both passed, use those values. baudrate = 19200 if len(args) == 0: - args = [ "/dev/ttyAMA0", baudrate ] + args = [ "/dev/ttyS0", baudrate ] elif len(args) == 1: args = [ args[0], baudrate ] else: @@ -167,19 +173,19 @@ def setTimes(self, p, f): # 'Raw' byte-writing method def writeBytes(self, *args): - self.timeoutWait() - self.timeoutSet(len(args) * self.byteTime) for arg in args: - super(Adafruit_Thermal, self).write(chr(arg)) + self.timeoutWait() + self.timeoutSet(self.byteTime) + super(Adafruit_Thermal, self).write(bytes([arg])) # Override write() method to keep track of paper feed. def write(self, *data): - for i in range(len(data)): - c = data[i] - if c != 0x13: + for i in range(len(data[0])): + c = data[0][i] + if ord(c) != 0x13: self.timeoutWait() - super(Adafruit_Thermal, self).write(c) + super(Adafruit_Thermal, self).write(c.encode('cp437','ignore')) d = self.byteTime if ((c == '\n') or (self.column == self.maxColumn)): From ddac7a6725ca9088dcfc6d89bf138bc3cbc9de16 Mon Sep 17 00:00:00 2001 From: Dennis Jung Date: Sun, 23 Oct 2016 13:47:33 +0200 Subject: [PATCH 2/4] Remove modification information --- Adafruit_Thermal.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index f057121..c107313 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -32,12 +32,6 @@ # - Trap errors properly. Some stuff just falls through right now. # - Add docstrings throughout! -#=============================================================================== -# Modification by Dennis Jung -# -# - Make module usable for Python3 and Raspberry Pi 3 -#=============================================================================== - # Python 2.X code using the library usu. needs to include the next line: from __future__ import print_function from serial import Serial From 48354f0333fb3cd9d5e110a5ab2e255619fc939f Mon Sep 17 00:00:00 2001 From: Dennis Jung Date: Sun, 23 Oct 2016 13:53:22 +0200 Subject: [PATCH 3/4] Change terminal to ttyS0 for Raspberry Pi 3 --- printertest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printertest.py b/printertest.py index 8a79279..b81db29 100755 --- a/printertest.py +++ b/printertest.py @@ -2,7 +2,7 @@ from Adafruit_Thermal import * -printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) +printer = Adafruit_Thermal("/dev/ttyS0", 19200, timeout=5) # Test inverse on & off printer.inverseOn() From 373efe44d7b699f35fa26de581c2a420c83f073c Mon Sep 17 00:00:00 2001 From: Dennis Jung Date: Sun, 23 Oct 2016 13:54:20 +0200 Subject: [PATCH 4/4] Apply changes to make code compatible for Python3 Verification: printertest.py produces correct output - PASSED --- Adafruit_Thermal.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index c107313..9e5ca0a 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -36,6 +36,7 @@ from __future__ import print_function from serial import Serial import time +import math class Adafruit_Thermal(Serial): @@ -267,7 +268,7 @@ def printBarcode(self, text, type): # Print string self.timeoutWait() self.timeoutSet((self.barcodeHeight + 40) * self.dotPrintTime) - super(Adafruit_Thermal, self).write(text) + super(Adafruit_Thermal, self).write(text.encode('utf-8', 'ignore')) self.prevByte = '\n' self.feed(2) @@ -417,7 +418,7 @@ def underlineOff(self): def printBitmap(self, w, h, bitmap, LaaT=False): - rowBytes = (w + 7) / 8 # Round up to next byte boundary + rowBytes = math.floor((w + 7) / 8) # Round up to next byte boundary if rowBytes >= 48: rowBytesClipped = 48 # 384 pixels max width else: @@ -444,7 +445,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False): for y in range(chunkHeight): for x in range(rowBytesClipped): super(Adafruit_Thermal, self).write( - chr(bitmap[i])) + bytes([bitmap[i]])) i += 1 i += rowBytes - rowBytesClipped self.timeoutSet(chunkHeight * self.dotPrintTime)