CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

weird problem uart getc and fgets.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

weird problem uart getc and fgets.
PostPosted: Thu Aug 08, 2013 6:39 am     Reply with quote

I have got a microcontroller with two hardware uarts, i am trying to echo the gps feed to pc via the controller.

Code:

WHILE (TRUE){
   t= getc(gps);
   putc(t,pc);
}

Works fine, and i get three lines of gps strings every few ms.

but when i change the code to this:
Code:

WHILE (TRUE){
   fgets(array,gps);
   fprintf(pc,array);
}


all i get is garbage...
Why this weird behavior?
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu Aug 08, 2013 7:58 am     Reply with quote

Multiple things wrong/possibly wrong.....

First how big is 'array'?.
Second, remember that there are only two characters of hardware buffering. With the second code, the chip will be sending data from array for several mSec (depends how long the string is). So what happens to anything that arrives on gps in this time?.... This is the point about using interrupt driven receive.
Then fprintf, really does require a format string (printf is documented to work without one, but fprinf isn't).

Best Wishes
haxan7



Joined: 27 Jul 2013
Posts: 79

View user's profile Send private message

PostPosted: Thu Aug 08, 2013 8:52 am     Reply with quote

Ttelmah wrote:

First how big is 'array'?.


Array is 500 characters and string from gps is 250 char max.

if i use interrupt driven receive, would the controller be able to receive from gps and transmit to pc (on external interrupt) at the same time?
i am using 16mhz/9600 baudrate.
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu Aug 08, 2013 9:15 am     Reply with quote

Yes.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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