Portenta H7 TCP Latency Issue

Hello,

I ran into a weird problem with TCP Connections on the Portenta H7 and I couldn’t find any mention of it online
TCP Connections seem to have a lot of latency (up to ~500ms).
I ran into it using the requests module, and experimented a bit. UDP connections have a RTT that I would expect, around 1ms over Ethernet.

Here are some measurements that should make it clear

>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 511.019 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 306.276 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 408.367 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 508.853 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 512.186 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 73.175 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 510.502 ms
>>> measure_rtt()
Received b'Hello, server'
Round-trip time: 512.188 ms
>>>

The code I used:
Client (on the Portenta):

def measure_rtt(host='address', port=21212):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    start_time = time.ticks_us()
    s.sendall(b'Hello, server')
    data = s.recv(13)
    end_time = time.ticks_us()
    print('Received', repr(data))
    print('Round-trip time:', time.ticks_diff(end_time, start_time) / 1000, 'ms')
    s.close()

Server (on a PC):

def start_server(host='0.0.0.0', port=21212):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((host, port))
        s.listen()
        while True:
            conn, addr = s.accept()
            with conn:
                print('Connected by', addr)
                while True:
                    data = conn.recv(1024)
                    if not data:
                        break
                    conn.sendall(data)

Has anyone else run into this problem or can reproduce this? Maybe its just my board thats not working

The round trip time cutoff seems to be awfully close to 512ms, to me it seems like a polling interval somewhere

@iabdalkader might have some thoughts.

Hi, could you give me a general-purpose script for the Portenta with the minimum amount of code to generate the issue? The same for the PC. I have python3 on the PC.

Hi thanks for the answer,
I put together some code that should be minimal and complete

On a desktop run following with python

import socket

def start_server(host='0.0.0.0', port=21212):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((host, port))
        s.listen()
        while True:
            conn, addr = s.accept()
            with conn:
                print('Connected by', addr)
                while True:
                    data = conn.recv(1024)
                    if not data:
                        break
                    conn.sendall(data)

start_server()

On the Portenta run following, make sure to replace the SERVER_IP with the ip of the desktop.

import socket
import time
import network

SERVER_IP = 'IP Address of the server here'

def measure_rtt(host=SERVER_IP, port=21212):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    start_time = time.ticks_us()
    s.sendall(b'Hello, server')
    data = s.recv(13)
    end_time = time.ticks_us()
    print('Received', repr(data))
    print('Round-trip time:', time.ticks_diff(end_time, start_time) / 1000, 'ms')
    s.close()

nic = network.LAN()
nic.active(True)
if not nic.isconnected():
    print("Waiting for connection...")
    while not nic.isconnected():
        time.sleep(1)
print(nic.ifconfig())

while True:
    measure_rtt()
    time.sleep(1)

This connects to the network (Ethernet) and continuously runs measure_rtt.
Make sure there is no firewall in the way on the server blocking it

If you want to use wifi the network connnection needs to be changed

Best regards
Yannick

Hi I’ll try to get this debugged for you soon.

Hi, so, I was able to debug this on the RT1062 with the new ethernet shield.

  1. You forgot to turn on DHCP in your script on the Portenta:
nic = network.LAN()
nic.active(True)
nic.ifconfig("dhcp")
  1. Here’s a log for my round-trip time:
