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

#use rs232 & printf mystery

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



Joined: 05 Nov 2010
Posts: 42
Location: Sweden

View user's profile Send private message Visit poster's website

#use rs232 & printf mystery
PostPosted: Fri Nov 05, 2010 11:29 am     Reply with quote

Hi gurus,

Been tinkering with the RS485 examples
and.. ehh.. have a question because it works??


(Examples includes 2 serial comms, one with terminal/PC and one with
other units on RS485 multidrop network)

Comm with PC is set up in main file with "#use rs232(... stream=PC)"

After that "RS485.c" is included which defines similar "#use rs232(... stream=RS485)"


So far so good.

Rx/Tx via RS485 is done using "fputc(xx, RS485)" as expected,
but when sending prompts to the PC, a normal "printf()" is used!
(i.e. NO "fprintf(PC, ...)" as I´m using)


The only way I see how this can work, is that you rely on the compiler to put the "#use rs232(...)" from the included
file BEFORE the same command in main file.
(According to doc´s, "printf()" uses the latest "use-statement" for it´s output.)

Am I right or have I missed something crucial here??


By the way, RS485 is working excellent - much thanks to all 9 pages of topics posted here on the subject.. Laughing


Regards:


/Björn
PCM 4.112 on 16F690
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Nov 05, 2010 12:43 pm     Reply with quote

The "#use rs232(...)" directives for each comm must include the assigned Tx/Rx pins AND the STREAM parameter
to redirect the printf output.

Defining:
#use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7,enable=PIN_C5,stream=RS485)
it doesn't matter if it is in an included file

In the header of your main.C
#use rs232 (baud=9600,xmit=PIN_B1,rcv=PIN_B0,stream=PC)

To transmit to the network:
fputc('A', RS485);
To transmit to the PC:
fprintf(PC, "Tx to PC");

Regards.
bdeb



Joined: 05 Nov 2010
Posts: 42
Location: Sweden

View user's profile Send private message Visit poster's website

PostPosted: Fri Nov 05, 2010 3:09 pm     Reply with quote

Hi Humberto,

I totally agree! My question was why it still works if you *don´t* do like that.

Pins are of course declared in real "#use-statements" - not included
in my text for two reasons;

They are not the issue + I don´t know how strict policy is about
not posting code from examples.

Thanks!


/Björn
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Fri Nov 05, 2010 3:41 pm     Reply with quote

Printf, or putc, always send their output to the _last_ RS232 definition.
So if you declare the RS485 stream first, you can do a putc/printf, without using the stream commands, and talk to the other stream.

Best Wishes
bdeb



Joined: 05 Nov 2010
Posts: 42
Location: Sweden

View user's profile Send private message Visit poster's website

PostPosted: Fri Nov 05, 2010 4:52 pm     Reply with quote

Hi Ttelmah,

OK, I assume you agree with my theory on why this example works.

Sorry for asking about these finnicky details, but since I use
the MPLAB IDE, I have to put all my driver code in .h files which make
things a bit messy and also depending on the compilers good will. Smile
(Linker only working if you use CCS IDE, right?)

Checking the list file is OK, but understanding is better..


Thanks!


/Björn
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