|
|
View previous topic :: View next topic |
Author |
Message |
picprogrammer
Joined: 10 Sep 2003 Posts: 35
|
Change of uart |
Posted: Wed Sep 25, 2013 7:27 am |
|
|
My project uses UART1 with a command line interface. Now I want to use multiple pcb on a RS485 bus on UART2. The idea is to select 1 print on the RS485 bus and talk only to that one.
Is it possible to switch UART stream? The manual call if printf is used, the last #use RS232.. is active. Is this possible in the code? I have a lot of printf in my program so it would take a lot of code to fprintf for the 2 separate. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 25, 2013 8:14 am |
|
|
Encapsulate....
replace 'printf', with:
printf(my_putc, what you want to print);
You can do a simple 'search and replace' for the "printf(", and replace this with "printf(my_putc", throughout the code.
Then just create a function 'my_putc', like:
Code: |
int1 use_first_stream=TRUE;
void my_putc(int8 chr)
{
if (use_first_stream)
fputc(STREAM1,chr);
else
fputc(STREAM2,chr);
}
|
This way there is just one pair of fputc calls, with a single flag 'deciding' what stream is to be used, and you can just change 'use_first_stream' to TRUE/FALSE, to select the stream to be used.
The printf functions, instead of all calling putc, will just call the new 'decider' (my_putc) function.
Quick/easy to do, and adds the least possible code.
Best Wishes |
|
|
picprogrammer
Joined: 10 Sep 2003 Posts: 35
|
|
Posted: Wed Sep 25, 2013 8:56 am |
|
|
Yes, but like that I have to double all +/-50 printf's with a lot of float printing. The code will increase a lot.
It would be handy if STREAM1 was a variable!
Sorry, did'nt read you measage. Thanks will try! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 25, 2013 9:31 am |
|
|
No you don't. You are missing the point...
Code: |
//Two #use RS232's here, setting up the streams STREAM1, and STREAM2
int1 use_first_stream=TRUE;
void my_putc(int8 chr)
{
if (use_first_stream)
fputc(STREAM1,chr);
else
fputc(STREAM2,chr);
}
void main(void)
{
float value=12345.2;
int16 valuei=234;
do
{
printf(my_putc, "Something complex %f, %ld\n\r",value,valuei);
use_first_stream~=1;
value+=1;
valuei+=1;
}
while (TRUE);
}
|
This will alternately print to STREAM1, then STREAM2, from the single printf as the 'use_first_stream' value changes.
Exactly what you want.
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
|