好吧,我在兩臺計算機之間安裝了UDP套接字設置。一臺電腦從遊戲杆獲取座標,並通過套接字將其作爲數組發送。在另一端接收數組,然後將數值發送給伺服器。問題是,這是否可以工作10秒左右,然後整個連接在接收端崩潰(在這種情況下是一個RPI),我必須重新啓動它。如果任何人都可以找到一個會膨脹的解決方案。下面的代碼。當使用python socket時,以太網連接崩潰
import socket
import time
import pygame
try:
import cPickle as pickle
except:
import pickle
from pygame.locals import *
pygame.init()
#Initiate Some Variables
IP = "x.x.x.x"
Port = "5000"
crashed = False
connectionID = "123456789"
clock = pygame.time.Clock()
ready = True
#Initalize Joystick
pygame.init()
pygame.joystick.init() # main joystick device system
try:
j = pygame.joystick.Joystick(0) # create a joystick instance
j.init() # init instance
print ('Enabled joystick: ' + j.get_name())
except pygame.error:
print ('no joystick found.')
m = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
if ready:
pycontrol = pygame.event.get()
#Get Joystick Pos
x = j.get_axis(0)
y = j.get_axis(1)
s = j.get_axis(2)
z = j.get_axis(3)
#Set Buttons To 0
JoyButton_0 = 0
JoyButton_1 = 0
JoyButton_2 = 0
JoyButton_3 = 0
JoyButton_4 = 0
JoyButton_5 = 0
JoyButton_6 = 0
#Lets do some math
#Create cubic graph y=x^3
#And a linear
cubic_x = (x**3)*100
cubic_y = (y**3)*100
linear_s = ((-0.5*s+1)-0.5)*100
linear_z = (z**3)*100
data_x = round(cubic_x, 0)
data_y = round(cubic_y, 0)
data_s = round(linear_s, 0)
data_z = round(linear_z, 0)
#Get Joystick Key Events
for event in pycontrol:
if event.type == pygame.QUIT:
crashed = True
############################
if event.type == pygame.JOYBUTTONDOWN:
if 1 == j.get_button(0):
JoyButton_0 = 1
if 1 == j.get_button(1):
JoyButton_1 = 1
if 1 == j.get_button(2):
JoyButton_2 = 1
######################
#Turn into array and steralize it
array = (data_x, data_y, data_s, data_z, JoyButton_0, JoyButton_1, JoyButton_2)
print (array)
#Pickle Array and set Protocol to 2 to be read by RPI
send_array = pickle.dumps(array, protocol=2)
m.sendto(send_array, (IP,5000))
time.sleep(0.05)
現在是爲發送端,這裏是接收端
#!/usr/bin/python
from Adafruit_PWM_Servo_Driver import PWM
import time
import socket
try:
import cPickle as pickle
except:
import pickle
# Setup Variables & Socket
IP = '192.168.0.122'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((IP, 5000))
print ("Ready")
servoMin = 150 # Min pulse length out of 4096
servoMax = 600 # Max pulse length out of 4096
# Initialise the PWM device using the default address
pwm = PWM(0x40)
# Note if you'd like more debug output you can instead run:
#pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60) # Set the Frequency to 60hz
while True:
raw_message,data = s.recvfrom(1024)
(data_x, data_y, data_s, data_z, JoyButton_0, JoyButton_1, JoyButton_2) = pickle.loads(raw_message)
if JoyButton_0 == 1:
print("heelo")
if JoyButton_1 == 1:
print("Potato")
if JoyButton_2 == 1:
print("Dog")
Aileron_Servo = (2.25*data_x)+375
Elevator_Servo = (2.25*data_y)+375
Throttle_Servo = (4.5*data_s)+150
Rudder_Servo = (2.25*data_z)+375
if 150 <= Aileron_Servo <= 600:
Servo0 = Alieron_Servo
pwm.setPWM(0, 0, Servo0)
time.sleep(1)
接收方可能會收到來自發送方的數據。嘗試限制您正在發送的數據或丟棄一些數據包。我們需要更多關於您遇到的錯誤的信息。系統日誌可能會有所幫助。 –
好吧,看看我能否在一分鐘內抓住系統日誌。但是,直到我試圖讓伺服系統移動時,連接似乎保持開放狀態,所以它有可能成爲伺服驅動器的問題。但我不知道如何會導致連接崩潰。而在崩潰的連接我的意思是IP地址和一切都消失了,不會回來,直到重新啓動。只是爲了清除它。 – ferret249