From 9ec3b2af0fe020421909a226ad7752e51551ff73 Mon Sep 17 00:00:00 2001 From: awawa-dev Date: Tue, 24 Jan 2023 21:45:57 +0100 Subject: [PATCH 1/4] Prefer ESP devices if ESP handshake is enabled --- sources/leddevice/dev_serial/ProviderRs232.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sources/leddevice/dev_serial/ProviderRs232.cpp b/sources/leddevice/dev_serial/ProviderRs232.cpp index 04ed9beec..227d2eed3 100644 --- a/sources/leddevice/dev_serial/ProviderRs232.cpp +++ b/sources/leddevice/dev_serial/ProviderRs232.cpp @@ -296,7 +296,7 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t* data) QString ProviderRs232::discoverFirst() { - for (int round = 0; round < 2; round++) + for (int round = 0; round < 4; round++) for (auto const& port : QSerialPortInfo::availablePorts()) { #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) @@ -306,9 +306,16 @@ QString ProviderRs232::discoverFirst() #endif { QString infoMessage = QString("%1 (%2 => %3)").arg(port.description()).arg(port.systemLocation()).arg(port.portName()); - - if (round != 0 || - (port.description().contains("Bluetooth", Qt::CaseInsensitive) == false && + quint16 vendor = port.vendorIdentifier(); + quint16 prodId = port.productIdentifier(); + bool knownESPA = (vendor == 0x303a && (prodId == 0x80c2)); + bool knownESPB = (vendor == 0x10c4 && (prodId == 0xea60)) || + (vendor == 0x1A86 && (prodId == 0x7523 || prodId == 0x55d4)); + if (round == 3 || + (_espHandshake && round == 0 && knownESPA) || + (_espHandshake && round == 1 && knownESPB) || + (!_espHandshake && round == 2 && + port.description().contains("Bluetooth", Qt::CaseInsensitive) == false && port.systemLocation().contains("ttyAMA0", Qt::CaseInsensitive) == false)) { Info(_log, "Serial port auto-discovery. Found serial port device: %s", QSTRING_CSTR(infoMessage)); From fb51e4ada9b0149bc165ed76a817e12a713f244a Mon Sep 17 00:00:00 2001 From: Awawa Date: Wed, 25 Jan 2023 11:51:39 +0100 Subject: [PATCH 2/4] Rescan devices each time when open --- sources/leddevice/dev_serial/ProviderRs232.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sources/leddevice/dev_serial/ProviderRs232.cpp b/sources/leddevice/dev_serial/ProviderRs232.cpp index 227d2eed3..bec7b2b90 100644 --- a/sources/leddevice/dev_serial/ProviderRs232.cpp +++ b/sources/leddevice/dev_serial/ProviderRs232.cpp @@ -65,9 +65,7 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig) } ProviderRs232::~ProviderRs232() -{ - if (_rs232Port.isOpen()) - _rs232Port.close(); +{ } int ProviderRs232::open() @@ -133,6 +131,11 @@ int ProviderRs232::close() QTimer::singleShot(200, this, [this]() { if (_rs232Port.isOpen()) EspTools::goingSleep(_rs232Port); }); EspTools::goingSleep(_rs232Port); + + for (int i = 0; i < 6 && _rs232Port.isOpen(); i++) + { + _rs232Port.waitForReadyRead(100); + } } else { @@ -157,7 +160,7 @@ bool ProviderRs232::powerOff() bool ProviderRs232::tryOpen(int delayAfterConnect_ms) { - if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty()) + if (_deviceName.isEmpty() || _rs232Port.portName().isEmpty() || (_isAutoDeviceName && _espHandshake)) { if (!_rs232Port.isOpen()) { @@ -309,7 +312,8 @@ QString ProviderRs232::discoverFirst() quint16 vendor = port.vendorIdentifier(); quint16 prodId = port.productIdentifier(); bool knownESPA = (vendor == 0x303a && (prodId == 0x80c2)); - bool knownESPB = (vendor == 0x10c4 && (prodId == 0xea60)) || + bool knownESPB = (vendor == 0x303a) || + (vendor == 0x10c4 && (prodId == 0xea60)) || (vendor == 0x1A86 && (prodId == 0x7523 || prodId == 0x55d4)); if (round == 3 || (_espHandshake && round == 0 && knownESPA) || @@ -323,7 +327,7 @@ QString ProviderRs232::discoverFirst() } else { - Warning(_log, "Serial port auto-discovery. Ignoring possible bluetooth device for now, try to find different available serial port: %s", QSTRING_CSTR(infoMessage)); + Warning(_log, "Serial port auto-discovery. Skipping this device for now: %s, VID: 0x%x, PID: 0x%x", QSTRING_CSTR(infoMessage), vendor, prodId); } } } From d6745e54ac194ce5615a3271ffd009824f2e29bb Mon Sep 17 00:00:00 2001 From: Awawa Date: Wed, 25 Jan 2023 23:55:30 +0100 Subject: [PATCH 3/4] Add auto-resume for Adalight device --- .../leddevice/dev_serial/ProviderRs232.cpp | 31 +++++++++++++++++++ .../leddevice/schemas/schema-adalight.json | 12 +++++++ 2 files changed, 43 insertions(+) diff --git a/sources/leddevice/dev_serial/ProviderRs232.cpp b/sources/leddevice/dev_serial/ProviderRs232.cpp index bec7b2b90..ddb7ef6f2 100644 --- a/sources/leddevice/dev_serial/ProviderRs232.cpp +++ b/sources/leddevice/dev_serial/ProviderRs232.cpp @@ -71,6 +71,10 @@ ProviderRs232::~ProviderRs232() int ProviderRs232::open() { int retval = -1; + + if (_retryMode) + return retval; + _isDeviceReady = false; // open device physically @@ -79,6 +83,27 @@ int ProviderRs232::open() // Everything is OK, device is ready _isDeviceReady = true; retval = 0; + + _currentRetry = 0; + _retryMode = false; + } + else if (_maxRetry > 0) + { + if (_currentRetry <= 0) + _currentRetry = _maxRetry + 1; + + _currentRetry--; + + if (_currentRetry > 0) + Warning(_log, "The serial device is not ready... will try to reconnect (try %i/%i).", (_maxRetry - _currentRetry + 1), _maxRetry); + else + Error(_log, "The serial device is not ready... give up."); + + if (_currentRetry > 0) + { + _retryMode = true; + QTimer::singleShot(2000, [this]() { _retryMode = false; if (_currentRetry > 0) enableDevice(true); }); + } } return retval; } @@ -294,6 +319,12 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t* data) } } } + + if (_maxRetry > 0 && rc == -1) + { + QTimer::singleShot(2000, this, [=]() { enable(); }); + } + return rc; } diff --git a/sources/leddevice/schemas/schema-adalight.json b/sources/leddevice/schemas/schema-adalight.json index 53231fed0..b9fac0ca9 100644 --- a/sources/leddevice/schemas/schema-adalight.json +++ b/sources/leddevice/schemas/schema-adalight.json @@ -138,6 +138,18 @@ } }, "propertyOrder" : 11 + }, + "maxRetry": + { + "type" : "integer", + "format" : "stepper", + "step" : 1, + "title" : "edt_dev_max_retry", + "minimum" : 0, + "maximum" : 120, + "default" : 0, + "required" : true, + "propertyOrder" : 12 } }, "additionalProperties": true From 56e012ef55a56debbe76a2dceaf9247eb6c3aa61 Mon Sep 17 00:00:00 2001 From: Awawa Date: Fri, 27 Jan 2023 16:12:13 +0100 Subject: [PATCH 4/4] Update ProviderRs232.cpp --- sources/leddevice/dev_serial/ProviderRs232.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/leddevice/dev_serial/ProviderRs232.cpp b/sources/leddevice/dev_serial/ProviderRs232.cpp index ddb7ef6f2..5bae220a4 100644 --- a/sources/leddevice/dev_serial/ProviderRs232.cpp +++ b/sources/leddevice/dev_serial/ProviderRs232.cpp @@ -52,12 +52,14 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig) _baudRate_Hz = deviceConfig["rate"].toInt(); _delayAfterConnect_ms = deviceConfig["delayAfterConnect"].toInt(0); _espHandshake = deviceConfig["espHandshake"].toBool(false); + _maxRetry = _devConfig["maxRetry"].toInt(60); Debug(_log, "Device name : %s", QSTRING_CSTR(_deviceName)); Debug(_log, "Auto selection: %d", _isAutoDeviceName); Debug(_log, "Baud rate : %d", _baudRate_Hz); Debug(_log, "ESP handshake : %s", (_espHandshake) ? "ON" : "OFF"); Debug(_log, "Delayed open : %d", _delayAfterConnect_ms); + Debug(_log, "Retry limit : %d", _maxRetry); isInitOK = true; }