Python 如何从远程服务器接收UDP数据包

Python 如何从远程服务器接收UDP数据包,python,python-3.x,sockets,udp,Python,Python 3.x,Sockets,Udp,我试图从一个带有几个模拟输入和PWM输出的设备上读取一些值,以控制一个LED条。该设备对通过以太网接收的UDP数据包作出响应。我有一些文件包格式,可以发送任何颜色我喜欢 但是,当我请求模拟输入值时,我的Python3程序(在Win10或Raspberry Pi上运行)无法接收设备的回复。当我使用Wireshark时,我看到设备以预期的格式发回UDP回复数据包。但是我的应用程序中没有接收到来自这些数据包的数据,套接字上的超时刚好过期 因此,基本上,我想了解如何从远程“服务器”接收UDP数据包。(我

我试图从一个带有几个模拟输入和PWM输出的设备上读取一些值,以控制一个LED条。该设备对通过以太网接收的UDP数据包作出响应。我有一些文件包格式,可以发送任何颜色我喜欢

但是,当我请求模拟输入值时,我的Python3程序(在Win10或Raspberry Pi上运行)无法接收设备的回复。当我使用Wireshark时,我看到设备以预期的格式发回UDP回复数据包。但是我的应用程序中没有接收到来自这些数据包的数据,套接字上的超时刚好过期

因此,基本上,我想了解如何从远程“服务器”接收UDP数据包。(我希望在这种情况下,远程设备是服务器是正确的吗?) 我是Python编程新手,所以关于代码样式等的建议。。。我也很感激

请看下面我的代码

#!/usr/bin/env python3
from threading import Thread
import socket
import select
from tkinter import *
import tkinter
import tkinter.font as tkFont

DEVICE_IP = "10.10.0.1"
UDP_PORT = 11991

myOutputPkt  = bytearray([0x88, 0x77, 0x66, 0x55, 0xff, 0xff, 0x0f, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00])
myRequestPkt = bytearray([0x88, 0x77, 0x66, 0x44, 0xff, 0xff, 0x0f, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00])

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

win = tkinter.Tk()

myFont = tkFont.Font(family = 'Helvetica', size = 16, weight = 'bold')

def sendOutput(IOnumber, IOvalue):
        myOutputPkt[10] = IOnumber
        myOutputPkt[11] = IOvalue & 0xff
        myOutputPkt[12] = (IOvalue & 0xff00) >> 8
        myOutputPkt[13] = (IOvalue & 0xff0000) >> 16
        myOutputPkt[14] = (IOvalue & 0xff000000) >> 24
        sock.sendto(myOutputPkt, (DEVICE_IP, UDP_PORT))

def sendColors():
        redValue = sliderR.get()
        greenValue = sliderG.get()
        blueValue = sliderB.get()
        print('Sending RGB colors(', redValue, greenValue, blueValue,')')
        sendOutput(2, redValue)
        sendOutput(3, greenValue)
        sendOutput(4, blueValue)

def getValue(IOnumber):
        myRequestPkt[11] = IOnumber & 0xff
        myRequestPkt[12] = (IOnumber & 0xff00) >> 8
        myRequestPkt[13] = (IOnumber & 0xff0000) >> 16
        myRequestPkt[14] = (IOnumber & 0xff000000) >> 24

        try:
            print('Sending request...')
            sent = sock.sendto(myRequestPkt, (DEVICE_IP, UDP_PORT))
            sock.setblocking(0)

            ready = select.select([sock], [], [], 5) # timeout_in_seconds
            if ready[0]:
                data = sock.recv(100)
                print(data)

        finally:
            print('closing socket')
            sock.close()

def exitProgram():
        print("Exit Button pressed")
        win.quit()

win.title("playing with UDP packets to control RGB LEDs")
win.geometry('800x600')

colorButton = Button(win, text = "send colors", font = myFont, command = sendColors, height = 1, width = 16)
colorButton.pack()

sliderR = tkinter.Scale(win, from_ = 0, to = 1000, orient = tkinter.HORIZONTAL)
sliderR.set(200)
sliderR.pack()
sliderG = tkinter.Scale(win, from_ = 0, to = 1000, orient = tkinter.HORIZONTAL)
sliderG.set(250)
sliderG.pack()
sliderB = tkinter.Scale(win, from_ = 0, to = 1000, orient = tkinter.HORIZONTAL)
sliderB.set(300)
sliderB.pack()

anaButton = Button(win, text = "get analog value", font = myFont, command = getValue, height = 1, width = 16)
anaButton.pack()

exitButton = Button(win, text = "exit", font = myFont, command = exitProgram, height = 1, width = 16)
exitButton.pack(side = BOTTOM)

mainloop()

如果您需要一般建议,我建议您签出,如果您需要网络/套接字帮助,那么我建议删除所有其他无关(Tk)代码。有关更多信息,请参阅。如果您需要一般建议,我建议您签出,如果您需要网络/套接字帮助,那么我建议删除所有其他无关(Tk)代码。有关更多信息,请参阅