" NameError:全球名称' ser'没有定义" python twitter

时间:2017-05-24 07:11:20

标签: python pyserial

我在Python中遇到以下问题,但我不明白为什么:

"Traceback (most recent call last):   File
C:/Users/W7-32/Downloads/TW31.py", line 82, in <module>
    driptwit()  ## Llamada a la funcion driptwit   File "C:/Users/W7-32/Downloads/TW31.py", line 71, in driptwit
    ser.write("1") NameError: global name 'ser' is not defined

源代码:

 # lIBRERIAS

 import twitter
 import tweepy
 import serial
 import time
 import threading
 import os
 import datetime

 ##Consumer y Access de Twitter Developers from tweepy import API
 consumer_key = 'QpaLpmQOS0DMTt2hFxy5WN40i'
 consumer_secret = 'WoUssPI9gIdiEIu0LSWG2bvGuAlvF8WTDStdFTkj5OIPwbeLth'
 access_token_key = '935906436-aMBTKoPOZshZhCRRil1vNCTUKPxQBSiuXe181hLp'
 access_token_secret = 'pxYs26cMqxQbhxQtgfIGnUjWGP1eLlK9mVxdaLeorPfZ4'

 #Autenticacion y conexion con Twitter
 #auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
 ##auth.set_access_token(access_token_key, access_token_secret)

 #api = tweepy.api(consumer_key, consumer_secret, access_token_key, >     access_token_secret)
 #api=tweepy.API(auth)
 ##Declaracion del puerto serial

 auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
 auth.set_access_token(access_token_key, access_token_secret)

 api=tweepy.API(auth)


 locations = ['COM3','COM4','COM5','COM6']

 for device in locations:

     try:

         ser = serial.Serial(device, baudrate=9600, bytesize=8,

         parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)

         print ("\nArduino found on: " + ser.portstr + " please >     wait...\n")
         time.sleep(2)
         break
     except:
                 print "Failed to connect on", device

 ## check serial port
 def checkokay():
     ser.flushInput()
     time.sleep(3)
     line = ser.readline()
     time.sleep(3)
     if line == ' ':
         line = ser.readline()
     print("here")


 print('Bienvenido a Print_Twitter')


 def driptwit():
     status = []
     x = 0
     status = api.user_timeline('FerCMejia')  ##grab latest statuses
     checkit = [s.text for s in status]  ##put status in an array
     drip = checkit[0].split()  ##split first tweet into words

     ## check for match and write to serial if match
     if (drip[0] == '#dripPrint'):
         print("Recibiendo Twitter, Imprimiendo")
         ser.write("1")
     else:
         if drip[0] == '#dripPrintStop':  ##break if done
             ser.write("0")
             print("Impresion detenida, esperando instruccion")
         else:
                 print("Esperando Tweet")
                 ser.write("0")


 while 1:
         driptwit()  ## Llamada a la funcion driptwit
         time.sleep(20)  ## Espera de 20 seg para volver a leer el status

虽然端口连接了Arduino,但它给我一个未定义的错误。

这是.py档案:

Archive Python

我使用Pycharm和Python2.7解释器,Windows7 32位。

1 个答案:

答案 0 :(得分:0)

_.inRange(3, 2, 4); // => true 不是全局变量。您可以在for循环中定义它,因此它的范围仅限于该循环。在for循环之上执行此操作:

ser

这使得它的范围不仅限于for循环。 for循环现在将使用在循环中调用它时刚刚定义的变量。 然后,在功能正文中,您需要在顶部添加ser = None for device in locations: 关键字,以确保当您致电global时,它会让全局变量不尝试创建一个新的。像这样:

ser

在另一个功能中相同:

def checkokay():
     global ser
     ser.flushInput()

现在你的变量是全局的,你的函数正在使用变量的全局版本。