ESC/POS Printer Python code

ESC/POS Printer Python code

[php]

#!/usr/bin/python
# -*- coding: utf-8 -*-
import serial
import time
#ser = serial.Serial ("/dev/ttyAMA0") #Open named port

def chineseOn(self):
self.writeBytes(27, 57, 1) #ESC 9 1

def chineseOff(self):
self.writeBytes(27, 116, 0) #ESC t 0

try:
ser = serial.Serial() #"/dev/cu.usbserial",19200, timeout=0.5) #Open named port
ser.port = "/dev/cu.usbserial"
#ser.port = "/dev/ttyS2"
ser.baudrate = 19200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
ser.timeout = 1 #non-block read
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
ser.close()
exit()
#dev/cu.usbserial 115200
#ser.baudrate = 19200 #Set baud rate to 9600
#ser.write('5')
##### initPrinter --Start

#ser.write(serial.to_bytes([27,64]))

#ser.write(serial.to_bytes([27,55,7]))
#ser.write(serial.to_bytes([7]))
#ser.write(serial.to_bytes([80,2]))
#ser.write(serial.to_bytes([800,20]))
#ser.write(serial.to_bytes([18,35]))
#ser.write(serial.to_bytes([15*2*2*2*2]))
##### initPrinter --End
#ser.write(serial.to_bytes([10]));
#ser.write(serial.to_bytes([10]));
#ser.write("www.powenko.comA");
#ser.write(serial.to_bytes([10]));
#ser.write("www.powenko.comB");
#ser.write(serial.to_bytes([10]));
#ser.write(serial.to_bytes([27,45,49])); # underline - Select underlining , ESC-1 (27,45,49) --ok
#ser.write("www.powenko.comC");
#ser.write(serial.to_bytes([10]));
#ser.write(serial.to_bytes([27,45,2])); # underline - two pixels --ok
#ser.write("www.powenko.com 4");
#ser.write(serial.to_bytes([10]));
#ser.write(serial.to_bytes([27,45,0])); # - Select underlining , ESC-0 (27,45,48) --ok
#ser.write("www.powenko.com 5");
#ser.write(serial.to_bytes([10]));
#time.sleep(1)
#ser.write(serial.to_bytes([27,32,1])); # #### Bold text ####
#ser.write("www.powenko.com 6");
#ser.write(serial.to_bytes([10]));
#time.sleep(1)
#ser.write(serial.to_bytes([27,45,0])); # bold text off
#ser.write("www.powenko.com 7");
#ser.write(serial.to_bytes([10]));
#time.sleep(1)
# ser.write(serial.to_bytes([29,33,255])); # height/width enlarge
# ser.write("abcdef");
# ser.write(serial.to_bytes([10]));
# time.sleep(1)
# ser.write(serial.to_bytes([29,33,0])); # back to normal
# ser.write(serial.to_bytes([29,107,0])); # back to normal
# ser.write("01234567");
# time.sleep(1)
#ser.write(serial.to_bytes([10,10,10]));
#ser.write(serial.to_bytes([27,100,5])); #cut paper
# ser.write(serial.to_bytes([29,86,0])); #cut paper
# ser.write(serial.to_bytes([10]));
# ser.write(serial.to_bytes([27,100,5])); #cut paper
# ser.write(serial.to_bytes([10]));
# time.sleep(1)
##########
#ser.write(serial.to_bytes([27])+"m"); # cut paper --ok
#ser.write(serial.to_bytes([27])+"q"); # release paper --ok
#ser.write("aaaa");
#ser.write(serial.to_bytes([27,33,16])); # double size --ok
#ser.write("bbbb");
ser.write(serial.to_bytes([27,116,0])); # chinese off
ser.write(serial.to_bytes([27,57,0])); # chinese
ser.write(u"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,1])); # chinese
ser.write(u"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,2])); # chinese
ser.write(u"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,0])); # chinese
ser.write(ur"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,1])); # chinese
ser.write(ur"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,2])); # chinese
ser.write(ur"好".encode('utf-8'));
ser.write(serial.to_bytes([27,57,0])); # chinese
ser.write(u"好".encode('gbk'));
ser.write(serial.to_bytes([27,57,1])); # chinese
ser.write(u"好".encode('gbk'));
ser.write(serial.to_bytes([27,57,2])); # chinese
ser.write(u"好".encode('gbk'));
ser.write(serial.to_bytes([27,57,0])); # chinese
ser.write(serial.to_bytes([0x53,0x62])); # chinese
ser.write(serial.to_bytes([0x62,0x53])); # chinese
#ser.write(u"好".encode('big-5'));
#ser.write(u"好".decode('utf-8'));
#ser.write(ur"好世界");
ser.write(serial.to_bytes([10]));
#ser.write(serial.to_bytes([10]));
#time.sleep(1)
#a = u"你好世界" # "Hello World"
#ser.write(a)
#ser.println(a)
#time.sleep(1)
#ser.close()
except serial.serialutil.SerialException:
print 'cannot open Serial Port'

[/php]

POS