@@ -287,6 +287,7 @@ TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn
287
287
{
288
288
this ->debugStream = &debugStream;
289
289
this ->modemStream = &modemStream;
290
+ this ->modemStream ->setTimeout (10000 );
290
291
this ->fp = fp;
291
292
this ->sf = sf;
292
293
this ->fsb = fsb;
@@ -369,8 +370,37 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
369
370
return readLine (buffer, size);
370
371
}
371
372
373
+ void TheThingsNetwork::autoBaud ()
374
+ {
375
+ // Courtesy of @jpmeijers
376
+ modemStream->setTimeout (2000 );
377
+ uint8_t attempts = 10 ;
378
+ size_t length = 0 ;
379
+ while (attempts-- && length == 0 )
380
+ {
381
+ delay (100 );
382
+ modemStream->write ((byte)0x00 );
383
+ modemStream->write (0x55 );
384
+ modemStream->write (SEND_MSG);
385
+ sendCommand (SYS_TABLE, 0 , true , false );
386
+ sendCommand (SYS_TABLE, SYS_GET, true , false );
387
+ sendCommand (SYS_TABLE, SYS_GET_VER, false , false );
388
+ modemStream->write (SEND_MSG);
389
+ length = modemStream->readBytesUntil (' \n ' , buffer, sizeof (buffer));
390
+ }
391
+ delay (100 );
392
+ clearReadBuffer ();
393
+ modemStream->setTimeout (10000 );
394
+ baudDetermined = true ;
395
+ }
396
+
372
397
void TheThingsNetwork::reset (bool adr)
373
398
{
399
+ if (!baudDetermined)
400
+ {
401
+ autoBaud ();
402
+ }
403
+
374
404
size_t length = readResponse (SYS_TABLE, SYS_RESET, buffer, sizeof (buffer));
375
405
376
406
// buffer contains "RN2xx3 1.x.x ...", splitting model from version
0 commit comments