@@ -52,27 +52,31 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
52
52
_baudRate_Hz = deviceConfig[" rate" ].toInt ();
53
53
_delayAfterConnect_ms = deviceConfig[" delayAfterConnect" ].toInt (0 );
54
54
_espHandshake = deviceConfig[" espHandshake" ].toBool (false );
55
+ _maxRetry = _devConfig[" maxRetry" ].toInt (60 );
55
56
56
57
Debug (_log, " Device name : %s" , QSTRING_CSTR (_deviceName));
57
58
Debug (_log, " Auto selection: %d" , _isAutoDeviceName);
58
59
Debug (_log, " Baud rate : %d" , _baudRate_Hz);
59
60
Debug (_log, " ESP handshake : %s" , (_espHandshake) ? " ON" : " OFF" );
60
61
Debug (_log, " Delayed open : %d" , _delayAfterConnect_ms);
62
+ Debug (_log, " Retry limit : %d" , _maxRetry);
61
63
62
64
isInitOK = true ;
63
65
}
64
66
return isInitOK;
65
67
}
66
68
67
69
ProviderRs232::~ProviderRs232 ()
68
- {
69
- if (_rs232Port.isOpen ())
70
- _rs232Port.close ();
70
+ {
71
71
}
72
72
73
73
int ProviderRs232::open ()
74
74
{
75
75
int retval = -1 ;
76
+
77
+ if (_retryMode)
78
+ return retval;
79
+
76
80
_isDeviceReady = false ;
77
81
78
82
// open device physically
@@ -81,6 +85,27 @@ int ProviderRs232::open()
81
85
// Everything is OK, device is ready
82
86
_isDeviceReady = true ;
83
87
retval = 0 ;
88
+
89
+ _currentRetry = 0 ;
90
+ _retryMode = false ;
91
+ }
92
+ else if (_maxRetry > 0 )
93
+ {
94
+ if (_currentRetry <= 0 )
95
+ _currentRetry = _maxRetry + 1 ;
96
+
97
+ _currentRetry--;
98
+
99
+ if (_currentRetry > 0 )
100
+ Warning (_log, " The serial device is not ready... will try to reconnect (try %i/%i)." , (_maxRetry - _currentRetry + 1 ), _maxRetry);
101
+ else
102
+ Error (_log, " The serial device is not ready... give up." );
103
+
104
+ if (_currentRetry > 0 )
105
+ {
106
+ _retryMode = true ;
107
+ QTimer::singleShot (2000 , [this ]() { _retryMode = false ; if (_currentRetry > 0 ) enableDevice (true ); });
108
+ }
84
109
}
85
110
return retval;
86
111
}
@@ -133,6 +158,11 @@ int ProviderRs232::close()
133
158
134
159
QTimer::singleShot (200 , this , [this ]() { if (_rs232Port.isOpen ()) EspTools::goingSleep (_rs232Port); });
135
160
EspTools::goingSleep (_rs232Port);
161
+
162
+ for (int i = 0 ; i < 6 && _rs232Port.isOpen (); i++)
163
+ {
164
+ _rs232Port.waitForReadyRead (100 );
165
+ }
136
166
}
137
167
else
138
168
{
@@ -157,7 +187,7 @@ bool ProviderRs232::powerOff()
157
187
158
188
bool ProviderRs232::tryOpen (int delayAfterConnect_ms)
159
189
{
160
- if (_deviceName.isEmpty () || _rs232Port.portName ().isEmpty ())
190
+ if (_deviceName.isEmpty () || _rs232Port.portName ().isEmpty () || (_isAutoDeviceName && _espHandshake) )
161
191
{
162
192
if (!_rs232Port.isOpen ())
163
193
{
@@ -291,12 +321,18 @@ int ProviderRs232::writeBytes(const qint64 size, const uint8_t* data)
291
321
}
292
322
}
293
323
}
324
+
325
+ if (_maxRetry > 0 && rc == -1 )
326
+ {
327
+ QTimer::singleShot (2000 , this , [=]() { enable (); });
328
+ }
329
+
294
330
return rc;
295
331
}
296
332
297
333
QString ProviderRs232::discoverFirst ()
298
334
{
299
- for (int round = 0 ; round < 2 ; round ++)
335
+ for (int round = 0 ; round < 4 ; round ++)
300
336
for (auto const & port : QSerialPortInfo::availablePorts ())
301
337
{
302
338
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
@@ -306,17 +342,25 @@ QString ProviderRs232::discoverFirst()
306
342
#endif
307
343
{
308
344
QString infoMessage = QString (" %1 (%2 => %3)" ).arg (port.description ()).arg (port.systemLocation ()).arg (port.portName ());
309
-
310
- if (round != 0 ||
311
- (port.description ().contains (" Bluetooth" , Qt::CaseInsensitive) == false &&
345
+ quint16 vendor = port.vendorIdentifier ();
346
+ quint16 prodId = port.productIdentifier ();
347
+ bool knownESPA = (vendor == 0x303a && (prodId == 0x80c2 ));
348
+ bool knownESPB = (vendor == 0x303a ) ||
349
+ (vendor == 0x10c4 && (prodId == 0xea60 )) ||
350
+ (vendor == 0x1A86 && (prodId == 0x7523 || prodId == 0x55d4 ));
351
+ if (round == 3 ||
352
+ (_espHandshake && round == 0 && knownESPA) ||
353
+ (_espHandshake && round == 1 && knownESPB) ||
354
+ (!_espHandshake && round == 2 &&
355
+ port.description ().contains (" Bluetooth" , Qt::CaseInsensitive) == false &&
312
356
port.systemLocation ().contains (" ttyAMA0" , Qt::CaseInsensitive) == false ))
313
357
{
314
358
Info (_log, " Serial port auto-discovery. Found serial port device: %s" , QSTRING_CSTR (infoMessage));
315
359
return port.portName ();
316
360
}
317
361
else
318
362
{
319
- Warning (_log, " Serial port auto-discovery. Ignoring possible bluetooth device for now, try to find different available serial port: %s " , QSTRING_CSTR (infoMessage));
363
+ Warning (_log, " Serial port auto-discovery. Skipping this device for now: %s, VID: 0x%x, PID: 0x%x " , QSTRING_CSTR (infoMessage), vendor, prodId );
320
364
}
321
365
}
322
366
}
0 commit comments