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 CCS Technical Support

Booleans and printf?

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



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

Booleans and printf?
PostPosted: Thu Sep 27, 2018 8:23 pm     Reply with quote

Hi All,

I’d like to send the state of two Boolean (int1) variables via printf, but there doesn’t seem to be a specific specifier for this purpose in CCS C? Other than encode the 2 variables into decimal value and then decode on the receiving end, is there another way?

Jack
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 27, 2018 9:27 pm     Reply with quote

If you want to send 0 or 1, then use %c with printf. But be aware that
printf will still send a byte. The byte will be 0x00 or 0x01. Example:
Code:

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

//======================================
void main()
{                                                                     
int1 temp;

temp = 1;

printf("%c", temp);

while(TRUE);
}
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Mon Oct 01, 2018 9:38 am     Reply with quote

Hi PCM,

Thanks for the input! That method didn't really work for me (PCH v5.050), but I'm not sure if it was the compiler, the Hyperterminal representation, or my VB application? When I attempted to send the boolean variables using '%c' and printf, the response seemed somewhat unpredictable. Sometimes it would print '0', sometimes a space ' ', and sometimes a smiley face.

I ended up doing this instead:

Code:

OutputStatus = 0;
if (bCoolingOn) OutputStatus += 1;
if (bHeatingOn) OutputStatus += 2;
printf(usb_cdc_putc, "!PID:P:%03d,%03d,%u,%03d,%03u\r", ProcessTemp, SetpointTemp, OutputStatus, Error, PWMCommand);


Jack
temtronic



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

View user's profile Send private message

PostPosted: Mon Oct 01, 2018 10:00 am     Reply with quote

just a comment..
Since you send a byte anyway...Why not just send an 'H' for Heating, On, 'C' for Cooling and '-' for off ? 'h' could mean 'heating mode but off, c for cooling mode but off...

Jay
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Mon Oct 01, 2018 12:12 pm     Reply with quote

temtronic wrote:
just a comment..
Since you send a byte anyway...Why not just send an 'H' for Heating, On, 'C' for Cooling and '-' for off ? 'h' could mean 'heating mode but off, c for cooling mode but off...

Jay


Big +1 from me regarding this approach. Very explicit. No ambiguity.
temtronic



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

View user's profile Send private message

PostPosted: Mon Oct 01, 2018 12:25 pm     Reply with quote

yeah the older you get, the easier you need computers to tell you 'stuff'. I've yet to figure out how to get Win7 to 'bkp/restore' like XP (like pick a day from the calendar'...

sigh..
Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Oct 01, 2018 12:43 pm     Reply with quote

and (of course) it is worth remembering that in C you can give an output character from a boolean with the syntax:

(temp==TRUE)?'H':'C'

which will return the character 'H' or 'C' according to whether 'temp' is TRUE or FALSE.
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