@@ -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;
@@ -374,8 +375,37 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
374
375
return readLine (buffer, size);
375
376
}
376
377
378
+ void TheThingsNetwork::autoBaud ()
379
+ {
380
+ // Courtesy of @jpmeijers
381
+ modemStream->setTimeout (2000 );
382
+ uint8_t attempts = 10 ;
383
+ size_t length = 0 ;
384
+ while (attempts-- && length == 0 )
385
+ {
386
+ delay (100 );
387
+ modemStream->write ((byte)0x00 );
388
+ modemStream->write (0x55 );
389
+ modemStream->write (SEND_MSG);
390
+ sendCommand (SYS_TABLE, 0 , true , false );
391
+ sendCommand (SYS_TABLE, SYS_GET, true , false );
392
+ sendCommand (SYS_TABLE, SYS_GET_VER, false , false );
393
+ modemStream->write (SEND_MSG);
394
+ length = modemStream->readBytesUntil (' \n ' , buffer, sizeof (buffer));
395
+ }
396
+ delay (100 );
397
+ clearReadBuffer ();
398
+ modemStream->setTimeout (10000 );
399
+ baudDetermined = true ;
400
+ }
401
+
377
402
void TheThingsNetwork::reset (bool adr)
378
403
{
404
+ if (!baudDetermined)
405
+ {
406
+ autoBaud ();
407
+ }
408
+
379
409
size_t length = readResponse (SYS_TABLE, SYS_RESET, buffer, sizeof (buffer));
380
410
381
411
// buffer contains "RN2xx3 1.x.x ...", splitting model from version
0 commit comments