Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
abho_4730071
Level 4
Level 4
First like received

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

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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

View solution in original post

0 Likes
3 Replies