UART1_Read_Text() is not working when I used escape sequences like \r\n

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
kumar123
Posts: 77
Joined: 17 Oct 2023 07:32

UART1_Read_Text() is not working when I used escape sequences like \r\n

#1 Post by kumar123 » 30 Apr 2024 05:25

Hello,
I was getting problem every time when I used UART1_Write_Text("\r\n"); and Delay_ms(1000);.
This the flow of execution of this given code.

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 code get stuck when I read data more than one byte or two digit numbers.
In this code, If I remove the UART1_Write_Text("\r\n") and Delay_ms(1000);, It will work.

can someone explain why this is happening, I want the same flow of execution like above code.

Regards,
Kumar

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#2 Post by paulfjujo » 30 Apr 2024 08:52

Hello,


you can try this :
Delay located outside of RX loop

Code: Select all



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

A better way is to use interrupt to manage RX UART ...

kumar123
Posts: 77
Joined: 17 Oct 2023 07:32

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#3 Post by kumar123 » 01 May 2024 07:00

Hi paulfjujo,
The way you suggested in the code I tried it is not working.
Writting these statement outside else it will not effect anythingUART1_Write_Text("\r\n"); Delay_ms(1000);

next thing you are talking about interrupt, could you explain more about this thing like, how to use interrupt?

regards
Kumar

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#4 Post by paulfjujo » 01 May 2024 17:22

kumar123 wrote:
01 May 2024 07:00

next thing you are talking about interrupt, could you explain more about this thing like, how to use interrupt?
tested on PIC12F1840

Code: Select all


#define MAXLEN1 64
char Buffer1[MAXLEN1];
unsigned short error;
volatile unsigned int CptErr;
volatile int   DataReady1 =0;      // drapeau Data recu
volatile int NB;
volatile int Index1, i1;
volatile  unsigned char c1, cx;



void Write_String(char * texte)
{
while (* texte)
 {
  UART_Write(* texte++);
 }
}

// --- Copie le texte depuis ROM vers RAM
void strConstRamCpy(char *dest, const char *source) {
  while(*source) *dest++ = *source++ ;
  *dest = 0 ;    // terminateur
}

void CRLF1()
{
 UART_Write(CR);
 UART_Write(LF);
}


void UART1_Write_CText(const char *txt1) {
   while (*txt1)
      UART1_Write(*txt1++);
      Delay_ms(1);

}
   
   
   void Raz_Buffer1()
{
       DataReady1=0;
      // nettoye le buffer
      for(i1=0;i1<MAXLEN1-1;i1++)  Buffer1[i1]=0;
        if(RCIF_bit==1) c1=RCREG;
      i1=0;
      Index1=0;
      c1=0;
      RCIE_bit = 1;
      p1=0;

}
   

void Interrupt() iv 0x0004 ics ICS_AUTO
{
 if ( (RCIE_bit==1)&& (RCIF_bit==1))
   {
    c1 = RCREG;
   // traitement separe des erreurs de COM
      if (RCSTA.OERR==1)    // voir parag 16.1.26 p273
      {
       RCSTA.CREN = 0 ;
       c1 = RCREG;
       TXREG=c1;     // echo of receiveid char
       RCSTA.CREN = 1 ;
        CptErr++;

       }
      if(RCSTA.FERR==1 )
      {
      RCSTA.SPEN = 0 ;
      CptErr++;
      RCSTA.SPEN= 1 ;
       c1 = RCREG;
      }
      // stop sur detection CR ou maxima bytes  atteint
       if( (( c1==13) || (i1>NB) )
        {
           RCIE_bit=0 ; //interdit IT Reception UART
           DataReady1 = 1;
            Buffer1[i1]=0;
            Index1=i1;
            Delay_ms(10);
           if(RCIF_bit==1) c1=RCREG;
            i1=0;
            c1=0;
            }
            else
            {
            Buffer1[i1]=c1;
            Index1=i1;
            i1++;
         }

   }

void main()
{

  
  // ... do Init Hardware and  ....
  
    UART1_Init(19200);

// delimiteur is CR =0x0D=13=Carriage return 
// used into the RX UART interrupt to get the message when CR occurs and detected into the keyboard message..

      NB=MAXLEN1-2;
	  DataReady1=0;
      Index1=0;
      GIE_bit=1;

     while(1)
     {
       Raz_Buffer1();
       Delay_ms(1000);   
	  if (DataReady1==1)
      { UART1_Write_Text(Buffer1);
        CRLF1()  ; //new line
      }
	  // ... treat you data stored into the Buffer1
	  
     }
 }
 
don't forget to chek your terminal behavior with EOL ( End OF Line)
example with my YAT Terminal
Terminal_setting_to_check.jpg
Terminal_setting_to_check.jpg (96.57 KiB) Viewed 187 times

kumar123
Posts: 77
Joined: 17 Oct 2023 07:32

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#5 Post by kumar123 » 03 May 2024 07:31

Hello,
When I introduced some delay or "\r\n" escape sequences, the code stopped reading text data. Why is this happening?
this is the below code I have

Code: Select all

void main(){
   char uart_input[10], uar[10] = "0";
   int entry = 1;
   RC1PPS = 0x17;
   CK1PPS = 0x16;
   RC0PPS = 0x0D;
   CKPPS = 0x0C;
   TRISC7_bit = 1; // RX1 as input
   TRISC6_bit = 0; // TX1 as output
   UART1_Init(9600);
   
   while(1){
        if(UART1_Data_Ready() == 1 ){
              UART1_Read_Text(uart_input,";", 9);
              UART1_Write_Text(uart_input);
              strcpy(uar, uart_input);
        }
          
        else{
            UART1_Write_Text(uar);   
        }
   }
}
Regards,
kumar

paulfjujo
Posts: 1557
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#6 Post by paulfjujo » 03 May 2024 18:21

hello,


UART can be blocked if OVERFLOW occurs !!! and error not treated !
when adding a delay , if data are always send by your terminal, during this laps of time
.. MCU itslef can't store incomming data anymore -> OVERFLOW

i don't know how MikroE manage the function UART1_Read_Text(uart_input,";", 9);
.....waiting until encouter the string=";" or 9 char into the buffer
string ";" or charactere ';'
string ";" => 2 bytes => ';' + zero value !
char >= ony 1 byte ';'

so, wait a MikroE solution

or use interrupt !

kumar123
Posts: 77
Joined: 17 Oct 2023 07:32

Re: UART1_Read_Text() is not working when I used escape sequences like \r\n

#7 Post by kumar123 » 04 May 2024 08:15

Hello paulfjujo,
In that case, Can we say this is the issue of mikroE end or UART1_Write_Text() ?

Regards,
Kumar

Post Reply

Return to “mikroC PRO for PIC General”