Python子进程获取数据时,Raspberry Pi处LCD上每行末尾的字符错误

2024-04-26 13:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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上的每一行末尾都会出现一个错误的字符。在

^{pr2}$

此图显示了结果。在

Wrong character at the end of each line when Python subprocess is used

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

#!/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()

此图显示了结果。没有错别字。在

No wrong character at the end of each line

我使用的软件是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

我认为这个问题是由子进程引起的。 我能做些什么来解决这个问题?在

谢谢你的帮助!在


Tags: cmdfalsetrueoutputdatagpioiflcd
2条回答

James Kent和{a2}解决了这个问题。每行的末尾都有新行。string.rstrip()删除了换行符。在

当混淆字符串内容时,请始终使用^{} function(或使用python3时,^{})检查字符串。这将产生字符串的字符串表示形式,可以粘贴回解释器中,而不会出现编码问题,并将任何不可打印或特殊控制字符显示为转义序列。在

在这种情况下,最有可能的罪魁祸首是换行符,\n,当它表示为转义序列时。在

相关问题 更多 >