这是我的第一个问题!(尽管使用该网站找到了我曾经遇到的大多数编程问题的答案)
我创建了一个 PiBotController,我计划在我的笔记本电脑上运行它,我想将控件(来自箭头键的输入)传递给控制我的机器人的树莓派。现在这不是硬件方面的问题,我创建了一个响应箭头键输入的程序,我可以通过 ssh 连接控制 pi 上的电机。
在线搜索我发现了以下使用 socketserver 的基本服务器和客户端代码,我可以开始工作发送一个简单的字符串。
服务器:
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
客户:
import socket
import sys
HOST, PORT = "192.168.2.12", 9999
data = "this here data wont send!! "
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(data))
print("Received: {}".format(received))
现在这工作正常并在我的Raspberry Pi(服务器)和我的笔记本电脑(客户端)上打印结果但是我多次尝试将它组合成一个功能,该功能与我的按键'和释放'一起激活,就像我在我的控制器。
PiBot控制器
#import the tkinter module for the GUI and input control
try:
# for Python2
import Tkinter as tk
from Tkinter import *
except ImportError:
# for Python3
import tkinter as tk
from tkinter import *
import socket
import sys
#variables
Drive = 'idle'
Steering = 'idle'
#setting up the functions to deal with key presses
def KeyUp(event):
Drive = 'forward'
drivelabel.set(Drive)
labeldown.grid_remove()
labelup.grid(row=2, column=2)
def KeyDown(event):
Drive = 'reverse'
drivelabel.set(Drive)
labelup.grid_remove()
labeldown.grid(row=4, column=2)
def KeyLeft(event):
Steering = 'left'
steeringlabel.set(Steering)
labelright.grid_remove()
labelleft.grid(row=3, column=1)
def KeyRight(event):
Steering = 'right'
steeringlabel.set(Steering)
labelleft.grid_remove()
labelright.grid(row=3, column=3)
def key(event):
if event.keysym == 'Escape':
root.destroy()
#setting up the functions to deal with key releases
def KeyReleaseUp(event):
Drive = 'idle'
drivelabel.set(Drive)
labelup.grid_remove()
def KeyReleaseDown(event):
Drive = 'idle'
drivelabel.set(Drive)
labeldown.grid_remove()
def KeyReleaseLeft(event):
Steering = 'idle'
steeringlabel.set(Steering)
labelleft.grid_remove()
def KeyReleaseRight(event):
Steering = 'idle'
steeringlabel.set(Steering)
labelright.grid_remove()
#connection functions
def AttemptConnection():
connectionmessagetempvar = connectionmessagevar.get()
connectionmessagevar.set(connectionmessagetempvar + "\n" + "Attempting to connect...")
def transmit(event):
HOST, PORT = "192.168.2.12", 9999
data = "this here data wont send!! "
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(data))
print("Received: {}".format(received))
#setting up GUI window
root = tk.Tk()
root.minsize(300,140)
root.maxsize(300,140)
root.title('PiBot Control Centre')
root.grid_columnconfigure(0, minsize=50)
root.grid_columnconfigure(1, minsize=35)
root.grid_columnconfigure(2, minsize=35)
root.grid_columnconfigure(3, minsize=35)
root.grid_rowconfigure(2, minsize=35)
root.grid_rowconfigure(3, minsize=35)
root.grid_rowconfigure(4, minsize=35)
root.configure(background='white')
root.option_add("*background", "white")
#set up the labels to display the current drive states
drivelabel = StringVar()
Label(root, textvariable=drivelabel).grid(row=0, column=1, columnspan=2)
steeringlabel = StringVar()
Label(root, textvariable=steeringlabel).grid(row=1, column=1, columnspan=2)
Label(root, text="Drive: ").grid(row=0, column=0, columnspan=1)
Label(root, text="Steering: ").grid(row=1, column=0, columnspan=1)
#set up the buttons and message for connecting etc..
messages=tk.Frame(root, width=150, height=100)
messages.grid(row=1,column=4, columnspan=2, rowspan=4)
connectionbutton = Button(root, text="Connect", command=AttemptConnection)
connectionbutton.grid(row=0, column=4)
connectionmessagevar = StringVar()
connectionmessage = Message(messages, textvariable=connectionmessagevar, width=100, )
connectionmessage.grid(row=1, column=1, rowspan=1, columnspan=1)
disconnectionbutton = Button(root, text="Disconnect")
disconnectionbutton.grid(row=0, column=5)
#pictures
photodown = PhotoImage(file="down.gif")
labeldown = Label(root, image=photodown)
labeldown.photodown = photodown
#labeldown.grid(row=4, column=1)
photoup = PhotoImage(file="up.gif")
labelup = Label(root, image=photoup)
labelup.photoup = photoup
#labelup.grid(row=2, column=1)
photoleft = PhotoImage(file="left.gif")
labelleft = Label(root, image=photoleft)
labelleft.photoleft = photoleft
#labelleft.grid(row=3, column=0)
photoright = PhotoImage(file="right.gif")
labelright = Label(root, image=photoright)
labelright.photoright = photoright
#labelright.grid(row=3, column=2)
photoupleft = PhotoImage(file="upleft.gif")
labelupleft = Label(root, image=photoupleft)
labelupleft.photoupleft = photoupleft
#labelupleft.grid(row=2, column=0)
photodownleft = PhotoImage(file="downleft.gif")
labeldownleft = Label(root, image=photodownleft)
labeldownleft.photodownleft = photodownleft
#labeldownleft.grid(row=4, column=0)
photoupright = PhotoImage(file="upright.gif")
labelupright = Label(root, image=photoupright)
labelupright.photoupright = photoupright
#labelupright.grid(row=2, column=2)
photodownright = PhotoImage(file="downright.gif")
labeldownright = Label(root, image=photodownright)
labeldownright.photodownright = photodownright
#labeldownright.grid(row=4, column=2)
#bind all key presses and releases to the root window
root.bind_all('<Key-Up>', KeyUp)
root.bind_all('<Key-Down>', KeyDown)
root.bind_all('<Key-Left>', KeyLeft)
root.bind_all('<Key-Right>', KeyRight)
root.bind_all('<KeyRelease-Up>', KeyReleaseUp)
root.bind_all('<KeyRelease-Down>', KeyReleaseDown)
root.bind_all('<KeyRelease-Left>', KeyReleaseLeft)
root.bind_all('<KeyRelease-Right>', KeyReleaseRight)
root.bind_all('<Key>', key)
root.bind_all('<Key>', transmit)
#set the labels to an initial state
steeringlabel.set('idle')
drivelabel.set('idle')
connectionmessagevar.set ('PiBotController Initiated')
#initiate the root window main loop
root.mainloop()
这个程序编译得很好,但没有向服务器发送任何数据?(我知道它仍然只是发送一个字符串,但我认为 id 从一些简单的东西开始......而且很明显我被卡住了,所以它可能是最好的)
任何建议使其在每次更改时仅发送字符串或发送变量驱动和转向都将不胜感激。
戴夫XX
编辑
这是我得到的传输功能,因为每当我按下/释放按键时它都会发送数据(就像我之前想要的那样),但是它只发送“空闲”变量的初始设置。现在看看代码,我想我可能应该获取主机和端口信息并从每次运行的函数中创建一个套接字连接?但我不确定所以这就是我现在所拥有的。
def transmit():
HOST, PORT = "192.168.2.12", 9999
DriveSend = drivelabel.get
SteeringSend = steeringlabel.get
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(Drive + "\n", "utf-8"))
sock.sendall(bytes(Steering + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(Steering))
print("Sent: {}".format(Drive))
print("Received: {}".format(received))