6
6
// qt includes
7
7
#include < QSerialPortInfo>
8
8
#include < QEventLoop>
9
+ #include < QThread>
9
10
10
11
#include < chrono>
12
+ #include < utils/InternalClock.h>
11
13
12
14
// Constants
13
15
constexpr std::chrono::milliseconds WRITE_TIMEOUT{ 1000 }; // device write timeout in ms
@@ -22,6 +24,7 @@ ProviderRs232::ProviderRs232(const QJsonObject& deviceConfig)
22
24
, _isAutoDeviceName(false )
23
25
, _delayAfterConnect_ms(0 )
24
26
, _frameDropCounter(0 )
27
+ , _espHandshake(true )
25
28
{
26
29
}
27
30
@@ -46,11 +49,13 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
46
49
_isAutoDeviceName = _deviceName.toLower () == " auto" ;
47
50
_baudRate_Hz = deviceConfig[" rate" ].toInt ();
48
51
_delayAfterConnect_ms = deviceConfig[" delayAfterConnect" ].toInt (0 );
52
+ _espHandshake = deviceConfig[" espHandshake" ].toBool (false );
49
53
50
- Debug (_log, " deviceName : %s" , QSTRING_CSTR (_deviceName));
51
- Debug (_log, " AutoDevice : %d" , _isAutoDeviceName);
52
- Debug (_log, " baudRate_Hz : %d" , _baudRate_Hz);
53
- Debug (_log, " delayAfCon ms: %d" , _delayAfterConnect_ms);
54
+ Debug (_log, " Device name : %s" , QSTRING_CSTR (_deviceName));
55
+ Debug (_log, " Auto selection: %d" , _isAutoDeviceName);
56
+ Debug (_log, " Baud rate : %d" , _baudRate_Hz);
57
+ Debug (_log, " ESP handshake : %s" , (_espHandshake) ? " ON" : " OFF" );
58
+ Debug (_log, " Delayed open : %d" , _delayAfterConnect_ms);
54
59
55
60
isInitOK = true ;
56
61
}
@@ -59,6 +64,8 @@ bool ProviderRs232::init(const QJsonObject& deviceConfig)
59
64
60
65
ProviderRs232::~ProviderRs232 ()
61
66
{
67
+ if (_rs232Port.isOpen ())
68
+ _rs232Port.close ();
62
69
}
63
70
64
71
int ProviderRs232::open ()
@@ -76,6 +83,24 @@ int ProviderRs232::open()
76
83
return retval;
77
84
}
78
85
86
+ void ProviderRs232::waitForExitStats ()
87
+ {
88
+ if (_rs232Port.isOpen ())
89
+ {
90
+ if (_rs232Port.bytesAvailable () > 16 )
91
+ {
92
+ auto incoming = QString (_rs232Port.readAll ());
93
+
94
+ Info (_log, " Received: %s" , QSTRING_CSTR (incoming));
95
+ }
96
+ if (!_isDeviceReady)
97
+ {
98
+ Debug (_log, " Close UART: %s" , QSTRING_CSTR (_deviceName));
99
+ _rs232Port.close ();
100
+ }
101
+ }
102
+ }
103
+
79
104
int ProviderRs232::close ()
80
105
{
81
106
int retval = 0 ;
@@ -89,9 +114,20 @@ int ProviderRs232::close()
89
114
{
90
115
Debug (_log, " Flush was successful" );
91
116
}
92
- Debug (_log, " Close UART: %s" , QSTRING_CSTR (_deviceName));
93
- _rs232Port.close ();
94
- // Everything is OK -> device is closed
117
+
118
+
119
+ if (_espHandshake)
120
+ {
121
+ // read the statistics on goodbye
122
+ QTimer::singleShot (6000 , this , &ProviderRs232::waitForExitStats);
123
+ connect (&_rs232Port, &QSerialPort::readyRead, this , &ProviderRs232::waitForExitStats);
124
+ }
125
+ else
126
+ {
127
+ Debug (_log, " Close UART: %s" , QSTRING_CSTR (_deviceName));
128
+ _rs232Port.close ();
129
+ }
130
+
95
131
}
96
132
return retval;
97
133
}
@@ -150,11 +186,61 @@ bool ProviderRs232::tryOpen(int delayAfterConnect_ms)
150
186
151
187
if (!serialPortInfo.isNull ())
152
188
{
153
- if (!_rs232Port.open (QIODevice::ReadWrite))
189
+ if (!_rs232Port.isOpen () && !_rs232Port. open (QIODevice::ReadWrite))
154
190
{
155
191
this ->setInError (_rs232Port.errorString ());
156
192
return false ;
157
193
}
194
+
195
+ if (_espHandshake)
196
+ {
197
+ disconnect (&_rs232Port, &QSerialPort::readyRead, nullptr , nullptr );
198
+
199
+ // reset to defaults
200
+ _rs232Port.setDataTerminalReady (true );
201
+ _rs232Port.setRequestToSend (false );
202
+ QThread::msleep (50 );
203
+
204
+ // reset device
205
+ _rs232Port.setDataTerminalReady (false );
206
+ _rs232Port.setRequestToSend (true );
207
+ QThread::msleep (100 );
208
+
209
+ // resume device
210
+ _rs232Port.setRequestToSend (false );
211
+ QThread::msleep (100 );
212
+
213
+ // read the reset message, search for AWA tag
214
+ auto start = InternalClock::now ();
215
+
216
+ while (InternalClock::now () - start < 1000 )
217
+ {
218
+ _rs232Port.waitForReadyRead (100 );
219
+ if (_rs232Port.bytesAvailable () > 16 )
220
+ {
221
+ auto incoming = _rs232Port.readAll ();
222
+ for (int i = 0 ; i < incoming.length (); i++)
223
+ if (!(incoming[i] == ' \n ' ||
224
+ (incoming[i] >= ' ' && incoming[i] <= ' Z' ) ||
225
+ (incoming[i] >= ' a' && incoming[i] <= ' z' )))
226
+ {
227
+ incoming.replace (incoming[i], ' *' );
228
+ }
229
+ QString result = QString (incoming).remove (' *' ).replace (' \n ' ,' ' ).trimmed ();
230
+ if (result.indexOf (" Awa driver" ,Qt::CaseInsensitive) >= 0 )
231
+ {
232
+ Info (_log, " DETECTED DEVICE USING HYPERSERIALESP8266/HYPERSERIALESP32 FIRMWARE (%s) at %i msec" , QSTRING_CSTR (result), int (InternalClock::now () - start));
233
+ start = 0 ;
234
+ break ;
235
+ }
236
+ }
237
+ if (InternalClock::now () <= start)
238
+ break ;
239
+ }
240
+
241
+ if (start != 0 )
242
+ Error (_log, " Could not detect HyperSerialEsp8266/HyperSerialESP32 device" );
243
+ }
158
244
}
159
245
else
160
246
{
0 commit comments