arduino-libraries / MKRGSM

GNU Lesser General Public License v2.1
55 stars 51 forks source link

MKR 1400 Hangs when antenna removed caused by Serial Flush #70

Open momor10 opened 5 years ago

momor10 commented 5 years ago

Hello, disconnecting the antenna during runtime will cause the MKR 1400 (didnt test any other) to crash. It happens in the MODEM.cpp in the send method during the _uart->flush();

void ModemClass::send(const char* command)
{
  if (_lowPowerMode) {
    digitalWrite(_dtrPin, LOW);
    delay(5);
  }

  // compare the time of the last response or URC and ensure 
  // at least 20ms have passed before sending a new command
  unsigned long delta = millis() - _lastResponseOrUrcMillis;
  if(delta < MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS) {
    delay(MODEM_MIN_RESPONSE_OR_URC_WAIT_TIME_MS - delta);
  }

  _uart->println(command);
  _uart->flush(); //crashes here
  _atCommandState = AT_COMMAND_IDLE;
  _ready = 0;
}

it doesnt matter when the antenna is disconnected , it always happens at the next call of that line. Im not sure how the antenna and the uart are related to eachother. why would it crash and why wouldnt it recover after reconnecting the antenna. After commenting out the _uart->flush() line it works and recovers from the signal loss after reconnecting the antenna. However im sure that line wasnt added just for fun. So is there another way to make sure all data are transmitted without using flush?

Im also having stability issues and waiting for https://github.com/arduino-libraries/MKRGSM/issues/66 and https://github.com/arduino-libraries/MKRGSM/issues/27 to be resolved. For now i added some timeouts to the while loops in the GSMClient.cpp and am testing..

momor10 commented 5 years ago

it will actually hang in the while loop of the Uart::flush() method in https://github.com/arduino/ArduinoCore-samd/blob/master/cores/arduino/Uart.cpp:

void Uart::flush()
{
  while(txBuffer.available()); // wait until TX buffer is empty <-- stuck here

  sercom->flushUART();
}