customer-display-test.py
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/python
# -*- encoding: utf-8 -*-
# Author : Alexis de Lattre <alexis.delattre@akretion.com>
# The licence is in the file __openerp__.py
# This is a test script, that you can use if you want to test/play
# with the customer display independantly from the Odoo server
# It has been tested with a Bixolon BCD-1100
from serial import Serial
from unidecode import unidecode
import sys
DEVICE = '/dev/ttyUSB0'
DEVICE_RATE = 9600
DEVICE_COLS = 20
def display_text(ser, line1, line2):
print "convert to ascii"
line1 = unidecode(line1)
line2 = unidecode(line2)
print "set lines to the right lenght (%s)" % DEVICE_COLS
for line in [line1, line2]:
if len(line) < DEVICE_COLS:
line += ' ' * (DEVICE_COLS - len(line))
elif len(line) > DEVICE_COLS:
line = line[0:DEVICE_COLS]
assert len(line) == DEVICE_COLS, 'Wrong length'
print "try to clear display"
ser.write('\x0C')
print "clear done"
print "try to position at start of 1st line"
ser.write('\x1B\x6C' + chr(1) + chr(1))
print "position done"
print "try to write 1st line"
ser.write(line1)
print "write 1st line done"
print "try to position at start of 2nd line"
ser.write('\x1B\x6C' + chr(1) + chr(2))
print "position done"
print "try to write 2nd line"
ser.write(line2)
print "write done"
def open_close_display(line1, line2):
ser = False
try:
print "open serial port"
ser = Serial(DEVICE, DEVICE_RATE, timeout=2)
print "serial port open =", ser.isOpen()
print "try to set cursor to off"
ser.write('\x1F\x43\x00')
print "cursor set to off"
display_text(ser, line1, line2)
except Exception, e:
print "EXCEPTION e=", e
sys.exit(1)
finally:
if ser:
print "close serial port"
ser.close()
if __name__ == '__main__':
line1 = u'POS Code Sprint'
line2 = u'@ Akretion 2014/07'
open_close_display(line1, line2)