converting uart string to int mikroc

Status
Not open for further replies.

Varunme

Member
How can i convert the uart strings to int in mikroc?

i attempted

atoi(uart_rd);


and

added a '\0' to uart_rd and sent to atoi, that too not working
 
atoi() should work....... Have you an example of the string you're trying to convert...

There is possible characters, that atoi() can't handle
 
i send single characters of

1, 2, 3 etc


when i used

char sec ="123";

it worked
by using uart , its not working
 
Last edited:
Code:
while(1)
     {
        delay_ms(200);
        if (UART1_Data_Ready())
        {

            on = UART1_Read();
            on2 = strcat(on,"\0");
            
            
                         
             
            intme = atoi (yon2);
                     

           
          for(rot=0; rot<=intme; rot++)
          {

                 PORTB=0b00010000; delay_ms(200);
                 PORTB=0b00001000; delay_ms(200);

                 }
                  }
        }
 
How do you declare the "on" variable?
What does "UART1_Read();" return?

I think it should be something like this:

Code:
char on[10];   // char table to store received string
int intme;

// Check if character is ready for reading and read it.
if (UART1_Data_Ready() == 1) { 
    on[0] = UART1_Read();   // read one character from uart
    on[1] = '\0';           // insert terminating zero
}

intme = atoi(on);

The above code should read one character from uart (if available) and convert it with itoa.
 
Last edited:
Then how can i convert , if i have to send a character like "100" to uart ?,

can be done by increasing the size of on[] array ? by taking the strlen ?
 
Last edited:
This is my final code, but even with single character, the program not working


Code:
void main(){
unsigned char txt[2];
int y,rot;
char on[10];   // char table to store received string
int intme;
trisb=0x00;
portb=0x00;

    UART1_Init(9600);



// Check if character is ready for reading and read it.
        if (UART1_Data_Ready() == 1) {
        on[0] = UART1_Read();   // read one character from uart
        on[1] = '\0';           // insert terminating zero
}

 
    
          for(rot=0; rot<=atoi(on );  rot++)
          {

                 PORTB=0b00010000; delay_ms(200);
                 PORTB=0b00001000; delay_ms(200);

                 }
                 }
 
Then how can i convert , if i have to send a character like "100" to uart ?

You can use the "UART1_Read_Text()" function to read multiple characters. Here is a library reference with example code:
**broken link removed**
 
when i tried

on[7] = UART1_Read_Text(output, "m", 255); // read one character from uart
on[8] = '\0'; // insert terminating zero

it says

"illegal expression with void"
 
on[7] = UART1_Read_Text(output, "m", 255);

No.... this function allows you to pass a buffer..... "output" a delimiter "m" and re-try "255" but returns a void.... so " on[7] " will receive an illegal qualifier.
 
"UART1_Read_Text()" -function does not return anything. It takes three parameters.
- char array to store the received text
- sequence of characters that identifies the end of a received string
- expected string length (or 255 if not known).

So, the piece of code that should work in your example should be:

UART1_Read_Text(on, "m", 255);

If you send something like "93m", the above line of code should be able to read and store the string "93" in the variable "on".

You should probably learn how string arrays work in C language:
**broken link removed**

Do you have trouble understanding the library reference?
 
Last edited:
yes,

I modified it as

Code:
void main(){
unsigned char txt[2];
unsigned char output[7];
int y,rot;
char on[28];   // char table to store received string
int intme;
trisb=0x00;
portb=0x00;

    	UART1_Init(9600);

	while(1){

	while (!UART1_Data_Ready());        // wait for UART character in buffer
	
	UART1_Read_Text(output, "m", 255);   // read one character from uart

	y=strlen(output);
     
	on[y]=output;
	on[y+1]='\0';        // insert terminating zero
    

          for(rot=0; rot<=atoi(on)-1; ++rot)
          {

                 PORTB=0b00010000; delay_ms(200);
                 PORTB=0b00001000; delay_ms(200);

                }
		}

       }


is it something wrong with it ? , its not working
 
Last edited:
is it something wrong with it ? , its not working

Yes. You cannot copy a string like this:
on[y]=output;

You don't even need to copy the string. You can use the "output" variable with the atoi().

Code:
void main(){
char output[7];
int i,rot;
trisb=0x00;
portb=0x00;
 
UART1_Init(9600);
 
while(1){
 
	while (!UART1_Data_Ready());        // wait for UART character in buffer

        // Initialize string with all '\0'
        for(i=0; i<7; i++){
            output[i] = '\0';
        } 

	UART1_Read_Text(output, "m", 255);   // read input from uart
 
        for(rot=0; rot<=atoi(output)-1; ++rot)
        {
               PORTB=0b00010000; delay_ms(200);
               PORTB=0b00001000; delay_ms(200);
        }
}
}
 
Last edited:
Your input is in the output buffer.... there is no need for the "on[y]; " just do " rot = atoi(output); " or " on[y] = output[y] ;"
 
Because of the data ready routine..... is the data coming continuously?
 
yes,

once i send the data with 'm' termination,

after some time the data will come with another 'm' termination.

so data coming continously


How can i correct it for continous ?
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…