0
\$\begingroup\$

I am developing a simple radio transmission network using B-L072Z-LRWAN1 boards. The network structure is formed by:

  1. One board acting as a gateway (where data is received).
  2. Multiple boards acting as nodes (which send data to the gateway).

The packet payload sent by nodes is N bytes long and I want to store this packet's information in case gateway stops working and nodes cannot communicate with it. I want to keep data that could not be sent by the node and send it once the gateway is active again.

Let's say I want to keep the last M messages that node could not send to the gateway. The buffer size should be M*N bytes long, but how do I implement this type of buffer in C?

Thank you in advance!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You could try a char[M][N] \$\endgroup\$
    – PlasmaHH
    Aug 29, 2018 at 15:07
  • 1
    \$\begingroup\$ You need to consider if you want FIFO or LIFO implementation with overflow management. Perhaps read this: embedjournal.com/implementing-circular-buffer-embedded-c \$\endgroup\$ Aug 29, 2018 at 15:17
  • \$\begingroup\$ I am using that implementation and is working awesome. Thank you! \$\endgroup\$
    – user197178
    Sep 25, 2018 at 8:52

1 Answer 1

0
\$\begingroup\$
    //pseudo algorithm First In First Out
    int FIFO[16]; // size of 2^N
    int WR_Ptr, RD_Ptr; // Write and read pointer
    int Input, Output;

    // On write event
    FIFO[WR_Ptr] = Input;
    WR_Ptr++; //increment WR pointer
    WR_Ptr &= 15 // apply mask 2^N-1, turn over in circular manner
    if WR_Ptr==RD_Ptr //buffer overrun?
    {
       //handle error
    }

    //On read event
    if WR_Ptr<>RD_Ptr //check if FIFO is not empty
    {
      Output = FIFO[RD_Ptr];
      RD_Ptr++; //increment RD pointer
      RD_Ptr &= 15 // apply mask 2^N-1, turn over in circular manner
    }

//clear FIFO
RD_Ptr = 0;
WR_Ptr = 0;
\$\endgroup\$
6
  • \$\begingroup\$ Could you change the types of FIFO, Input and Output to something other then int? As it is this pseudocode is a bit confusing. \$\endgroup\$
    – jaskij
    Aug 29, 2018 at 18:21
  • \$\begingroup\$ It's a pseudo code only, what thype would be more demonstrative? \$\endgroup\$ Aug 29, 2018 at 18:23
  • \$\begingroup\$ Something suggesting that it stores those messages - something implying a structure, or honestly - anything other than int to differentiate from WR_Ptr and RD_Ptr \$\endgroup\$
    – jaskij
    Aug 29, 2018 at 20:33
  • \$\begingroup\$ @JanDorniak Don't be afraid to post your answer if you have a better solution. \$\endgroup\$ Aug 30, 2018 at 10:41
  • \$\begingroup\$ not really. And I'm not afraid - mostly I use SE on mobile which isn't conductive to posting good answers. Although I might suggest an edit to yours since it's more of a clarification of your answer then separate answer (FIFOs are fairly standard anyway) \$\endgroup\$
    – jaskij
    Aug 30, 2018 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.