2013-08-20 88 views
0

我一直在我的「控制器」中爲我的樹莓派機器人使用字符串變量,這取決於用戶箭頭鍵輸入以及它們何時更改通過套接字服務器發送信息。這個程序在我的筆記本電腦上運行良好,並且一個基本的服務器可以打印接收到的信息在我的Raspberry Pi上運行我現在正在使用這些信息來控制電機等。我的問題是由於某種原因,我無法得到這個工作。字符串變量不能在樹莓派上工作

import SocketServer 
import Tkinter as Tk 
from Tkinter import * 

class MyTCPHandler (SocketServer.BaseRequestHandler): 
     def handle(self): 
       self.DriveSend = self.request.recv(1024).strip() 
       self.SteeringSend = self.request.recv(1024).strip() 
       print("{} wrote:".format(self.client_address[0])) 
       print(self.DriveSend) 
       print(self.SteeringSend) 
       main.DriveVar.set(self.DriveSend) 
       main.SteeringVar.set(self.SteeringSend) 
       self.request.sendall(self.DriveSend.upper()) 
       self.request.sendall(self.SteeringSend.upper()) 
if __name__ == "__main__": 
     HOST, PORT = "192.168.2.12", 9999 
     server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) 
     server.serve_forever() 
     DriveVar = StringVar() 
     SteeringVar = StringVar() 

下面是產生的異常/錯誤。

Exception happened during processing of request from ('192.168.2.10', 52447) 
Traceback (most recent call last): 
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock 
self.process_request(request, client_address) 
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request 
self.finish_request(request, client_address) 
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request 
self.RequestHandlerClass(request, client_address, self) 
File "/usr/lib/python2.7/SocketServer.py", line 638, in __init__ 
self.handle() 
File "server.py", line 12, in handle 
main.DriveVar.set(self.DriveSend) 
NameError: global name 'main' is not defined 

爲什麼這不起作用? 如何解決它還是我應該怎麼用,而不是(在銘記變量在由主

+0

你的'main'定義在哪裏?如果它是一個模塊名稱,你應該導入它。如果'Main'是一個模塊中定義的類,那麼你必須在它前面加上模塊名稱。區分大小寫。 – pepr

回答

0

稱爲類中的一個函數接收我最終設法得到這個工作,但沒有字符串變量。

import SocketServer 
import Tkinter as Tk 
from Tkinter import * 

class MyTCPHandler (SocketServer.BaseRequestHandler): 
    def handle(self): 
      self.DriveSend = self.request.recv(1024).strip() 
      self.SteeringSend = self.request.recv(1024).strip() 
      print("{} wrote:".format(self.client_address[0])) 
      #print(self.DriveSend) 
      #print(self.SteeringSend) 
      self.request.sendall(self.DriveSend.upper()) 
      self.request.sendall(self.SteeringSend.upper()) 
      motorcontrol.self 
      return(self.DriveSend,self.SteeringSend) 

def motorcontrol(motor_var) 
    motor = motor_var.DriveSend 
    steering = motor_var.SteeringSend 
if __name__ == "__main__": 
    HOST, PORT = "192.168.2.12", 9999 
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) 
    server.serve_forever()