簡體   English   中英

使用python套接字時以太網連接崩潰

[英]Ethernet connection crashes when using python socket

好的,我在兩台計算機之間設置了UDP套接字。 一台計算機從操縱桿獲取坐標,並將其作為數組發送到套接字上。 在另一端,接收數組,然后將該值發送到伺服器。 問題是,這可以正常工作約10秒鍾,然后整個連接在接收端崩潰(在這種情況下為RPI),我必須重新啟動它。 如果有人能找到解決方案,那將是無可避免的。 下面的代碼。

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)

我看到你睡着了,但他們不匹配。 發件人處於睡眠狀態0.05秒,而接收方處於睡眠狀態1秒。 這可能會導致緩沖區溢出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM