[英]pySerial Program Stops Working Correctly
我一直在搞弄我的Arduino,并正在创建一个程序来在LCD屏幕上显示推文。 不幸的是,我编写的用于检索和发送推文的python程序似乎在一分钟或更短的时间后就停止工作。 然后它将停止将任何输出打印到控制台,并且每次更新时仅在Arduino上显示一个字符。
我假设这是与串行相关的代码的问题,因为如果我删除与Twitter相关的所有内容,它将遇到相同的问题。 如果我从Arduino IDE中的串行监视器发送输入,则Arduino代码可以正常工作,因此我认为我在这方面也很出色。
以下是我的代码,任何帮助将不胜感激。
import serial
import tweepy
import time
ser = serial.Serial("COM3", 9600)
auth = tweepy.OAuthHandler("authstuff")
auth.set_access_token("accessstuff")
api = tweepy.API(auth)
def find_tweet():
public_tweets = api.user_timeline("duderitsover")
tweet = public_tweets[0].user.screen_name + ": " + public_tweets[0].text
print tweet
ser.write(tweet.encode('ascii'))
while True:
print "in loop"
find_tweet()
time.sleep(4)
还有Arduino代码,以防万一(很糟,很抱歉)
#include <LiquidCrystal.h>
#include <String.h>
LiquidCrystal lcd(2, 3, 4, 8, 9, 10, 11);
String long_string = "Long sting long string longer string long string this string is very long";
String data;
String temp_data;
String read_input;
unsigned long current_time;
unsigned long led_delay = 0;
bool has_updated = false;
int dial_position;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
}
void loop() {
current_time = millis();
if (led_delay < current_time) {
digitalWrite(5, LOW);
has_updated = false;
}
Serial.println(analogRead(A0));
dial_position = analogRead(A0) / 10;
if (read_input != "" && read_input != long_string) {
long_string = read_input;
has_updated = true;
}
if (dial_position < 10) {
temp_data = long_string.substring(0, 33);
}
else if (dial_position < 20) {
temp_data = long_string.substring(33, 65);
}
else if (dial_position < 30) {
temp_data = long_string.substring(65, 97);
}
else if (dial_position < 50) {
temp_data = long_string.substring(65 + 32, 97 + 32);
}
else if (dial_position < 60) {
temp_data = long_string.substring(65 + 64 + 32, 97 + 64 + 32);
}
else if (dial_position < 70) {
temp_data = long_string.substring(65 + 64 + 64, 97 + 64 + 64);
}
else if (dial_position < 80) {
temp_data = long_string.substring(65 + 128 + 32, 97 + 128 + 32);
}
else if (dial_position < 90) {
temp_data = long_string.substring(65 + 128 + 64, 97 + 128 + 64);
}
else if (dial_position < 100) {
temp_data = long_string.substring(65 + 128 + 96, 97 + 128 + 96);
}
if (temp_data != data && temp_data != "") {
data = temp_data;
if (temp_data.length() > 16) {
lcd.clear();
lcd.print(data.substring(0, 16));
lcd.setCursor(0, 1);
lcd.print(data.substring(16, 33));
}
else {
lcd.clear();
lcd.print(data);
}
}
}
void serialEvent(){
read_input = Serial.readString();
Serial.println("UPDATING");
digitalWrite(5, HIGH);
led_delay = current_time + 2000;
}
我的循环每次运行几次,使用Serial.reset_input_buffer()命令解决了该问题,不知道它是否功能正确,是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.