How to convert strings received in UART to numbers

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

cross mob
NuVi_4267411
Level 1
Level 1
First like received First reply posted First question asked

Hi, I'm developing a project involving the PSoC4 and UART using SCBs.

I have the following code that echos the sent bytes on the terminal.

"

int main(void)
{
/* Start UART operation */
UART_Start();

/* Transmit header to the terminal */
for(;;)
{
if(UART_SpiUartGetRxBufferSize() != 0ul) {
uint32 data = UART_SpiUartReadRxData();
UART_SpiUartWriteTxData(data);
}
}
}

"

I want to receive a numerical value throught UART interface and convert it to another range of values, but I can´t get it to recgonize the received bytes as a number and not as individual. 

Thanks for considering my question,

Nuno Vicente

0 Likes
1 Solution

Nuno Vicente,

The project I supplied is a demo for the GetString() function.

For you to use it without all the demo features do the following:

  • Copy the following files into your project:
    • GetString_char.c
    • GetString.h
    • GetString.c.  Inside the comm_GetString() function made the following changes
      • Replace comm_GetChar() with UART_SpiUartGetChar()
      • Replace comm_PuttChar() with UART_SpiUartPutChar()
  • In your code when you want to read your terminal input call:
    UART_GetString(tstr, TSTR_SZ, filter_type); 
    where
    tstr is the pointer to a temporary multicharacter string array.
    TSTR_SZ is the size of the array
    filter_type is one of the string types you want to filter for.  These filter types will be found in GetString.h  For example ift_dec_fixed will support reading decimal numbers with a decimal point.

Once you complete your input with an ENTER, then the string will be returned in tstr.

Then you can use scanf() to convert the filtered string into a number if that's what you want to do next.

Here's a code fragment using GetString().

 

#define TSTR_SZ 30
char tstr[TSTR_SZ];
...
{
uint16 idacval;
...
  UART_SpiUartPutString("\n\rEnter the current IDAC current value in uA (eq. 250)");
  comm_GetString(tstr, TSTR_SZ, ift_dec);  /* Get the input from the terminal (filtered to decimal characters) */
  sscanf(tstr, "%u, &idacval);   /* convert the decimal string to an unsigned 16-bit number */
...
}

 

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

0 Likes
6 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Nuno Vicente,

I recommend my GetString() function.

I have supplied an example project at: GetString-function-that-supports-input-character-filtering    

It take input from a terminal and converts it to the format you specify such as:

  • unsigned or signed decimal (eg .  34953 or -4510)
  • unsigned or signed decimal fixed point (eg  3.1415 or -46.789)
  • unsigned hexidecimal  (eg. 401EA)
  • floating point (eq. 834.5, 510.45454e-9, -0.6343e45)
  • string (eg. The Lion Sleeps Tonight!)
  • Filesystem string [used for SD card valid Volume, directory and file names] (eq. Volume:\directory\dir2\filename.ext)

It automatically filters out characters not acceptable to the type you select.

Try it.  You'll like it!

Len
"Engineering is an Art. The Art of Compromise."

Hi 

Thanks for your contribuition, but I don't know how to place that function on my code. Also my PSoC is PSoC 4200, CY8CKIT-042 so I think that I have to port that code to my PSoC.

The goal of my design is to receive a value from 0-4000 mV (it also could be 4.000 or similar) from UART interface and convert it to 0-255, for setting the IDAC output current.

Could you please tell me how to do it with your piece of code?

Thanks,

Nuno Vicente

0 Likes

Nuno Vicente,

The project I supplied is a demo for the GetString() function.

For you to use it without all the demo features do the following:

  • Copy the following files into your project:
    • GetString_char.c
    • GetString.h
    • GetString.c.  Inside the comm_GetString() function made the following changes
      • Replace comm_GetChar() with UART_SpiUartGetChar()
      • Replace comm_PuttChar() with UART_SpiUartPutChar()
  • In your code when you want to read your terminal input call:
    UART_GetString(tstr, TSTR_SZ, filter_type); 
    where
    tstr is the pointer to a temporary multicharacter string array.
    TSTR_SZ is the size of the array
    filter_type is one of the string types you want to filter for.  These filter types will be found in GetString.h  For example ift_dec_fixed will support reading decimal numbers with a decimal point.

Once you complete your input with an ENTER, then the string will be returned in tstr.

Then you can use scanf() to convert the filtered string into a number if that's what you want to do next.

Here's a code fragment using GetString().

 

#define TSTR_SZ 30
char tstr[TSTR_SZ];
...
{
uint16 idacval;
...
  UART_SpiUartPutString("\n\rEnter the current IDAC current value in uA (eq. 250)");
  comm_GetString(tstr, TSTR_SZ, ift_dec);  /* Get the input from the terminal (filtered to decimal characters) */
  sscanf(tstr, "%u, &idacval);   /* convert the decimal string to an unsigned 16-bit number */
...
}

 

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Sorry but I did what you said but it says "implicit declaration of function 'UART_GetString' is invalid in C99"

NuVi_4267411_1-1622763634511.png

 

I really don't know what do I have to do here, if you could help me, I appreciate.

Nuno Vicente

0 Likes

Nuno Vicente,

Sorry.  Please look at my late post for corrections.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

While ago, I posted the following code sample.

https://community.cypress.com/t5/Code-Examples/tty-utils-a-utility-sample-for-CLI-type-program/m-p/7...

With this utility (tty_utils.h and tty_utisl.c) we can write main.c something like

#include "project.h"
#include "stdio.h"
#include "tty_utils.h"

void init_hardware(void)
{
    tty_init() ;
    CyGlobalIntEnable; /* Enable global interrupts. */
}

int main(void)
{
    int number = 0 ;
    
    init_hardware() ;

    splash("unmber receving test") ;

    prompt() ;
    for(;;) {
//        if (get_string() > 0) { // got a string
        if (get_line()) { // got a line

            print("Got: ") ;
            print(str) ;
            print(" : ") ;
            sscanf(str, "%d", &number) ;
            snprintf(str, STR_BUF_LEN, "%d = %x", number, number) ;
            print(str) ;
            print("\n") ;
            prompt() ;
        }
    }
}

And the Tera Term log is something like

001-Tera-Term-log.JPG

Note: After the "Got :" the first number is actually the received string,

the second one and third one are sprintf'ed int number.

Schematic 

002-schematic.JPG

Pins

003-Pins.JPG

 

Note: Although the sample was written for CY8CKIT-044 (CY8C4247AZI-M485),

today I used CY8CKIT-043 as I was writing some demos of CapSense and

this board was connected to my PC. Anyway, 044 and 043 has same device on board

and the same UART pins are used, so it worked...

 

moto

0 Likes