Quantcast
Viewing all articles
Browse latest Browse all 4325

MicroPython • How to receive large (>1024 byte) UDP datagram packets?

This is on a Pico W. I'm using the socket module, trying to receive UDP packets that have more than 1024 bytes. But I can't figure this out - I only get a max of 1024 from a recv() call, and then I don't know how to get the remaining data that should have been received.

Here's my code:

Code:

import networkimport socketfrom time import sleepdef connect():    ssid = 'blablabla'    password = 'hidden'    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    while not wlan.isconnected():        print('Connecting…')        wlan.connect(ssid, password)        waittime = 0        while not wlan.isconnected():            sleep(0.1)            waittime += 1            if waittime > 65:                break    ip = wlan.ifconfig()[0]    return ipdef open_socket(ip):    address = (ip, 80)    connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)    connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)    connection.bind(address)    connection.setblocking(False)    return connectiondef receive_udp(connection):    total = bytearray()    while True:        try:            part, add = connection.recvfrom(2048)            print(len(part))            total.extend(part)        except OSError:            return total        except TypeError:            return total# mainip = connect()print(f'Connected as {ip}')connection = open_socket(ip)print(connection)while True:    data = receive_udp(connection)    if len(data) > 0:        print(f"data: {len(data)}")
When I run it and then send a file to it (size: 1671 byte) with "cat file.png | nc -u 192.168.4.249 80" command, it outputs:

Code:

MPY: soft rebootConnecting…Connecting…Connected as 192.168.4.249<socket state=5 timeout=0 incoming=0 off=0>1024data: 1024
Where's the remaining data, how do I fetch that?

Statistics: Posted by tem_pi — Sat Jul 20, 2024 7:18 pm



Viewing all articles
Browse latest Browse all 4325

Trending Articles