View previous topic :: View next topic |
Author |
Message |
Guest
|
Long serial timeout |
Posted: Fri Jun 22, 2007 3:37 am |
|
|
Hi,
Have done many searches but couldn't find answer I was looking for.
PIC is 16LF877 4Mhz. Compiler. 3.181
I am using the code below to read in a serial string.
This works fine.
What I want to to be able to time out if nothing is received for about 10 seconds.
All other timeout routines seem to be too short.
Thanks in advance
Steve
Code: | #include "C:\PIC_C_Code\Smart Modem\Smart Modem.h"
#include <string.h>
void Get_String2();
void get_string(char* s, int max);
#define MAX_BUFFER 10
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Get_Char();
Get_String2();
return;
}
void Get_String2()
{
char string[MAX_BUFFER];
while(1)
{
printf("please type a string: ");
// Wait for a key press
while(!kbhit(MODEM));
get_string(string, MAX_BUFFER);
fprintf(RDTG,"%s",string);
}
}
void get_string(char* s, int max)
{
int len;
char c;
--max;
len=0;
do {
c=fgetc(MODEM);
// if(c==8) { // Backspace
// if(len>0) {
// len--;
// putc(c);
// putc(' ');
// putc(c);
// }
// } else
if ((c>=' ')&&(c<='~'))
if(len<max) {
s[len++]=c;
putc(c);
}
} while(c!=13);
s[len]=0;
} |
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sat Jun 23, 2007 9:56 pm |
|
|
V3.181 is quite old, I´m not sure if in the Examples folder you have the EX_TGETC.C
program that shows exactly what you need.
Humberto |
|
|
Guest
|
|
Posted: Thu Jun 28, 2007 2:28 am |
|
|
Many Thanks Humberto.
I do have that piece of code.
Was reluctant to use it as I didn't think it would be possible to achieve the 10 seconds I would like?
Is it possible?
(I will play in the meantime)
Thanks again
Steve |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Jun 28, 2007 6:56 am |
|
|
Steve,
With the pointed example, I wish you get the way, how to do it. The program
was used widely and I´m not aware of something wrong using it. Of course you
will need to adapt it to your 10 sec timeout application.
Humberto |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Thu Jun 28, 2007 7:29 am |
|
|
Setup a timer to interrupt every 1ms and count to 10000
Code: |
voidt main( void )
{
unsigned int16 timeOutCounter = 0;
set_rtcc( SomeValue );
while( !kbhit( MODEM ) && ( timeOutCounter < 10000 ) );
if ( timeOut Counter > 10000 ) }
timeOutCounter = 0;
///timout code goes here
}
else {
//kbhit code goes here
}
#pragma INT_RTCC
void ms_timer( void )
{
set_rtcc( SomeValue );
timeOutCounter++
}
|
|
|
|
Guest
|
|
Posted: Thu Jun 28, 2007 8:36 am |
|
|
Getting there. Thanks to all.
Just adapting the example code to suit works fine. |
|
|
newbie_1974
Joined: 04 Jul 2007 Posts: 4
|
timeout |
Posted: Thu Jul 05, 2007 12:05 am |
|
|
hi, would it be possible to get a copy of your time out code, thanks. |
|
|
|