3 Replies Latest reply on Aug 26, 2020 3:38 AM by MoTa_728816

    How can i read a string value and return Hexadeciaml value in psoc CY8CKIT-050 5lp

    abho_4730071

      I am sending a string value(Lets say "Test") from my application. So i want to receive that string("Test") in psoc and send hexadecimal value to my application from psoc.

      I need a very simple PSOC program.

      Note :- I know how to read a character but not sure how to read a string in PSOC.

       

      Thanks,

      Abinash

        • 1. Re: How can i read a string value and return Hexadeciaml value in psoc CY8CKIT-050 5lp
          MoTa_728816

          Hi,

           

          While ago, I posted a sample tty_utils

          Re: tty_utils a utility sample for CLI type program

           

          If you use may sample, you can get a string by using

           

          int value ;

           

          if (get_stiring()) {

              sscanf(str, "%x", &value) ; /* get value from the str[] which is the string buffer */

             ...

          }

           

          moto

          P.S. by using my sample, when get_string() returns non-0 value,

          the string is stored in a global char array str[] which has the length of STR_BUF_LEN+1

          Or you can use get_line() which will get input up to EOL.

          In the above sample, I got a string and converted to an int as the string was a hexdecimal string, such as "0x32".

          • 2. Re: How can i read a string value and return Hexadeciaml value in psoc CY8CKIT-050 5lp
            abho_4730071

            How to receive string using UART Communication.

            I have written a program but its not able to compare.

             

            Below is my program.

             

            #include "project.h"

             

             

            int main(void)

            {

                //uint8_t c;

                char c[30];

               

                CyGlobalIntEnable; /* Enable global interrupts. */

             

             

                UART_Start() ;

                UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                UART_PutString("This is an ECHO program\n\r") ;

               

                for (;;) {

              

                    if (UART_GetRxBufferSize() > 0) { /* received some string */

                        c[29] = UART_GetByte() ;

                       

                       

                       // UART_PutString("This is an ECHO program\n\r") ;

                      //c = UART_GetChar() ;

                        //CyDelay(1000);

                       

                       int val=strcmp(c, "factory cal ph0x0D");

                    int val1 =strcmp(c, "factory cal orp0X0D");

                        if (val==0)

                        {

                         

                            UART_PutString("Light \n") ;

                            LED_Write(1);

                            UART_PutString("else1");

                       

                        }

                        else if(val1==0)

                        {

                            LED_Write(0);

                            UART_PutString("else2");

                        }

                      

                    }

                   

                }

            }

            • 3. Re: How can i read a string value and return Hexadeciaml value in psoc CY8CKIT-050 5lp
              MoTa_728816

              Hi,

               

              A c string is an array of chars ending with a NULL (0).

               

              So "Hello" is

              cstr[] = { 'H', 'e', 'l', 'l', 'o', 0 } ;

               

              So we need to read letters from the start of the array to the end,

              meantime we need to monitor if the string is too long for the buffer.

              As you were specify the buffer length 30, I changed it to 31 for additional NULL.

               

              Schematic

              002-schematic.JPG

              UART Config

              003-UART_Config1.JPG

              004-UART_Config2.JPG

              May be we can go with the default 4 bytes buffer, but to be safe, I chose 32 (could be 31)

               

              Pins

              005-pins.JPG

               

              main.c

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

              #include "project.h"

              #include "stdio.h"

               

               

              #define STR_LEN 30

              #define BS '\b'

               

               

              int main(void)

              {

                  int  str_received = 0 ;

                  uint8_t c ;

                  char cstr[STR_LEN+1]; /* one for NULL */

                  int  index = 0 ;

                 

                  CyGlobalIntEnable; /* Enable global interrupts. */

               

               

                  UART_Start() ;

                  UART_PutString("\x1b[2J\x1b[;H") ; /* clear screen */

                  UART_PutString("UART STR Receive Test\n\r") ;

               

               

                  for (;;) {

                      if ((str_received == 0) && (UART_GetRxBufferSize() > 0)) { /* received some letters */

                          c = UART_GetByte() ;

                          if (c == BS) {

                              if (index > 0) {

                                  index-- ;

                                  cstr[index] = 0 ;

                                  UART_PutString("\b \b") ;

                              }

                          } else {

                              if ((c != '\r')&&(c != '\n')) {

                                  cstr[index] = c ;

                                  index++ ;

                                  if (index >= STR_LEN) {

                                      cstr[STR_LEN] = 0 ;

                                      index = 0 ;

                                      str_received = 1 ;/* but over flow */

                                  }

                              } else {

                                  cstr[index] = 0 ; /* terminate the string */

                                  index = 0 ;

                                  str_received = 1 ;

                              }

                              UART_PutChar(c) ;

                          }

                      }

                     

                      if (str_received) { 

                          int val  = strcmp(cstr, "factory cal ph0x0D");

                          int val1 = strcmp(cstr, "factory cal orp0X0D");

               

               

                          if (val==0) {

                              UART_PutString("Light \n") ;

                              LED_Write(1);

                              UART_PutString("else1");

                              UART_PutString("\n\r") ;

                          } else if(val1==0) {

                              LED_Write(0);

                              UART_PutString("else2");

                              UART_PutString("\n\r") ;

                          }

                          str_received = 0 ;

                      }

                  }

              }

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

               

              Tera Term log

              001-teraterm.JPG

               

              moto