Received b'Hello, server'
Round-trip time: 260.299 ms
Received b'Hello, server'
Round-trip time: 22.581 ms
Received b'Hello, server'
Round-trip time: 17.422 ms
Received b'Hello, server'
Round-trip time: 21.405 ms
Received b'Hello, server'
Round-trip time: 20.219 ms
Received b'Hello, server'
Round-trip time: 20.48 ms
Received b'Hello, server'
Round-trip time: 20.615 ms
Received b'Hello, server'
Round-trip time: 21.747 ms
Received b'Hello, server'
Round-trip time: 19.911 ms
Received b'Hello, server'
Round-trip time: 20.974 ms
Received b'Hello, server'
Round-trip time: 20.26 ms
Received b'Hello, server'
Round-trip time: 20.442 ms
Received b'Hello, server'
Round-trip time: 20.612 ms
Received b'Hello, server'
Round-trip time: 20.093 ms
Received b'Hello, server'
Round-trip time: 21.404 ms
Received b'Hello, server'
Round-trip time: 21.274 ms
Received b'Hello, server'
Round-trip time: 21.056 ms
Received b'Hello, server'
Round-trip time: 20.763 ms
Received b'Hello, server'
Round-trip time: 21.229 ms
Received b'Hello, server'
Round-trip time: 26.665 ms
Received b'Hello, server'
Round-trip time: 14.531 ms
Received b'Hello, server'
Round-trip time: 20.717 ms
Received b'Hello, server'
Round-trip time: 20.32 ms
Received b'Hello, server'
Round-trip time: 20.822 ms
Received b'Hello, server'
Round-trip time: 20.646 ms
Received b'Hello, server'
Round-trip time: 514.952 ms
Received b'Hello, server'
Round-trip time: 19.988 ms
Received b'Hello, server'
Round-trip time: 21.723 ms
Received b'Hello, server'
Round-trip time: 21.187 ms
Received b'Hello, server'
Round-trip time: 19.392 ms
Received b'Hello, server'
Round-trip time: 20.626 ms
Received b'Hello, server'
Round-trip time: 20.222 ms
Received b'Hello, server'
Round-trip time: 20.545 ms
Received b'Hello, server'
Round-trip time: 21.844 ms
Received b'Hello, server'
Round-trip time: 19.254 ms
Received b'Hello, server'
Round-trip time: 20.618 ms
Received b'Hello, server'
Round-trip time: 20.329 ms
Received b'Hello, server'
Round-trip time: 20.363 ms
Received b'Hello, server'
Round-trip time: 21.749 ms
Received b'Hello, server'
Round-trip time: 19.717 ms
Received b'Hello, server'
Round-trip time: 20.516 ms
Received b'Hello, server'
Round-trip time: 20.466 ms
Received b'Hello, server'
Round-trip time: 20.613 ms
Received b'Hello, server'
Round-trip time: 20.564 ms
Received b'Hello, server'
Round-trip time: 20.299 ms
Received b'Hello, server'
Round-trip time: 21.347 ms
Received b'Hello, server'
Round-trip time: 20.396 ms
Received b'Hello, server'
Round-trip time: 19.97 ms
Received b'Hello, server'
Round-trip time: 20.371 ms
Received b'Hello, server'
Round-trip time: 19.996 ms
Received b'Hello, server'
Round-trip time: 19.825 ms
Received b'Hello, server'
Round-trip time: 20.572 ms
Received b'Hello, server'
Round-trip time: 20.226 ms
Received b'Hello, server'
Round-trip time: 20.608 ms
Received b'Hello, server'
Round-trip time: 20.564 ms
Received b'Hello, server'
Round-trip time: 20.299 ms
Received b'Hello, server'
Round-trip time: 20.557 ms
Received b'Hello, server'
Round-trip time: 21.574 ms
Received b'Hello, server'
Round-trip time: 20.216 ms
Received b'Hello, server'
Round-trip time: 20.573 ms
Received b'Hello, server'
Round-trip time: 20.467 ms
Received b'Hello, server'
Round-trip time: 20.709 ms
Received b'Hello, server'
Round-trip time: 20.608 ms
Received b'Hello, server'
Round-trip time: 20.564 ms
Received b'Hello, server'
Round-trip time: 20.614 ms
Received b'Hello, server'
Round-trip time: 25.256 ms
Received b'Hello, server'
Round-trip time: 6.809 ms
Received b'Hello, server'
Round-trip time: 20.574 ms
Received b'Hello, server'
Round-trip time: 20.517 ms
Received b'Hello, server'
Round-trip time: 20.66 ms
Received b'Hello, server'
Round-trip time: 20.711 ms
Received b'Hello, server'
Round-trip time: 20.91 ms
Received b'Hello, server'
Round-trip time: 20.512 ms
Received b'Hello, server'
Round-trip time: 21.053 ms
Received b'Hello, server'
Round-trip time: 20.95 ms
Received b'Hello, server'
Round-trip time: 20.181 ms
Received b'Hello, server'
Round-trip time: 21.228 ms
Received b'Hello, server'
Round-trip time: 20.652 ms
Received b'Hello, server'
Round-trip time: 20.583 ms
Received b'Hello, server'
Round-trip time: 20.485 ms
Received b'Hello, server'
Round-trip time: 19.548 ms
Received b'Hello, server'
Round-trip time: 20.188 ms
Received b'Hello, server'
Round-trip time: 20.988 ms
Received b'Hello, server'
Round-trip time: 20.885 ms
Received b'Hello, server'
Round-trip time: 20.75 ms
Received b'Hello, server'
Round-trip time: 20.398 ms
Received b'Hello, server'
Round-trip time: 20.261 ms
Received b'Hello, server'
Round-trip time: 20.751 ms
Received b'Hello, server'
Round-trip time: 26.528 ms
Received b'Hello, server'
Round-trip time: 14.394 ms
Received b'Hello, server'
Round-trip time: 20.837 ms
Received b'Hello, server'
Round-trip time: 20.662 ms
Received b'Hello, server'
Round-trip time: 21.352 ms
Received b'Hello, server'
Round-trip time: 20.045 ms
Received b'Hello, server'
Round-trip time: 21.859 ms
Received b'Hello, server'
Round-trip time: 21.395 ms
Received b'Hello, server'
Round-trip time: 20.394 ms
Received b'Hello, server'
Round-trip time: 19.443 ms
Received b'Hello, server'
Round-trip time: 21.531 ms
Received b'Hello, server'
Round-trip time: 21.851 ms
Received b'Hello, server'
Round-trip time: 21.883 ms
Received b'Hello, server'
Round-trip time: 20.689 ms
Received b'Hello, server'
Round-trip time: 19.219 ms
Received b'Hello, server'
Round-trip time: 21.138 ms
Received b'Hello, server'
Round-trip time: 20.002 ms
Received b'Hello, server'
Round-trip time: 19.08 ms
Received b'Hello, server'
Round-trip time: 20.348 ms
Received b'Hello, server'
Round-trip time: 20.721 ms
Received b'Hello, server'
Round-trip time: 21.008 ms
Received b'Hello, server'
Round-trip time: 20.482 ms
Received b'Hello, server'
Round-trip time: 19.944 ms
Received b'Hello, server'
Round-trip time: 20.576 ms
Received b'Hello, server'
Round-trip time: 12.914 ms
Received b'Hello, server'
Round-trip time: 23.033 ms
Received b'Hello, server'
Round-trip time: 18.207 ms
Received b'Hello, server'
Round-trip time: 21.192 ms
Received b'Hello, server'
Round-trip time: 21.267 ms
Received b'Hello, server'
Round-trip time: 21.088 ms
Received b'Hello, server'
Round-trip time: 21.456 ms
Received b'Hello, server'
Round-trip time: 19.807 ms
Received b'Hello, server'
Round-trip time: 20.775 ms
Received b'Hello, server'
Round-trip time: 20.959 ms
Received b'Hello, server'
Round-trip time: 20.747 ms
Received b'Hello, server'
Round-trip time: 18.991 ms
Received b'Hello, server'
Round-trip time: 20.738 ms
Received b'Hello, server'
Round-trip time: 20.524 ms
Received b'Hello, server'
Round-trip time: 19.503 ms
Received b'Hello, server'
Round-trip time: 20.222 ms
Received b'Hello, server'
Round-trip time: 21.357 ms
Received b'Hello, server'
Round-trip time: 20.561 ms
Received b'Hello, server'
Round-trip time: 22.055 ms
Received b'Hello, server'
Round-trip time: 20.62 ms
Received b'Hello, server'
Round-trip time: 30.458 ms
Received b'Hello, server'
Round-trip time: 9.463 ms
Received b'Hello, server'
Round-trip time: 21.018 ms
Received b'Hello, server'
Round-trip time: 21.749 ms

