From c91ca6c70357d8a6e5474f4ec4b53164734b9b6b Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 12 Aug 2024 19:07:32 +0200 Subject: [PATCH] Fix some first connect EACCES errors on freshly plugged pm3 --- client/src/uart/uart_posix.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 5e7133354..f728b5deb 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -44,6 +44,7 @@ #include "comms.h" #include "ui.h" +#include "util_posix.h" // msleep // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7 #ifndef SOL_TCP @@ -358,7 +359,13 @@ serial_port uart_open(const char *pcPortName, uint32_t speed, bool slient) { free(prefix); - sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); + // Freshly available port can take a while before getting permission to access it. Up to 600ms on my machine... + for (uint8_t i =0; i < 10; i++) { + sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); + if (sp->fd != -1 || errno != EACCES) + break; + msleep(100); + } if (sp->fd == -1) { uart_close(sp); return INVALID_SERIAL_PORT;