Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/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 SerialTimeoutException脚本崩溃_Python_Arduino_Pyserial - Fatal编程技术网

Python SerialTimeoutException脚本崩溃

Python SerialTimeoutException脚本崩溃,python,arduino,pyserial,Python,Arduino,Pyserial,此脚本在较长时间后由于串行超时异常而崩溃。在出现以下错误之前,它已达到5000个循环左右: Traceback (most recent call last): File "C:\Users\3d Exposure\Desktop\venderFix.py", line 69, in <module> A.setHigh(12) File "C:\Python27\lib\arduino\arduino.py", line 30, in setHigh sel

此脚本在较长时间后由于串行超时异常而崩溃。在出现以下错误之前,它已达到5000个循环左右:

Traceback (most recent call last):
  File "C:\Users\3d Exposure\Desktop\venderFix.py", line 69, in <module>
    A.setHigh(12)
  File "C:\Python27\lib\arduino\arduino.py", line 30, in setHigh
    self.__sendData('1')
  File "C:\Python27\lib\arduino\arduino.py", line 58, in __sendData
    self.serial.write(str(serial_data))
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 270, in write
    raise writeTimeoutError
SerialTimeoutException: Write timeout
为了与Arduino通信,我使用Python Arduino原型API v2。它工作得很好,这是第一个也是唯一的问题,在系列我有这个脚本。
我想我需要自己设置一个超时,尽管我不能100%确定如何确保这不会有相同或类似的问题。

我在这里看不到波特率或数据率。但这将是,您正在填充串行缓冲区,然后新的写入失败。或者您正在尝试编写,而另一个编写正在进行。@Serdalis我如何阻止这种情况发生?您的示例中没有足够的信息让我说。但通常你需要看看你的波特率是否能处理你试图通过的数据量。如果不能,您需要等待缓冲区变空后再重新写入。@Serdalis这是在我使用的不太流行的hastag搜索中,每半小时发送一条消息,将pin13发送到HIGH。有时它会24小时不使用。发送连续数小时的串行数据不会影响这一点吗?我怎样才能学会检查缓冲区是否为空?嗯,我误解了你的例子。缓冲区在这里不起作用。当您调用高/低功能时,检查引脚的状态,可能您正在将其设置为一个已经存在的值。还要检查连接,当连接断开时(特别是长时间暂停后),也会发生写入超时。你可能睡得不够久,把它设为1,看看会发生什么。对不起,我不能给出任何结论。
import json
import urllib
from pprint import pprint
import time
from arduino import Arduino

##############################################################
#Change to suit the Mars Bars, currently at 0.2 of a second  #
vendtime = 0.2                                               #
                                                             #
#Delay Time Between each Search (never below 15 seconds)     #
delayTime = 15                                               #
#This is the search term for the URL. (%23 = #)              # 
searchTerm = '%23happy'                                      #
                                                             #
A = Arduino('COM3')                                          #
A.output([13]) #Output on pin 12                             #
##############################################################


#to collect the first tweet without vending
countTweet = 0
#To test Twitter for consistancy 
tweet= 0
noTweet= 0



#the infinate loop
while True:

    #j contains the JSON we load from the URL
    j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q='+searchTerm+'&result_type=recent&rpp=1&filter:retweets').read())

    #Debug JSON from twitter (for faults on the Twitter end or possible GET limit id below 15 seconds per request)
    #pprint(j) #needed for debugging only

    #find the text and the tweet id
    if 'results' in j and j['results']:
        text = j['results'][0]['text']
        id = j['results'][0]['id']
        #how many times the Json is complete
        tweet+= 1
    else:
        #How many times the Json is incomplete (sometimes twitter malfunctions. About 0.1 in 100 are broken)
        noTweet += 1

    #print the text and id to the screen
    pprint(text) #needed for debugging only
    pprint(id)   #needed for debugging only

    #to get the existing tweet from before we power on, if the first ID has been stored already (count == 1)
    if countTweet != 0:  #if countTweet is not equal to 0 then it's not the first tweet
        #pprint ("new loop") #needed for debugging only

        #if lastID is not equal to ID
        if lastID != id:
        #Tell Arduino to Vend
            #pin 12 HIGH
            A.setHigh(13)
            #Sleep for the time specified in vendtime
            time.sleep(vendtime)
            #pin 12 LOW
            A.setLow(13)
            #Display the tweet that triggered the vend
            #pprint(text) #needed for debugging only
            #pprint(id)   #needed for debugging only
            #Make lastID equal to ID so that next time we can compare it 
            lastID = id
            #pprint ('lastID updated') #needed for debugging only
        #if no new tweets, print     
        else:  #needed for debugging only
            pprint ('no new tweets') #needed for debugging only
    #If it's the first loop, confirm by printing to the screen
    else:
        pprint("First loop complete")
        pprint(text)
        pprint(id)
        lastID = id
        pprint(lastID)
        countTweet += 1 #Add 1 to countTweet

    pprint ('Number of Tweets')
    pprint (countTweet)
    pprint('Working JSON')
    pprint(tweet)
    pprint('Broken JSON')
    pprint(noTweet)

    pprint('waiting')
    time.sleep(delayTime)