Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit 6710a0e

Browse files
committed
add bluetooth backend
1 parent 40d0634 commit 6710a0e

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ font = ImageFont.truetype("FreeSans.ttf", SIZE)
3737
Text(font, pad_right=50)
3838
```
3939

40+
To use a Bluetooth connection:
41+
1. pair your device
42+
2. specify the serial device node when instantiating the printer:
43+
44+
```
45+
printer = P700(BTSerialBackend.auto(devPath='/dev/ttyS8'))
46+
```
47+
4048
The Following printers are currently supported:
4149

4250
* Brother P-Touch PT-700 (aka P700)
@@ -50,6 +58,7 @@ but have not been tested (please tell us if they do!):
5058
The following backends are currently supported:
5159

5260
* USB Printer Device Class via PyUSB
61+
* Bluetooth Serial connection via PySerial
5362

5463
The official source of this repository is at https://git.scc.kit.edu/scc-net/labelprinterkit.
5564
Pull requests and issues are also accepted on github at https://github.com/notafile/labelprinterkit.

labelprinterkit/backends/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import usb.core
22
import usb.util
33
import threading
4+
import serial
45

56
def is_usb_printer(dev):
67
if dev.bDeviceClass == 7:
@@ -27,3 +28,30 @@ def write(self, data: bytes):
2728

2829
def read(self, count: int) -> bytes:
2930
return self.dev.read(0x81, count)
31+
32+
class BTSerialBackend():
33+
def __init__(self, dev):
34+
self.dev = dev
35+
self.lock = threading.Lock()
36+
37+
@classmethod
38+
def auto(cls, devPath: str):
39+
dev = serial.Serial(
40+
devPath,
41+
baudrate=9600,
42+
stopbits=serial.STOPBITS_ONE,
43+
parity=serial.PARITY_NONE,
44+
bytesize=8,
45+
dsrdtr=False,
46+
timeout=1
47+
)
48+
if dev is None:
49+
raise OSError('Device not found')
50+
return cls(dev)
51+
52+
def write(self, data: bytes):
53+
self.dev.write(data)
54+
55+
def read(self, count: int) -> bytes:
56+
data = self.dev.read(count)
57+
return data

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'pillow',
2525
'pyusb',
2626
'packbits',
27+
'pyserial'
2728
],
2829
setup_requires=[
2930
'setuptools_scm'

0 commit comments

Comments
 (0)