|
|
View previous topic :: View next topic |
Author |
Message |
Bill Legge
Joined: 07 Sep 2010 Posts: 24 Location: West Australia
|
RS232 and TIMEOUT |
Posted: Tue Jul 05, 2011 4:35 am |
|
|
I'm trying to get some code to work:
1. Read a string from the RS232 UART.
2. Use TIMEOUT to keep going if there is no input.
The following code works OK without TIMEOUT but not as below:
Code: |
/////////////////////////////////////////////////////////////////////////////
// Project: 8722 RS232_SPP_01\main.c //
// Author: WVL //
// Date: July 2011 //
// Outline: Test comms with gets and TIMEOUT: //
// 10Mhz Xtal run at 40Mhz using PLL //
/////////////////////////////////////////////////////////////////////////////
#include <main.h>
#include <lcd420_flex.c>
#use rs232(UART1,BAUD=9600,TIMEOUT=1000)// default, 8_bits, no parity
#define HEARTBEAT PIN_H0 // LED on futurlec board
void main(){
lcd_init();
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_low_volt_detect(FALSE);
printf(lcd_putc, "\f18F8722 RS232_SPP_01");
delay_ms(100);
char mystring[20]="A TEST";
while(1){
printf("Type a string\r\n"); // hang till input unless TIMEOUT used
gets(mystring); // gets reads till a RETURN(value 13)
if(RS232_ERRORS==0)
{
printf("TIMEOUT ERROR\r\n");
}
printf("You typed:%s\r\n",mystring);
output_toggle(Heartbeat);
delay_ms(200);
} // while(1)
} //main
|
I am using a terminal emulator to test this - the emulator does append a CR to the typed string.
Some advice please
Regards Bill Legge _________________ Denmark in West Australia
'Where the forest meets the sea' |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Jul 05, 2011 5:00 am |
|
|
You need to test for the character being 0 as well. RS232_ERRORS, can be zero from other reasons.
Gets, is a 'wrapper' function to getc, and waits for CR, so will keep waiting if a timeout occurs. Look at the 'get_string' function in input.c, and then add a test to this:
Code: |
//after the c=getc();
if (c==0 && RS232_ERRORS==0) break;
|
Then if there is a timeout, the string will still be null terminated, but will just stop where the timeout triggered.
If you want to know that this happened, make the get_string function return a value, and set this true/false according to the exit reason.
Best Wishes |
|
|
Bill Legge
Joined: 07 Sep 2010 Posts: 24 Location: West Australia
|
TIMEOUT |
Posted: Tue Jul 05, 2011 4:55 pm |
|
|
Thanks Ttelmah,
I'm working on using the get_string function in input.c as you suggested but when I compile it I get these error messages:
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 17(16,17): Undefined identifier -- getch
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 19(8,9): Undefined identifier -- putchar
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 45(12,13): Undefined identifier -- getch
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 49(15,16): Undefined identifier -- putchar
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 50(15,16): Undefined identifier -- putchar
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 51(15,16): Undefined identifier -- putchar
*** Error 12 "C:\Program Files\PICC\PICC WVL Code\PIC18\8722 RS232_SPP_01\input.c" Line 56(14,15): Undefined identifier -- putchar
7 Errors, 0 Warnings.
The line numbers given do not contain the offending code but only the getc and putc commands - not getch and putch as listed in the errors?
I have included the '#include <ctype.h>' and #include <stdlib.h>
Why does the error message refer to code that does not appear in the line?
Regards Bill Legge _________________ Denmark in West Australia
'Where the forest meets the sea' |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Jul 06, 2011 1:53 am |
|
|
I'd guess you are compiling from MPLAB, and have added input.c, to the list of files to compile. Don't.....
In MPLAB, just have your main program.
Then include things like input.c, in this program.
Problem is that input.c, is not a stand-alone program. It _requires_ the main code, before it can work. In fact CCS (and quite a few other people on the web', are really being 'naughty', in using the .c postfix for routines like this. The sequence should be:
.c - compilable C program.
.h - file to be included that is not compilable in it's own right
MPLAB, if it sees a .c file in the program list, _will_ attempt to compile it. Hence the errors.....
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|