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

How to know if I'm using serial com?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

How to know if I'm using serial com?
PostPosted: Wed Jan 29, 2020 11:55 am     Reply with quote

Hi!
I have created a library for my own usage. There i place very common functions i use often.
One of this functions send some information through serial port.

I got used to using conditional compiling to "enable/disable" parts of the code.
In this time i need to disable this part of the code when I'm using the library but the main program is not using RS232 communication. Otherwise, if i leave the code, then i get an error saying that i should use #use RS232 first. Even when I'm not using this function in the program.

So i need some way to verify if the program is using RS232, so this function is enabled by conditional compiling or not.

I couldn't find any way to achieve this. If i just could know if some constant is defined only when RS232 is used...

I DON'T WANT to create my own DEBUG constant to enable this. Hope there is already one.

Thanks for your help!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jan 29, 2020 12:53 pm     Reply with quote

RS232_ERRORS is created when you use a UART.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: How to know if I'm using serial com?
PostPosted: Wed Jan 29, 2020 4:21 pm     Reply with quote

Marttyn wrote:

So i need some way to verify if the program is using RS232, so this
function is enabled by conditional compiling or not.

I couldn't find any way to achieve this. If i just could know if some
constant is defined only when RS232 is used...

What do you mean by "when RS232 is used" ?

Do you mean:

1. You want a way to tell at runtime, if your program has a
#use rs232() statement in it ?

or

2. You want a way to tell at runtime, if your program has putc(), getc(),
or printf() in it ?
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 29, 2020 6:39 pm     Reply with quote

Adding on to PCM P's questions....

3)What about a 'conditional' print ? IE...main() never prints except for an ERROR message ? Say the program is a greenhouse controller, all is well until it gets real cold, that triggers a message to be sent, using serial to PC. As long as the greenhouse is warm, no serial transissions, so PRINTs are never used.

We really need a small example to better understand what you need to accomplish. I know English isn't everyone's best language, I've been fighting with it for 66 years !!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jan 29, 2020 11:23 pm     Reply with quote

Agreed.
I must admit I read it that he wanted to know if the RS232
declarations were present, not if the I/O was actually called.
Strikes that if he is adding a debug include, then this should simply
have the RS232 declarations inside it.
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Thu Jan 30, 2020 3:42 am     Reply with quote

Thanks for your replies!

I want to know if #use RS232 was used, at compile time, not at runtime.

Lets imagine this:

Code:

int Add(int a, int b){
long result = a + b;

#ifdef RS232
   //only print this to COM port if its in use
   printf("%u + %u = %Lu", a, b, result);
#endif

   return (result);
}


So, when COM port is open, it will output the printf and also the internal result. But if its not, then only the result.
Of course, "RS232" is not a real define... (i think).
What i need is a define created by the compiler only when #use RS232 is used.

I could use this instead:

Code:

#define DEBUG

#ifdef DEBUG
#use rs232(uart1, baud=115200)
#endif

int Add(int a, int b){
long result = a + b;

#ifdef DEBUG
   //only print this to COM port if its in use
   printf("%u + %u = %Lu", a, b, result);
#endif

   return (result);
}


But i just don't want to have extra defines, or additional coding. Just use the functions when i need them, without other worries.
There must be a way to find out if serial is open. Compiler should have some available define...
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 4:15 am     Reply with quote

At the end of the day, just use a #define. That is what it is for...

Simple answer is 'no'. Reason is that the code does not exist at the
pre-compile stage where things like #if apply.
The compilation is a layered structure. The macro definitions and tests
are done _before_ the code is actually compiled, so at this point there
is no ability to test what is going to happen 'later' during the compile.

#USE RS232, is a compiler directive applied at compile time, not a
pre processor directive.
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Thu Jan 30, 2020 4:28 am     Reply with quote

Make sense i guess...
I thought that while at precompile time, if "#use RS232" was found, maybe a #define was created, or some flag was set, that could be checked still at precompile time.

header.h
Code:

#define IM_USING_RS232

#use rs232(uart1, baud=115200)


library.c
Code:
int Add(int a, int b){
long result = a + b;

#ifdef IM_USING_RS232
   //only print this to COM port if its in use
   printf("%u + %u = %Lu", a, b, result);
#endif

   return (result);
}


Looks redundant to me.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 4:31 am     Reply with quote

You can go one step 'further':

Put a '#ifdef IM_USING_RS232' before the #use rs232, then you
only have to make the single change of defining this to automatically
add the #use rs232, and the code that uses it.
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Thu Jan 30, 2020 4:38 am     Reply with quote

Ttelmah wrote:
You can go one step 'further':

Put a '#ifdef IM_USING_RS232' before the #use rs232, then you
only have to make the single change of defining this to automatically
add the #use rs232, and the code that uses it.


Keep in mind that my library is one for all. That means that, some programs use some of the included functions, and other programs use others.

When a program calls the add(a, b) function, this program may use, and others may not use the RS232.

So adding a extra #ifdef as you suggest, would just need additional changes to the main program, and its really not needed. As the program that is already using RS232 should not "not compile" that depending on the IM_USING_RS232 define.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 4:55 am     Reply with quote

I believe I found a way to do it.

Compile this code, with and without commenting the #use rs232() line.
Then look at the compiler build messages in the MPLAB output window.
You will get a warning message, and it will tell you if the #use rs232() line
is there or not. Also, in main(), it will put in printf() if #use rs232() is there.
Code:

#include <16F1847.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)


// This will show it in the Build window at compile-time.
#if definedinc(RS232_ERRORS)
#warning We have RS232
#else
#warning We don't have it.
#endif

//=====================================
void main()
{

// This will put printf code into the program
// if the #use rs232() statement is there.
#if(definedinc(RS232_ERRORS))
printf("Hello there\n\r");
#endif

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 5:02 am     Reply with quote

It will only work if you have 'errors' in your RS232 declaration. Now you
should always have this, but 'caveat'.
I had actually tried exactly this earlier, but on a PIC24, was not getting
the nice simple behaviour required.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 5:11 am     Reply with quote

I agree.

I made a refined version. This one acts like the O.P. actually wanted.
Code:

#include <16F1847.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)

#define RS232 definedinc(RS232_ERRORS)

// This will show it in the Build window at compile-time.
#if RS232
#warning We have serial.
#else
#warning We don't have it.
#endif

//=====================================
void main()
{

// This will put printf code into the program
// if the #use rs232() statement is there.
#if(RS232)
printf("Hello there\r");
#endif

while(TRUE);
}
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Thu Jan 30, 2020 5:26 am     Reply with quote

Great! A nice approach!
But, to avoid to use ERRORS check this:
Code:
#if definedinc(STDOUT)

The first two simple test ive made worked fine... But dont know if it will work on all the circumstances...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 30, 2020 5:40 am     Reply with quote

I added your refinement and it works in this test program:
Code:
#include <16F1847.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)

#define RS232 definedinc(STDOUT)

// This will show it in the Build window at compile-time.
#if RS232
#warning We have serial.
#else
#warning We don't have serial.
#endif

//=====================================
void main()
{

// This will put printf code into the program
// if the #use rs232() statement is there.
#if(RS232)
printf("Hello there\r");
#endif

while(TRUE);
}

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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