View previous topic :: View next topic |
Author |
Message |
nehallove
Joined: 16 Jan 2008 Posts: 61
|
gets function for receiving uart data doesn't work |
Posted: Tue Sep 11, 2012 1:44 pm |
|
|
I am using PIC16f1937 and I am using UART. getc() and putc() works. But gets(string) doesn't work.
Code: |
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main(){
char string[30];
setup_oscillator(OSC_16MHZ);
while(1){
gets(string);
puts(string);
}
} |
_________________ nehal |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19563
|
|
Posted: Tue Sep 11, 2012 2:09 pm |
|
|
Big difference is you need to send a line feed before gets will return.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9254 Location: Greensville,Ontario
|
|
Posted: Tue Sep 11, 2012 2:35 pm |
|
|
also add 'errors' to the use rs232(...) statement to avoid UART 'lockup'. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Sep 11, 2012 5:11 pm |
|
|
Let me politely add, that i never have used gets() as it makes it too easy to hang the program.
The ONLY TWO receive functions i ever use on the USART are:
KBHIT, and when it is TRUE.
mybyte=getc();
and follow that -- process and loop while KBHIT stays true.
I build a string buffer as i go, ignoring ASC 10 and replacing ASC 13 with ASC 0 in the recovered string data.
By putting a KBHIT, conditional block in main() - you can run other processes instead of stall on the 'deadwait' that the pitiful (and really useless) GETS() imposes. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Tue Sep 11, 2012 6:58 pm |
|
|
Many fail to recognize that rs232 is asynchronous...yes it is never ever synchronized to your code unless you use flow control or if you have a very short isr linked to the RDA interrupt. Never never put delay_xx or puts or printf into an isr and expect a pleasant result.
The code below might work sometimes and sometimes not. The reason is while the puts is sending data the gets is blocked. If you are typing at a keyboard and stop to view the puts data before typing the next line you have created human flow control and it has a good chance of working. Otherwise just hope it's your lucky day.
Code: |
while(1)
{
gets(string);
puts(string);
}
|
|
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Mon Sep 17, 2012 5:50 pm |
|
|
Thank you guys. _________________ nehal |
|
|
|