View previous topic :: View next topic |
Author |
Message |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
Booleans and printf? |
Posted: Thu Sep 27, 2018 8:23 pm |
|
|
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
|
|
Posted: Thu Sep 27, 2018 9:27 pm |
|
|
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
|
|
Posted: Mon Oct 01, 2018 9:38 am |
|
|
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
|
|
Posted: Mon Oct 01, 2018 10:00 am |
|
|
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
|
|
Posted: Mon Oct 01, 2018 12:12 pm |
|
|
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
|
|
Posted: Mon Oct 01, 2018 12:25 pm |
|
|
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
|
|
Posted: Mon Oct 01, 2018 12:43 pm |
|
|
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. |
|
|
|