So this was my original problem: I tried printing PIL barcode images using CPCL commands over bluetooth. The printer is a Rongta RPP320 bluetooth printer which support CPCL & ESC/POS. I chose CPCL because I have another printer that only runs CPCL and to cut developing time I use CPCL, for the present at least. For now I am just testing a basic "Hello World" command. So after reading the HP and Zebra CPCL pdf booklets I think I don't really understand everything but enough to have a try.
So I scrounged the bluetooth script from this very helpful site.
Here the adapted code:
import bluetooth
serverMACAddress = '00:00:12:03:00:12'
port = 3
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((serverMACAddress, port))
while 1:
a = b'''!0 200 200 210 1
TEXT 4 0 30 40 Hello World
FORM
OUTPUT'''
byt = bytearray(a)
for b in byt:
s.send(hex(b))
sock.close()
After this script is run I listen for any feedback from the printer using the server script in above link. Any connection problems via bluetooth I can sort out.
What I want to know is should I send my CPCL text as a hexadecimal bytes or should I encode it with Utf-8 or Latin 1 or something else? Because the printer is not talking to me at the moment.
E.g.
a = '''!0 200 200 210 1
TEXT 4 0 30 40 Hello World
FORM
OUTPUT'''
enc = a.encode('Latin 1')
Answer: Use Utf-8 and send all commands as ASCII decimals that is encoded as bytearray
.
EDIT to solve original problem:
So I have, after many hours, come the conclusion that PyBluez will not work on Android and moved away from trying to print barcode PIL images with CPCL. I have solved the bluetooth connection with Android problems I had in another thread, but I am not finished trying PyBluez on other OS'. I switched the printer to accept ESC/POS commands and used the following to print barcodes (this also works on a UROVO K319 printer):
gs = 29
esc = 27
bcode = str(1001)
print('Building barcode command')
#extend bytearray
#initialise printer
init_cmd = [esc,64]
wrt_send = bytearray(init_cmd)
#set bcode string placement below barcode
hri_cmd = [gs,72,2]
wrt_send.extend(bytearray(hri_cmd))
hrif = [gs,102,1]
wrt_send.extend(bytearray(hrif))
#set bcode height
hbcode_cmd = [gs,104,80]
wrt_send.extend(bytearray(hbcode_cmd))
#set bcode width
wbcode_cmd = [gs,119,6]
wrt_send.extend(bytearray(wbcode_cmd))
#print bcode very important is len of bcode for Code128 and linefeed dec 10 at end
prnt_bcode_cmd_pre = [gs,107,73,len(bcode)]
post = bytearray([10])
bar = bcode.encode('utf-8')
prnt_bcode = bytearray(prnt_bcode_cmd_pre)
prnt_bcode.extend(bar)
prnt_bcode.extend(post)
wrt_send.extend(prnt_bcode)
self.send_stream.write(wrt_send)
self.send_stream.flush()
The reason I did not use escpos python library is because I could not get it to work with bluetooth in Android environment. I know this did not adhere to what I was trying in my original question, but it did solve my problem about printing barcodes on my printer via bluetooth on Android.
Original question answer: Use Utf-8 encoding and send all commands as ASCII decimals that is encoded as bytearray
. If you send Strings, encode them separately and extend the bytearray
.