Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子进程获取数据时,Raspberry Pi处LCD上每行末尾的字符错误_Python_Subprocess_Raspberry Pi_Raspbian_Lcd - Fatal编程技术网

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

Python子进程获取数据时,Raspberry Pi处LCD上每行末尾的字符错误,python,subprocess,raspberry-pi,raspbian,lcd,Python,Subprocess,Raspberry Pi,Raspbian,Lcd,我将HD44780兼容LCD与Raspberry Pi型号B连接。接线如下: LCD Raspberry Pi Model B/B+ 1 : GND 6 : GND 2 : 5V 2 : 5V 3 : Contrast (0

我将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()
此图显示了结果

当我不使用subprocess并且只插入任何字符串时,我不会在每行的末尾得到错误的字符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()
此图显示了结果。没有错误的字符

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

$ 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
我认为这个问题是由子流程引起的。 我能做些什么来解决这个问题


谢谢你的帮助

并解决了它。每行末尾都有一行新词
string.rstrip()
删除了换行符。

当对字符串内容感到困惑时,请始终使用检查字符串(或在使用Python 3时,)。这将生成字符串的字符串表示形式,可以将其粘贴回解释器中,而无需进行编码,并将任何不可打印或特殊的控制字符显示为转义序列


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

我怀疑发生的是,您正在从另一个进程的stdout中提取一个尾随字符,它很可能是一个换行符。您可以尝试使用string.rstrip(),这将不带任何参数删除所有空白字符。由于
Process.communitate()
只提供两个字符串的元组,您能否向我们展示
print repr(ip地址输出)
生成的内容?我怀疑你那里有
\n
字符。就是这样!你是对的。有一条新线。
print repr(ip\u address\u output)
的输出是
'10.0.0.71\n'
并且
string.rstrip()
删除了换行符。谢谢!!!