0

我使用的是预编码脚本,因为我是 Python 菜鸟,在套接字编程方面遇到了一些麻烦。

我使用两台计算机,一台正在运行 Matlab/Simulink 实时应用程序,它通过 UDP 向我的 Windows 笔记本电脑发送数据。数据包含光标和目标的 XY 坐标、目标半径和颜色(总共 6 个信号)。在笔记本电脑上,我正在运行一个 python 脚本,理想情况下,它会读出 udp 信号并在辅助屏幕上实时(或接近实时)在给定坐标处绘制椭圆。不幸的是,执行速度可能不足以跟上以 1000Hz 发送的 UDP 信号。这导致在发送信号的变化与屏幕上显示之间存在大约 40 秒的巨大延迟。

如果我是正确的,那么问题出在 recvfrom 命令中。接收到的数据存储在某种缓冲区中,并以 FIFO 样式显示,而不是以 1000Hz 实时显示所有内容。问题是我不知道如何解决这个问题。大多数关于套接字编程的解释都超出了我的想象。它不需要以 1000Hz 显示,50Hz 就可以了,但尽快显示更改会非常好。而不是 FIFO,我只想读取最后收到的数据。

谁能帮我吗?先感谢您。

import socket
import struct
from PySide import QtGui
from PySide import QtCore
import random
import ctypes
import sys
import time
import math

UDP_IP = "192.168.1.25" #server
UDP_PORT = 25000 #server

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT)) #


class MyGui(QtGui.QWidget):

    def __init__(self):

        super(MyGui,self).__init__()

        #self.screen()
        user32 = ctypes.windll.user32
        self._W = user32.GetSystemMetrics(0)
        self._H = user32.GetSystemMetrics(1)
        self._rW = random.random()
        self._rH = random.random()
        self._rR = random.random()
        self._rC = 500

        self._CursorX = 0
        self._CursorY = 0
        self._CursorXt = 0
        self._CursorYt = 0
        self._CursorRt = 0
        self._Color = 0    

        print(self.geometry())
        self._W = self.width()
        self.setStyleSheet("background-color: black")
        self.setGeometry(QtCore.QRect(2000,50,500,300))
        self.showFullScreen()


    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        self._drawTarget(qp)
        self._drawCursor(qp)
        qp.end()

    def _drawTarget(self, qpT):

        # color1 = QtGui.QColor()
        # color1.setNamedColor('#d4d4d4')

        color1=QtGui.QColor(self._Color)

        qpT.setPen(color1)

        qpT.setBrush(QtGui.QColor(self._Color))
        qpT.drawEllipse(QtCore.QPointF(self._CursorXt, self._CursorYt), self._CursorRt, self._CursorRt)

    def _drawCursor(self, qpC):

        color2 = QtGui.QColor(0, 0, 0)
        color2.setNamedColor('#FFFF00')
        qpC.setPen(color2)

        qpC.setBrush(QtGui.QColor('#FFFF00'))
        qpC.drawEllipse(QtCore.QPointF(self._CursorX, self._CursorY), 20, 20)

    def mousePressEvent(self, QMouseEvent):
        print ("mouse position")
        print (QMouseEvent.pos())

    def setCursorPosition(self,x,y,xt,yt,Rt,color):
        self._CursorX = x
        self._CursorY = y
        self._CursorXt = xt
        self._CursorYt = yt
        self._CursorRt = Rt
        self._Color = color 
        self.update()



class MyProcess:
    def __init__(self, whichGui):
        self._x = 0
        self._y = 0 
        self._xt = 0
        self._yt = 0
        self._Rt = 0
        self._color = 0
        self._whichGui = whichGui
        self._T = QtCore.QTimer()
        self._T.timeout.connect(self._update)
        self._T.start(30)

    def _update(self):

        data, addr = sock.recvfrom(8*6)
        #print ("received message:", data)
        value = struct.unpack('dddddd', data)
        #print (value)
        self._xt = value[0]
        self._yt = value[1]
        self._Rt = value[2]
        self._x = value[3]
        self._y = value[4]
        self._color = value[5]
        self._whichGui.setCursorPosition(self._x, self._y, self._xt, self._yt, self._Rt, self._color)


# Run the Graph
G = MyGui()
G.show()


P = MyProcess(G)
4

0 回答 0