I’m going from ethernet on the RT1062 to WiFi on my PC. I get about 20 ms, which seems reasonable.

As for testing on the Portenta:

Received b'Hello, server'
Round-trip time: 187.903 ms
Received b'Hello, server'
Round-trip time: 20.96 ms
Received b'Hello, server'
Round-trip time: 26.255 ms
Received b'Hello, server'
Round-trip time: 16.408 ms
Received b'Hello, server'
Round-trip time: 21.091 ms
Received b'Hello, server'
Round-trip time: 22.579 ms
Received b'Hello, server'
Round-trip time: 20.566 ms
Received b'Hello, server'
Round-trip time: 22.01 ms
Received b'Hello, server'
Round-trip time: 21.006 ms
Received b'Hello, server'
Round-trip time: 20.873 ms
Received b'Hello, server'
Round-trip time: 21.661 ms
Received b'Hello, server'
Round-trip time: 21.221 ms
Received b'Hello, server'
Round-trip time: 21.091 ms
Received b'Hello, server'
Round-trip time: 27.174 ms
Received b'Hello, server'
Round-trip time: 15.096 ms
Received b'Hello, server'
Round-trip time: 21.222 ms
Received b'Hello, server'
Round-trip time: 20.916 ms
Received b'Hello, server'
Round-trip time: 21.003 ms
Received b'Hello, server'
Round-trip time: 20.523 ms
Received b'Hello, server'
Round-trip time: 27.174 ms
Received b'Hello, server'
Round-trip time: 15.314 ms
Received b'Hello, server'
Round-trip time: 20.392 ms
Received b'Hello, server'
Round-trip time: 18.889 ms
Received b'Hello, server'
Round-trip time: 19.741 ms
Received b'Hello, server'
Round-trip time: 22.845 ms
Received b'Hello, server'
Round-trip time: 20.982 ms
Received b'Hello, server'
Round-trip time: 21.293 ms
Received b'Hello, server'
Round-trip time: 19.974 ms
Received b'Hello, server'
Round-trip time: 17.239 ms
Received b'Hello, server'
Round-trip time: 21.43 ms
Received b'Hello, server'
Round-trip time: 22.593 ms
Received b'Hello, server'
Round-trip time: 21.834 ms
Received b'Hello, server'
Round-trip time: 21.119 ms
Received b'Hello, server'
Round-trip time: 21.623 ms
Received b'Hello, server'
Round-trip time: 15.94 ms
Received b'Hello, server'
Round-trip time: 20.265 ms
Received b'Hello, server'
Round-trip time: 21.507 ms
Received b'Hello, server'
Round-trip time: 20.886 ms
Received b'Hello, server'
Round-trip time: 21.003 ms
Received b'Hello, server'
Round-trip time: 21.584 ms
Received b'Hello, server'
Round-trip time: 20.944 ms
Received b'Hello, server'
Round-trip time: 21.642 ms
Received b'Hello, server'
Round-trip time: 22.341 ms
Received b'Hello, server'
Round-trip time: 21.797 ms
Received b'Hello, server'
Round-trip time: 22.283 ms
Received b'Hello, server'
Round-trip time: 21.39 ms
Received b'Hello, server'
Round-trip time: 20.497 ms
Received b'Hello, server'
Round-trip time: 21.156 ms
Received b'Hello, server'
Round-trip time: 21.351 ms
Received b'Hello, server'
Round-trip time: 21.643 ms
Received b'Hello, server'
Round-trip time: 20.206 ms
Received b'Hello, server'
Round-trip time: 21.139 ms
Received b'Hello, server'
Round-trip time: 21.099 ms

Same 20ms.

So, I’d check your network.

Thanks for testing, can you tell me the setup you have for the Portenta and the OpenMV version?

I have tested with an esp32 in the same network and it worked fine, so I am wondering if my Portenta is faulty

I just had it connected via Ethernet to an Eero 6 WiFi Router. My laptop then was connected via WiFi.

And the OpenMV version? I am using ‘OpenMV v4.5.2; MicroPython v1.20-omv-r22; PORTENTA-STM32H747’

OpenMV v4.4.3; MicroPython v1.20-omv; PORTENTA-STM32H747

The RT1062, though, was using the latest firmware. So, any MicroPython changes would have affected it too.