Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 3e7be0b

Browse files
authored
Merge pull request #36 from galacticfan/master
Python3 compatibility
2 parents 4ede517 + fc39336 commit 3e7be0b

2 files changed

Lines changed: 71 additions & 26 deletions

File tree

Adafruit_Thermal.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
# - Trap errors properly. Some stuff just falls through right now.
3333
# - Add docstrings throughout!
3434

35-
# Python 2.X code using the library usu. needs to include the next line:
36-
from __future__ import print_function
3735
from serial import Serial
3836
import time
3937
import sys
38+
import math
4039

4140
class Adafruit_Thermal(Serial):
4241

@@ -181,12 +180,12 @@ def setTimes(self, p, f):
181180
def writeBytes(self, *args):
182181
if self.writeToStdout:
183182
for arg in args:
184-
sys.stdout.write(chr(arg))
183+
sys.stdout.write(bytes([arg]))
185184
else:
186-
self.timeoutWait()
187-
self.timeoutSet(len(args) * self.byteTime)
188185
for arg in args:
189-
super(Adafruit_Thermal, self).write(chr(arg))
186+
self.timeoutWait()
187+
self.timeoutSet(len(args) * self.byteTime)
188+
super(Adafruit_Thermal, self).write(bytes([arg]))
190189

191190
# Override write() method to keep track of paper feed.
192191
def write(self, *data):
@@ -262,7 +261,7 @@ def setDefault(self):
262261
self.setCodePage()
263262

264263
def test(self):
265-
self.write("Hello world!")
264+
self.write("Hello world!".encode('cp437', 'ignore'))
266265
self.feed(2)
267266

268267
def testPage(self):
@@ -341,20 +340,20 @@ def printBarcode(self, text, type):
341340
n = len(text)
342341
if n > 255: n = 255
343342
if self.writeToStdout:
344-
sys.stdout.write(chr(n))
343+
sys.stdout.write((chr(n)).encode('cp437', 'ignore'))
345344
for i in range(n):
346-
sys.stdout.write(text[i])
345+
sys.stdout.write(text[i].encode('utf-8', 'ignore'))
347346
else:
348-
super(Adafruit_Thermal, self).write(chr(n))
347+
super(Adafruit_Thermal, self).write((chr(n)).encode('utf-8', 'ignore'))
349348
for i in range(n):
350349
super(Adafruit_Thermal,
351-
self).write(text[i])
350+
self).write(text[i].encode('utf-8', 'ignore'))
352351
else:
353352
# Older firmware: write string + NUL
354353
if self.writeToStdout:
355-
sys.stdout.write(text)
354+
sys.stdout.write(text.encode('utf-8', 'ignore'))
356355
else:
357-
super(Adafruit_Thermal, self).write(text)
356+
super(Adafruit_Thermal, self).write(text.encode('utf-8', 'ignore'))
358357
self.prevByte = '\n'
359358

360359
# === Character commands ===
@@ -461,7 +460,7 @@ def feed(self, x=1):
461460
# datasheet claims sending bytes 27, 100, <x> works,
462461
# but it feeds much more than that. So, manually:
463462
while x > 0:
464-
self.write('\n')
463+
self.write('\n'.encode('cp437', 'ignore'))
465464
x -= 1
466465

467466
# Feeds by the specified number of individual pixel rows
@@ -504,7 +503,7 @@ def underlineOff(self):
504503
self.writeBytes(27, 45, 0)
505504

506505
def printBitmap(self, w, h, bitmap, LaaT=False):
507-
rowBytes = (w + 7) / 8 # Round up to next byte boundary
506+
rowBytes = math.floor((w + 7) / 8) # Round up to next byte boundary
508507
if rowBytes >= 48:
509508
rowBytesClipped = 48 # 384 pixels max width
510509
else:
@@ -531,11 +530,10 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
531530
for y in range(chunkHeight):
532531
for x in range(rowBytesClipped):
533532
if self.writeToStdout:
534-
sys.stdout.write(
535-
chr(bitmap[i]))
533+
sys.stdout.write(bytes([bitmap[i]]))
536534
else:
537535
super(Adafruit_Thermal,
538-
self).write(chr(bitmap[i]))
536+
self).write(bytes([bitmap[i]]))
539537
i += 1
540538
i += rowBytes - rowBytesClipped
541539
self.timeoutSet(chunkHeight * self.dotPrintTime)
@@ -549,17 +547,17 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
549547
# For any other behavior (scale, B&W threshold, etc.), use
550548
# the Imaging Library to perform such operations before
551549
# passing the result to this function.
552-
def printImage(self, image, LaaT=False):
550+
def printImage(self, image_file, LaaT=False):
553551
from PIL import Image
554-
552+
image = Image.open(image_file)
555553
if image.mode != '1':
556554
image = image.convert('1')
557555

558556
width = image.size[0]
559557
height = image.size[1]
560558
if width > 384:
561559
width = 384
562-
rowBytes = (width + 7) / 8
560+
rowBytes = math.floor((width + 7) / 8)
563561
bitmap = bytearray(rowBytes * height)
564562
pixels = image.load()
565563

@@ -726,11 +724,11 @@ def setCharSpacing(self, spacing):
726724
# with existing code written for the Arduino library.
727725
def print(self, *args, **kwargs):
728726
for arg in args:
729-
self.write(str(arg))
727+
self.write((str(arg)).encode('cp437', 'ignore'))
730728

731729
# For Arduino code compatibility again
732730
def println(self, *args, **kwargs):
733731
for arg in args:
734-
self.write(str(arg))
735-
self.write('\n')
732+
self.write((str(arg)).encode('cp437', 'ignore'))
733+
self.write('\n'.encode('cp437', 'ignore'))
736734

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
1-
Python-Thermal-Printer
2-
======================
1+
# Python-Thermal-Printer Module
2+
3+
Python3 port of the original Adafruit [Python-Thermal-Printer](https://github.com/adafruit/Python-Thermal-Printer) library.
4+
5+
## Getting Started
6+
7+
Install Raspbian Buster and Wire the printer according to [this](https://learn.adafruit.com/networked-thermal-printer-using-cups-and-raspberry-pi/connect-and-configure-printer). I powered the printer with the GPIO pins as well.
8+
9+
Run a test to see if the printer is working by punching in these commands into the terminal.
10+
11+
```
12+
stty -F /dev/serial0 19200
13+
echo -e "This is a test.\\n\\n\\n" > /dev/serial0
14+
```
15+
16+
### Installing
17+
18+
Update the system and install prequisities.
19+
20+
```
21+
sudo apt-get update
22+
sudo apt-get install git cups wiringpi build-essential libcups2-dev libcupsimage2-dev python-serial python-pil python-unidecode
23+
```
24+
25+
Install the printer driver. Don't worry about the warnings that g++ gives.
26+
27+
```
28+
git clone https://github.com/adafruit/zj-58
29+
cd zj-58
30+
make
31+
sudo ./install
32+
```
33+
34+
Make the printer the default printer. This is useful if you are going to be doing other things with it.
35+
36+
```
37+
sudo lpadmin -p ZJ-58 -E -v serial:/dev/serial0?baud=19200 -m zjiang/ZJ-58.ppd
38+
sudo lpoptions -d ZJ-58
39+
```
40+
41+
Restart the system. Clone this repository and try to run *printertest.py*.
42+
43+
```
44+
git clone https://github.com/galacticfan/Python-Thermal-Printer/
45+
cd Python-Thermal-Printer
46+
python3 printertest.py
47+
```
48+
49+
Let me know if you have any issues.

0 commit comments

Comments
 (0)