arduino-libraries / MKRWAN

An Arduino library for sending and receiving data using LoRaWAN protocol and LoRa® radios.
https://www.arduino.cc
GNU Lesser General Public License v3.0
88 stars 60 forks source link

Impossible to receive 0x00 in the received message payload #33

Closed ruipalma closed 6 years ago

ruipalma commented 6 years ago

I can confirm the behaviour reported by vahisa in https://forum.arduino.cc/index.php?topic=536628.0, The modem.read() method do not work with 0x00 payload.

facchinm commented 6 years ago

Will take a look, thanks for reporting

ruipalma commented 6 years ago

Using the TtnModemTest.ino to interact directly with the modem I can see that the modem is not transmiting the 0x00 bytes after a +RECV. So the problem seems to be in the murata firmware.

ruipalma commented 6 years ago

The bug is in the murata firmware. The command AT+FORMAT only apply to the sended messages, not the received ones. The received messages are processed in the function

ATEerror_t at_Receive(const char *param)
{
  AT_PRINTF("+RECV=");
  AT_PRINTF("%d,%d\r\n\n", ReceivedDataPort, ReceivedDataSize);
  if (ReceivedDataSize)
  {
    AT_PRINTF("%s", ReceivedData);
    ReceivedDataSize = 0;
  }
  AT_PRINTF("\r");

  return AT_OK;
}

As you can see the received data is scanned as a string, so the 0x00 is a terminator. Maybe we can use the "format_send_v2" to process the received data also. Something like:

ATEerror_t at_Receive(const char *param)
{

  AT_PRINTF("+RECV=");

  if (format_send_v2==0)
  {
      AT_PRINTF("%d,%d\r\n\n", ReceivedDataPort, ReceivedDataSize);
      if (ReceivedDataSize)
      {
        AT_PRINTF("%s", ReceivedData);
      }
  }
  else
  {
      AT_PRINTF("%d,%d\r\n\n", ReceivedDataPort, ReceivedDataSize*2);
      for (unsigned i = 0; i < ReceivedDataSize; i++)
      {
        AT_PRINTF("%02x", ReceivedData[i]);
      }
  }
  ReceivedDataSize = 0;
  AT_PRINTF("\r");

  return AT_OK;
}

Maybe this issue should be in the mkr 1300 fw repository,

EDIT: ReceivedDataSize*2 to prevent library break.

ruipalma commented 6 years ago

OK. I have done the modification from the last post in the murata fw and I can confirm that its working. The modem sends a HEX string when +DFORMAT=1. I attached the modified fw. I keep the version. fw.zip

EDIT: ReceivedDataSize*2 to prevent library break.

facchinm commented 6 years ago

Hi @ruipalma , would you mind adding a PR on https://github.com/arduino/mkrwan1300-fw with your modification? Thanks!

ruipalma commented 6 years ago

Hi @facchinm, I have done the PR.

facchinm commented 6 years ago

Fixed by merging https://github.com/arduino/mkrwan1300-fw/commit/e547242a651568b7fefe0d5b543a6ff6022619f5