Sorry I made you wait! I was able to print the thermal printer "PAPERANG" from Python.
Download from here
$ conda create -n paperang python=3.6
$ conda activate paperang
$ cd miaomiaoji-tool
$ pip install PyBluez-win10
$ pip install opencv-python
$ python message_process.py
I am changing the forked one from here.
When I try to run message_process.py, I get an error in the filter function. Enclose the filter functions on lines 47 and 64 in a list. How to use map / filter in Python3
valid_devices = list(filter(lambda d: len(d) == 2 and d[1] in valid_names, nearby_devices))
・ ・ ・
valid_service = list(filter(
lambda s: 'protocol' in s and 'name' in s and s['protocol'] == 'RFCOMM' and s['name'] == 'SerialPort',
service_matches
))
With pybluez, OSError appears at find_service on the 63rd line, so PyBluez-win10 is used. * OS Error when program try to use functions from bluetooth._msbt # 279
service_matches = find_service(uuid=self.uuid, address=self.address)
I get an error that the service name cannot be found, so let's print and display service_matches.
[{'host': '00:15:83:B7:11:AF', 'name': b'SerialPort', 'description': '', 'port': 1, 'protocol': 'RFCOMM', 'rawrecord': b'5J\t\x00\x00\n\x00\x01\x00\x0f\t\x00\x015\x03\x19\x11\x01\t\x00\x045\x0c5\x03\x19\x01\x005\x05\x19\x00\x03\x08\x01\t\x00\x055\x03\x19\x10\x02\t\x00\x08\x08\xff\t\x00\t5\x085\x06\x19\x11\x01\t\x01\x02\t\x01\x00%\nSerialPort', 'service-classes': [b'1101'], 'profiles': [(b'1101', 258)], 'provider': None, 'service-id': None, 'handle': 65551}, {'host': '00:15:83:B7:11:AF', 'name': b'WeChat', 'description': '', 'port': 8, 'protocol': 'RFCOMM', 'rawrecord': b'5T\t\x00\x00\n\x00\x01\x00\x0e\t\x00\x015\x11\x1c\xe5\xb1R\xedkF\t\xe9Fxf^\x9a\x97,\xbc\t\x00\x045\x0c5\x03\x19\x01\x005\x05\x19\x00\x03\x08\x08\t\x00\x055\x03\x19\x10\x02\t\x00\x08\x08\xff\t\x00\t5\x085\x06\x19\x11\x01\t\x01\x02\t\x01\x00%\x06WeChat', 'service-classes': ['E5B152ED-6B46-09E9-4678-665E9A972CBC'], 'profiles': [(b'1101', 258)], 'provider': None, 'service-id': None, 'handle': 65550}]
'name': b Since it is'SerialPort', add b to'SerialPort' on line 65.
lambda s: 'protocol' in s and 'name' in s and s['protocol'] == 'RFCOMM' and s['name'] == b'SerialPort',
When I print service_matches, I get the following struct.pack error.
File "message_process.py", line 88, in packPerBytes
result += struct.pack('<i', self.crc32(bytes))
struct.error: argument out of range
Let's change <i on line 88 to <I. struct — Interpret bytes as packed binary data
result += struct.pack('<I', self.crc32(bytes))
Then I get an encode error, so
File "message_process.py", line 110, in recv
logging.info("Recv: " + raw_msg.encode('hex'))
AttributeError: 'bytes' object has no attribute 'encode'
Change .encode ('hex') on lines 110 and 122 to .hex (). [How do I fix AttributeError:'bytes' object has no attribute'encode'?](Https://stackoverflow.com/questions/55701623/how-do-i-fix-attributeerror-bytes-object-has-no- attribute-encode)
logging.info("Recv: " + raw_msg.hex())
・ ・ ・
, self.payload_length, self.payload.hex()
Since the error of the map function appears in image_process.py, enclose the map function on the 32nd line in list.
File "\miaomiaoji-tool\image_process.py", line 20, in frombits
for b in range(len(bits) / 8):
TypeError: object of type 'map' has no len()
Also, since the range range of the for statement on the 20th line is float, cast it to int.
for b in range(int(len(bits) / 8)):
I get an error in struct.pack on line 153, so comment it out and it's OK.
msg = struct.pack("<%dc" % len(binary_img, *binary_img)
To print a 300-line line, it must be binary. The contents of sendImageToBt are msg with the binary string as it is.
def sendImageToBt(self, binary_img):
self.sendPaperTypeToBt()
# msg = struct.pack("<%dc" % len(binary_img, *binary_img)
msg = binary_img
self.sendToBt(msg, BtCommandByte.PRT_PRINT_DATA, need_reply=False)
self.sendFeedLineToBt(self.padding_line)
Print a pure black image with 300 lines
img = b'\xff' * 48 * 300
mmj.sendImageToBt(img)
I was able to print.
In fact, you need to send to self.sendToBt for each line.
def sendImageToBt(self, binary_img):
self.sendPaperTypeToBt()
# msg = struct.pack("<%dc" % len(binary_img, *binary_img)
msgs = [binary_img[x: x+192] for x in range(0, len(binary_img), 192)] # 4*48
for msg in msgs:
self.sendToBt(msg, BtCommandByte.PRT_PRINT_DATA, need_reply=False)
self.sendFeedLineToBt(self.padding_line)
This is the true 300 line line. I changed the function name to sendBinaryToBt.
\ xff is black and \ x00 is white. It is easy to understand if \ xff is converted to a binary number, but \ xff becomes 0b11111111 and eight black vertical lines are drawn. Therefore, 8 bits represent 8 pixels of the image. The width of the horizontal line is up to 48'\ xff'. That is, the image width is 8 x 48 = 384 pixels.
The image is resized, binarized, converted to a value of [0,1], converted to hexadecimal by 8 pixels, and printed line by line.
def sendImageToBt(self, binary_img):
self.sendPaperTypeToBt()
height, width = binary_img.shape[:]
for line in range(height):
bits = [0 if x > 0 else 1 for x in binary_img[line]]
bits = [bits[x:x+8] for x in range(0, len(bits), 8)]
msg = ''
for bit in bits:
bin = '0b'+''.join(str(x) for x in bit)
msg += '{:02x}'.format(int(bin, 0))
msg = bytes.fromhex(msg)
self.sendToBt(msg, BtCommandByte.PRT_PRINT_DATA, need_reply=False)
self.sendFeedLineToBt(self.padding_line)
・ ・ ・
Print an existing image(need opencv):
img = cv2.imread('kumamcn.png', 0)
ret, binary_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
height, width = binary_img.shape[:]
binary_img = cv2.resize(binary_img, (384, int(height*384.0/width)), cv2.INTER_AREA)
mmj.sendImageToBt(binary_img)
done. Now you can print any image!
If this article is useful, please buy raw oysters in the book.
Recommended Posts