Wrong character at the end of each line on LCD at Raspberry Pi when data is fetched by Python subprocess -
hi connected hd44780 compatible lcd raspberry pi model b. wiring follows:
lcd raspberry pi model b/b+ 1 : gnd 6 : gnd 2 : 5v 2 : 5v 3 : contrast (0-5v) 6 : gnd 4 : rs (register select) 26 (gpio7) 5 : r/w (read write) 6 : gnd 6 : enable 24 (gpio8) 7 : data bit 0 --- not used in 4 bit mode --- 8 : data bit 1 --- not used in 4 bit mode --- 9 : data bit 2 --- not used in 4 bit mode --- 10: data bit 3 --- not used in 4 bit mode --- 11: data bit 4 22 (gpio25) 12: data bit 5 18 (gpio24) 13: data bit 6 16 (gpio23) 14: data bit 7 12 (gpio28) 15: lcd backlight +5v** 2 : 5v 16: lcd backlight gnd 6 : gnd
when execute script, grabs via python subprocess host information system, on lcd wrong character @ end of each line.
#!/usr/bin/python # # hd44780 lcd test script # raspberry pi # # author : matt hawkins # site : http://www.raspberrypi-spy.co.uk # # date : 26/07/2012 #import import rpi.gpio gpio import time # define gpio lcd mapping lcd_rs = 7 lcd_e = 8 lcd_d4 = 25 lcd_d5 = 24 lcd_d6 = 23 lcd_d7 = 18 # define device constants lcd_width = 16 # maximum characters per line lcd_chr = true lcd_cmd = false lcd_line_1 = 0x80 # lcd ram address 1st line lcd_line_2 = 0xc0 # lcd ram address 2nd line # timing constants e_pulse = 0.00005 e_delay = 0.00005 def main(): # main program block gpio.setmode(gpio.bcm) # use bcm gpio numbers gpio.setup(lcd_e, gpio.out) # e gpio.setup(lcd_rs, gpio.out) # rs gpio.setup(lcd_d4, gpio.out) # db4 gpio.setup(lcd_d5, gpio.out) # db5 gpio.setup(lcd_d6, gpio.out) # db6 gpio.setup(lcd_d7, gpio.out) # db7 # initialise display lcd_init() lcd_byte(lcd_line_1, lcd_cmd) p = subprocess.popen('''hostname''', stdout=subprocess.pipe, shell=true) hostname_output = p.communicate()[0] lcd_string(hostname_output) lcd_byte(lcd_line_2, lcd_cmd) p = subprocess.popen("ip addr | grep 'state up' -a2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'", stdout=subprocess.pipe, shell=true) ip_address_output = p.communicate()[0] lcd_string(str(ip_address_output)) def lcd_init(): # initialise display lcd_byte(0x33,lcd_cmd) lcd_byte(0x32,lcd_cmd) lcd_byte(0x28,lcd_cmd) lcd_byte(0x0c,lcd_cmd) lcd_byte(0x06,lcd_cmd) lcd_byte(0x01,lcd_cmd) def lcd_string(message): # send string display message = message.ljust(lcd_width," ") in range(lcd_width): lcd_byte(ord(message[i]),lcd_chr) def lcd_byte(bits, mode): # send byte data pins # bits = data # mode = true character # false command gpio.output(lcd_rs, mode) # rs # high bits gpio.output(lcd_d4, false) gpio.output(lcd_d5, false) gpio.output(lcd_d6, false) gpio.output(lcd_d7, false) if bits&0x10==0x10: gpio.output(lcd_d4, true) if bits&0x20==0x20: gpio.output(lcd_d5, true) if bits&0x40==0x40: gpio.output(lcd_d6, true) if bits&0x80==0x80: gpio.output(lcd_d7, true) # toggle 'enable' pin time.sleep(e_delay) gpio.output(lcd_e, true) time.sleep(e_pulse) gpio.output(lcd_e, false) time.sleep(e_delay) # low bits gpio.output(lcd_d4, false) gpio.output(lcd_d5, false) gpio.output(lcd_d6, false) gpio.output(lcd_d7, false) if bits&0x01==0x01: gpio.output(lcd_d4, true) if bits&0x02==0x02: gpio.output(lcd_d5, true) if bits&0x04==0x04: gpio.output(lcd_d6, true) if bits&0x08==0x08: gpio.output(lcd_d7, true) # toggle 'enable' pin time.sleep(e_delay) gpio.output(lcd_e, true) time.sleep(e_pulse) gpio.output(lcd_e, false) time.sleep(e_delay) if __name__ == '__main__': try: main() except keyboardinterrupt: lcd_byte(lcd_line_1, lcd_cmd) lcd_string("") lcd_byte(lcd_line_2, lcd_cmd) lcd_string("") gpio.cleanup()
this image shows result.
when not use subprocess , insert strings, not wrong character end of each line. example proves:
#!/usr/bin/python # # hd44780 lcd test script # raspberry pi # # author : matt hawkins # site : http://www.raspberrypi-spy.co.uk # # date : 26/07/2012 #import import rpi.gpio gpio import time import subprocess # execute unix commands subprocess # define gpio lcd mapping lcd_rs = 7 lcd_e = 8 lcd_d4 = 25 lcd_d5 = 24 lcd_d6 = 23 lcd_d7 = 18 # define device constants lcd_width = 16 # maximum characters per line lcd_chr = true lcd_cmd = false lcd_line_1 = 0x80 # lcd ram address 1st line lcd_line_2 = 0xc0 # lcd ram address 2nd line # timing constants e_pulse = 0.00005 e_delay = 0.00005 def main(): # main program block gpio.setmode(gpio.bcm) # use bcm gpio numbers gpio.setup(lcd_e, gpio.out) # e gpio.setup(lcd_rs, gpio.out) # rs gpio.setup(lcd_d4, gpio.out) # db4 gpio.setup(lcd_d5, gpio.out) # db5 gpio.setup(lcd_d6, gpio.out) # db6 gpio.setup(lcd_d7, gpio.out) # db7 # initialise display lcd_init() lcd_byte(lcd_line_1, lcd_cmd) lcd_string("1st test line") lcd_byte(lcd_line_2, lcd_cmd) lcd_string('''2nd line here''') def lcd_init(): # initialise display lcd_byte(0x33,lcd_cmd) lcd_byte(0x32,lcd_cmd) lcd_byte(0x28,lcd_cmd) lcd_byte(0x0c,lcd_cmd) lcd_byte(0x06,lcd_cmd) lcd_byte(0x01,lcd_cmd) def lcd_string(message): # send string display message = message.ljust(lcd_width," ") in range(lcd_width): lcd_byte(ord(message[i]),lcd_chr) def lcd_byte(bits, mode): # send byte data pins # bits = data # mode = true character # false command gpio.output(lcd_rs, mode) # rs # high bits gpio.output(lcd_d4, false) gpio.output(lcd_d5, false) gpio.output(lcd_d6, false) gpio.output(lcd_d7, false) if bits&0x10==0x10: gpio.output(lcd_d4, true) if bits&0x20==0x20: gpio.output(lcd_d5, true) if bits&0x40==0x40: gpio.output(lcd_d6, true) if bits&0x80==0x80: gpio.output(lcd_d7, true) # toggle 'enable' pin time.sleep(e_delay) gpio.output(lcd_e, true) time.sleep(e_pulse) gpio.output(lcd_e, false) time.sleep(e_delay) # low bits gpio.output(lcd_d4, false) gpio.output(lcd_d5, false) gpio.output(lcd_d6, false) gpio.output(lcd_d7, false) if bits&0x01==0x01: gpio.output(lcd_d4, true) if bits&0x02==0x02: gpio.output(lcd_d5, true) if bits&0x04==0x04: gpio.output(lcd_d6, true) if bits&0x08==0x08: gpio.output(lcd_d7, true) # toggle 'enable' pin time.sleep(e_delay) gpio.output(lcd_e, true) time.sleep(e_pulse) gpio.output(lcd_e, false) time.sleep(e_delay) if __name__ == '__main__': try: main() except keyboardinterrupt: lcd_byte(lcd_line_1, lcd_cmd) lcd_string("") lcd_byte(lcd_line_2, lcd_cmd) lcd_string("") gpio.cleanup()
this image shows result. there no wrong characters.
the software use raspbian os operating system.
$ uname -a linux pi71 3.12.28+ #709 preempt mon sep 8 15:28:00 bst 2014 armv6l gnu/linux $ python -v python 2.7.3 $ locate subprocess | grep 2.7 /usr/lib/pypy-upstream/lib-python/2.7/subprocess.py /usr/lib/pypy-upstream/lib-python/2.7/test/subprocessdata /usr/lib/pypy-upstream/lib-python/2.7/test/subprocessdata/sigchild_ignore.py /usr/lib/pypy-upstream/lib-python/2.7/test/test_subprocess.py /usr/lib/python2.7/subprocess.py /usr/lib/python2.7/subprocess.pyc
i think issue caused subprocess. can fix this?
thanks help!
when confused string contents, inspect string using repr()
function (or when using python 3, ascii()
). this'll produce string representation of string can pasted interpreter without encoding headaches , show non-printable or special control character escape sequence instead.
in case, culprit newline character, \n
when represented escape sequence.
Comments
Post a Comment