I have tank project with RPi 4. My tasks are connect a camera and get that cam record from main pc, and movement control from main pc. I did these things by UDP. But I had a problem.
import cv2, socket, os
import time
import pygame
import pickle
import numpy as np
from threading import Thread
# PC
class Connection:
def __init__(self, s_ip, rpi_ip, port):
self.sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s_ip = s_ip
self.c_ip = rpi_ip
self.port = port
self.sck.bind((s_ip, port))
def read_image(self):
sizedata, addr = self.sck.recvfrom(4)
size = int.from_bytes(sizedata, 'little')
data, addr = self.sck.recvfrom(size)
print(size)
data = pickle.loads(data)
return cv2.imdecode(data, cv2.IMREAD_COLOR)
def send_command(self, cmd):
self.sck.sendto(cmd, (self.c_ip, self.port))
def render(conn):
while True:
img = conn.read_image()
cv2.imshow('server', img)
if cv2.waitKey(10) == 13:
break
cv2.destroyAllWindows()
###################################################################
# pc ip rpi ip port
conn = Connection('10.20.50.142', '10.20.50.13', 6666)
render_thread = Thread(target=render, args=(conn,))
render_thread.start()
screen = pygame.display.set_mode([600, 450])
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP: conn.send_command(b'K_UP')
elif event.key == pygame.K_DOWN : conn.send_command(b'K_DN')
elif event.key == pygame.K_RIGHT: conn.send_command(b'K_RT')
elif event.key == pygame.K_LEFT: conn.send_command(b'K_LT')
elif event.key == pygame.K_q: conn.send_command(b'SHUT')
elif event.type == pygame.KEYUP: conn.send_command(b'STOP')
break
after this code I got that message. [WinError 10040] A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itsel.
If I explain , I connect to my main pc and after about 5 second cam window is suddenly closed and I got above message from terminal. Could you help me about this problem?