当通过Python子进程获取数据时,在Raspberry Pi上LCD的每一行末尾有错误字符

问题描述 投票:2回答:2

我将兼容HD44780的LCD与Raspberry Pi B型相连。接线如下:

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

[当我执行此脚本时,该脚本只是通过Python子进程从系统中获取了一些主机信息,因此我在LCD的每一行末尾看到一个错误的字符。

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins
# Site   : http://www.raspberrypi-spy.co.uk
#
# Date   : 26/07/2012

#import
import RPi.GPIO as GPIO
import time

# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 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 to display

  message = message.ljust(LCD_WIDTH," ")  

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for 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()

此图像显示结果。

当我不使用子进程而只是插入任何字符串时,在每一行的末尾都不会出现错误的字符。这个例子证明了这一点:

#!/usr/bin/python # # HD44780 LCD Test Script for # Raspberry Pi # # Author : Matt Hawkins # Site : http://www.raspberrypi-spy.co.uk # # Date : 26/07/2012 #import import RPi.GPIO as GPIO import time import subprocess # Execute UNIX commands as subprocess # Define GPIO to LCD mapping LCD_RS = 7 LCD_E = 8 LCD_D4 = 25 LCD_D5 = 24 LCD_D6 = 23 LCD_D7 = 18 # Define some device constants LCD_WIDTH = 16 # Maximum characters per line LCD_CHR = True LCD_CMD = False LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 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 to display message = message.ljust(LCD_WIDTH," ") for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) def lcd_byte(bits, mode): # Send byte to data pins # bits = data # mode = True for character # False for 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()

此图像显示结果。没有错误的字符。

“每行末尾没有错误字符”

我使用的软件是Raspbian操作系统。

$ 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

我认为此问题是由子过程引起的。我该怎么做才能解决此问题?

感谢您的帮助!

我已将兼容HD44780的LCD与Raspberry Pi B型连接。接线如下:LCD Raspberry Pi B / B + 1型:GND ...

python subprocess raspberry-pi raspbian lcd
2个回答
3
投票
[如果对字符串内容感到困惑,请始终使用repr() function(或使用Python 3,repr())检查字符串。这将产生字符串的字符串表示形式,可以将该字符串表示形式粘贴回您的解释器中,而不会造成麻烦,并且将显示任何不可打印或特殊的控制字符作为转义序列。

0
投票
[ascii()\n解决了它。每行末尾都有换行符。 James Kent删除了换行符。
© www.soinside.com 2019 - 2024. All rights reserved.