2016-01-11 44 views
0

我一直在弄乱我的Arduino,并且正在创建一个程序来在液晶显示屏上显示推文。不幸的是,我编写的用于检索和发送推文的python程序似乎在一分钟或更短时间后停止工作。然后停止将任何输出打印到控制台,并且每次更新时只在Arduino上显示一个字符。pySerial程序停止正常工作

我假设它是与串行相关的代码的问题,因为它运行到相同的问题,如果我删除所有与Twitter相关的一切。 Arduino的代码工作正常,如果我从Arduino IDE的串行监视器发送输入,所以我认为我在这方面也很好。

以下是我的代码,任何帮助将不胜感激。

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的代码(这是一个烂摊子,抱歉)使用Serial.reset_input_buffer()每隔几次命令我的循环运行,不知道

#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; 
} 

回答

0

解决问题如果它是功能正确的方法。