3 Replies Latest reply on Sep 27, 2018 8:00 AM by nik_berry_3605396

    UART Rx development help

    nik_berry_3605396

      I apologize for being a bit of a newb. C is not my strongest language and it's way of doing arrays is a bit foreign to me. I was hoping somebody could help me with figuring out how to implement a UART RX receiving function for fetching packets being sent from another dev board to my PSoC 5LP board. They gave examples of TX and RX functions. I understood TX but having trouble figuring out the RX. They gave:

      int recv_packet(char *p, int len){

          char c;

          int received = 0;

          /* sit in a loop reading bytes until we put together

          * a whole packet.

          * Make sure not to copy them into the packet if we

          * run out of room.

          */

          while(1) {

              /* get a character to process

              */

              c = UART_GetChar();

              /* handle bytestuffing if necessary

              */

              switch(c) {

                  /* if it's an END character then we're done with

                  * the packet

                  */

                  case END:

                      /* a minor optimization: if there is no

                      * data in the packet, ignore it. This is

                      * meant to avoid bothering IP with all

                      * the empty packets generated by the

                      * duplicate END characters which are in

                      * turn sent to try to detect line noise.

                      */

                      if(received)

                          return received;

                      else

                          break;

                  /* if it's the same code as an ESC character, wait

                  * and get another character and then figure out

                  * what to store in the packet based on that.

                  */

                  case ESC:

                      c = UART_GetChar();

                      /* if "c" is not one of these two, then we

                      * have a protocol violation. The best bet

                      * seems to be to leave the byte alone and

                      * just stuff it into the packet

                      */

                      switch(c) {

                          case ESC_END:

                              c = END;

                              break;

                          case ESC_ESC:

                              c = ESC;

                              break;

                      }

                  /* here we fall into the default handler and let

                  * it store the character for us

                  */

                  default:

                      if(received < len)

                      p[received++] = c;

              }

          }

      }

       

      So first question: The comments they gave are helpful but I'm having trouble with the while loop. Is the return in the first case (for END) going to end the while loop? I've never seen a while loop use a (1) instead of some variable.

       

      Second question: How do I use this function effectively? With fiddling with the TX function I've understood that arrays need to have a declared length in C but how would I essentialy make the array dynamically resized to be the size of the packet received?