2 Replies Latest reply on Jun 26, 2019 6:06 AM by DaCh_4286001

    I want the program won't run until I type in a character into UART.

    DaCh_4286001

      So I want to use UART to write code that program won't run until I type in a character in UART.

       

      my code looks like this

       

      for(;;)

      {

                 while((UART_ReadRxStatus()& UART_RX_STS_FIFO_NOTEMPTY) ==0 )

              {

                  data.command = UART_GetChar();      //put input character as command

              } 

        

                 while((UART_ReadRxStatus()& UART_RX_STS_FIFO_NOTEMPTY) ==0 )

              {

                  data.size= UART_GetChar();      //put input character into size

              } 

       

      }

       

      and it is not working correctly after I type in first character. It never stuck in while loop after UART received first character

        • 1. Re: I want the program won't run until I type in a character into UART.
          MoTa_728816

          Hi,

           

          As I noticed that I've been writing same code again and again,

          recently I wrote a UART utility for myself.

          I hope that It may be useful for you, too.

           

          int get_line(void) ; /* get one line till EOL or the length is up to STR_BUF_LEN */

          int get_string(void) ; / * get a string with a delimiter of space, tab, newline */

           

          they fill strings to

            char str[STR_BUF_LEN]

           

          schematic

          001-schematic.JPG

           

          UART Config

          002-UART-Config.JPG

           

          main.c as a sample usage

          ======================

          #include "project.h"

          #include "stdio.h"

          #include "tty_utils.h"

           

          void init_hardware(void)

          {

              tty_init() ;

              CyGlobalIntEnable; /* Enable global interrupts. */

          }

           

          int main(void)

          {

              init_hardware() ;

           

              splash("tty utils test") ;

           

              prompt() ;

              for(;;) {

          //        if (get_string() > 0) { // got a string

                  if (get_line()) { // got a line

                      print("Got: ") ;

                      print(str) ;

                      print("\n") ;

                      prompt() ;

                  }

              }

          }

          ======================

           

          If you want to stop your program until a string is received.

          Please modify main.c as below

          ======================

          #include "project.h"

          #include "stdio.h"

          #include "tty_utils.h"

           

          void init_hardware(void)

          {

              tty_init() ;

              CyGlobalIntEnable; /* Enable global interrupts. */

          }

           

          int main(void)

          {

              init_hardware() ;

           

              splash("tty utils test") ;

           

              prompt() ;

              for(;;) {

                 while(get_string() <= 0) {

                          ; // waiting for a string to be received

                   }

                   // if program gets here, str has been received

                   print("Got: ") ;

                   print(str) ;

                   print("\n") ;

                   prompt() ;

              }

          }

          ======================

           

          Note:

          After posting the first response with a sample for PSoC 4 (CY8CKIT-044),

          I amended and added another sample for 5LP (CY8CKIT-059).

           

          moto