Page 1 of 1

UART not reading integer greater than 10

Posted: 23 Jan 2024 12:12
by kumar123
Hi,
This given below code is not printing number greater than 10, for the first time if I entered 10,12,23,45,1234,..... it get stuck, second time it print repeating number(1010, 1212,2323,...).The entered number even very large number(1,2,10,23,100,230,500,1000,123234,112323435,....). when I entered single digit number, after entering digit greater than 10. It will work. For single digit number it is working.
Any suggestion would be appreciated?

Code: Select all

UART1_Init(9600); // Initialize the UART with a baud rate of 9600
    
    while(1) {
        if(UART1_Data_Ready() == 1) {
            memset(input_num, 0, 25);
            memset(output_num, 0, 25);
            UART1_Read_Text(input_num, ";", 24);
            sprintf(output_num,"%s\r\n", input_num);
        }
        UART1_Write_Text(output_num);
        Delay_ms(500);
    }
Regards,
Kumar

Re: UART not reading integer greater than 10

Posted: 24 Jan 2024 15:00
by IvanJeremic
Hi,

The Uart echo example seems to process larger numbers fine for me, it tried sending 1,2,10,23,100,230,500,1000,123234,112323435 and it did it without an issue.

Regards,

Ivan.

Re: UART not reading integer greater than 10

Posted: 25 Jan 2024 05:17
by kumar123
Hi Ivan,
It works for this below code:
I will read any number and print, it works fine.
Code:1

Code: Select all

While(1){ 
         if(UART1_Data_Ready()== 1){
             UART1_Read_Text(input, ";", 255);
             UART1_Write_Text(input);
         }
}
but it does not work for this below code:
Code:2

Code: Select all

While(1){ 
         if(UART1_Data_Ready()== 1){
             UART1_Read_Text(input, ";", 255);
             UART1_Write_Text(input);
         }
         else {
             UART1_Write_Text(input);
         }
}
In code :2 If I entered any number(like:12, 10,234235, ......) first time it get stuck, in second time if I entered it print duplicate of that entered number(1212,1010,234235234235).

Regards,
Kumar

Re: UART not reading integer greater than 10

Posted: 25 Jan 2024 12:02
by IvanJeremic
Hi,

I am not sure what you are trying to do with the:
else {
UART1_Write_Text(input);
}

This will just keep printing everything you wrote forever, if it gets stuck it is probably because it got too much information, if it does not print everything the input string is not large enough for all the read data.

Regards,

Ivan.

Re: UART not reading integer greater than 10

Posted: 25 Jan 2024 13:14
by kumar123
Hi Ivan,
I want output of this below code like- initially it print the whatever have the initial value, it will keep on printing until I entered the new integer, again it will keep on printing(old value ) value until I entered the new value and so on.
This below code works well for single digit Integer(0-9), when I read integer greater than 10, first time it get stuck, second time I read same value it print wrong value.

Code: Select all

void main(){
     char str[256] = "0";
     char st[256];
     TRISC6_bit = 0;   // Tx pin set as output
     TRISC7_bit = 1;   // Rx pin set as input

     UART1_Init(9600); // Initialize the UART with a baud rate of 9600

     Delay_ms(4);     //Wait for UART to stabilize

     while(1){
     
        if(UART1_Data_Ready() == 1){
            UART1_Read_Text(st, ";", 255);
            UART1_Write_Text(st);
             strcpy(str,st);
            UART1_Write_Text("\r\n");
            Delay_ms(1000);

        }
        else{
            UART1_Write_Text(str);
            UART1_Write_Text("\r\n");
            Delay_ms(1000);
        }
     }
 }
This is when I am reading single digit integer:
screa1.png
screa1.png (207.33 KiB) Viewed 480 times
This is when first time I am reading integer greater than 10 it get stuck:
scr2.png
scr2.png (204.46 KiB) Viewed 480 times
This is when second time enter the same integer get gives wrong value:
sc3.png
sc3.png (204.52 KiB) Viewed 480 times
Hope you understood the problem.

Regards,
Kumar

Re: UART not reading integer greater than 10

Posted: 30 Jan 2024 08:20
by kumar123
Hi,

Does anyone have suggestion for the above query?

Regards
Kumar

Re: UART not reading integer greater than 10

Posted: 31 Jan 2024 15:08
by IvanJeremic
Hi,

The MCU seems to not be able to handle this, whenever it tries to read a bit larger data it will cause this issue, I tried changing the code a bit but at best it would work clunky.

I think you should try using interrupts for this instead of If.
External Interrupt.7z
(4.03 KiB) Downloaded 21 times
Regards,

Ivan.