UART1_Read_Text

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

UART1_Read_Text

#1 Post by Pesticida » 19 Oct 2011 12:29

Hello,

I have a Question ,I'm using the UART1_Read_Text Syntax like here:

Code: Select all

UART1_Read_Text(output, "*!", 10);    // reads text until '*!' is found

And I have entered a to short string,the Source code is blocking and naturally the Watchdog reset the MCU!while is going in a endless loop and waiting for "*!"

How can I make a Timeout routine to break the UART1_Read_Text if the user have entered the wrong string?!


Thanks for any Help!


Regards Pesti

coskunkazma
Posts: 34
Joined: 30 Aug 2011 07:03

Re: UART1_Read_Text

#2 Post by coskunkazma » 19 Oct 2011 13:20

Pesticida wrote:Hello,

I have a Question ,I'm using the UART1_Read_Text Syntax like here:

Code: Select all

UART1_Read_Text(output, "*!", 10);    // reads text until '*!' is found

And I have entered a to short string,the Source code is blocking and naturally the Watchdog reset the MCU!while is going in a endless loop and waiting for "*!"

How can I make a Timeout routine to break the UART1_Read_Text if the user have entered the wrong string?!


Thanks for any Help!


Regards Pesti

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: UART1_Read_Text

#3 Post by Sparky1039 » 19 Oct 2011 16:15

One method would be to use the UARTx_Read and the UARTx_Data_Ready commands to monitor if an incoming byte has been received. This requires that you test each incoming byte to see if it matches the characters you are looking for. This is still a "blocking function" but it is more flexible in dealing with the WDT.

Code: Select all

unsigned char testString [2] = {'*', '!'};
unsigned char i = 0;
unsigned char receive = 0;

do
{
if (UART1_Data_Ready() != 1) asm clrwdt;  // loop until byte is received, clear WDT

receive = UART1_Read();  // read imcoming byte

if (receive == testString[i]) i++; // test for character match. if true index array for next character compare.
else i = 0;  // if false, reset array index and try again.

} while (i != 2); // loop until all chars found, exit 
The most efficient way to do this would be to utilize UART interrupts and service each incoming byte as it arrives allowing you to perform other tasks while waiting (including WDT clearing). Doing so prevents the search routine from becoming a blocking function.

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: UART1_Read_Text

#4 Post by Pesticida » 20 Oct 2011 06:54

Hello,

Thanks Sparky ! I will try your suggestion.

Regards Pesti

Post Reply

Return to “mikroC PRO for PIC General”