基本上,我一直在尝试编写一个程序,当您按特定的按钮时,会显示特定的图像。例如,如果我按下Button1,将显示Image1。接下来,如果我按下Button2,Image1将消失并且Image2将显示。我遇到的问题是关于button2的。最终,我希望它可以打开许多图像文件。现在,我已经使用python将串行数据从arduino发送到树莓派。 python程序识别出button1被按下并显示图像。但是,当我按button2时(在按button1之前或之后),程序停止并且出现错误消息。 解决该问题的任何帮助都将非常有用!
我曾尝试更改从Arduino发送的字节,但无济于事。我是串行数据处理的新手,所以可能会错过一些东西。
Arduino代码
int First=2;
int buttonState=0;
int Second=3;
int buttonState2=0;
void setup()
{
Serial.begin(9600);
pinMode(First, INPUT);
pinMode(Second, INPUT);
}
void loop()
{
int buttonState=digitalRead(First);
int buttonState2=digitalRead(Second);
if(buttonState==HIGH)
{
Serial.print(0x0);
delay(100);
}
if (buttonState==LOW)
{
//do nothing
}
//BUTTON STATE SECOND
if(buttonState2==HIGH)
{
Serial.print(0x1);
delay(100);
}
if (buttonState2==LOW)
{
//do nothing
}
}
Python代码(由于问题的发布方式,两行的格式已关闭):
import numpy
import cv2, glob
import sys
import os
import serial
from subprocess import Popen
ser=serial.Serial('/dev/ttyUSB0',9600)
Title = cv2.imread('/home/pi/Desktop/Avengers/Title.jpg')
Zero = cv2.imread('/home/pi/Desktop/Avengers/000.jpg')
while True:
command = ser.read()
if command:
#flush serial for unprocessed data
ser.flushInput()
print("new command:", command)
if (int(command) == 0):
cv2.namedWindow("Title", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Title",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow('Title', Title)
cv2.waitKey(0)
cv2.destroyAllWindows()
if (int(command) == 1):
cv2.namedWindow("Zero", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Zero",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow('Zero', Zero)
cv2.waitKey(0)
cv2.destroyAllWindows()
我希望按下一个按钮时显示一个图像,而按下另一个按钮时显示另一个图像。代码的结果允许一个按钮显示图像。但是,当按下另一个按钮时,没有图像显示。简而言之,一个按钮有效,而另一个则无效。错误消息显示为:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 490, in read
'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/OpenJPG.py", line 14, in <module>
command = ser.read()
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 497, in read
raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)