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

Using fputs

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



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

Using fputs
PostPosted: Wed Oct 24, 2007 6:35 pm     Reply with quote

I am trying to send a string using fputs. Can someone tell me if I have this right? Or will it work?

Code:


      sprintf(b,"%s","0x450x110x0D");
      fputs(b,COM_B);  // Send Data
      fgets(b,COM_B);  // Receive Response
      fprintf(COM_A, "%s", b);



Thanks in advance,

Scott
Ttelmah
Guest







PostPosted: Thu Oct 25, 2007 3:01 am     Reply with quote

Why?....
Just use:

sprintf(b,"0x450x110x0D");

Or:

strcpy(b,"0x450x110x0D");

Sprintf, is designed for where you want to combine multiple things into one resulting string. Using it as you originally show, is a 'sledgehammer/nut' type solution, and while it may work on latter compilers, is not likely to on older versions, since it is designed to receive string variables, and in CCS, a constant string (what you get when you declare a string in inverted commas), is not a string variable.
The strcpy solution is the 'best' way of doing this.

Best Wishes
Scottl



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Thu Oct 25, 2007 5:10 pm     Reply with quote

OK here is all my code!

I am not getting this! Either I have a hardware issue or it is not going to work.

Can anyone tell me why the following is not going to work? I am pulling my hair out what's left of it!

683.h
Code:

#include <12F683.h>
#device adc=8
#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT, NOIESO, NOFCMEN
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A1,rcv=PIN_A0,bits=8,STREAM=COM_A)
#use rs232(baud=9600,parity=N,xmit=PIN_A4,rcv=PIN_A5,bits=8,STREAM=COM_B)
#rom 0x7FF = {0x3400}



Code:


#include "C:\Program Files\PICC\Examples\683.h"
#include <string.h>

   char string_a[5];
   char button2[5];
   char button1[5];

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
while(1)
{

   fgets(string_a,COM_A);
   strcpy(button2, "EDC1");
   strcpy(button1, "EDC3");
 
   if(string_a == button1)
   {
      fprintf(COM_A,"button1");
   }

   if(string_a == button2)
   {
      fprintf(COM_A,"button2");
   }
   
   if((string_a != button1) || (string_a != button2))
   {
      fprintf(COM_A,"error 2");
   }
}
}



Thanks to all that replies,

Scott
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Oct 26, 2007 2:09 am     Reply with quote

OK , first of all you can't do string comparisons like that. What you are doing is comparing 2 pointers NOT the strings they contain. You need to do the following.

fgets(string_a,COM_A);

if(strcmp(string_a, "EDC1") == 0) {
fprintf(COM_A,"button1");
} else if(strcmp(string_a, "EDC3") == 0) {
fprintf(COM_A,"button2");
} else {
fprintf(COM_A,"error 2");
}

Not sure about the rest.
Scottl



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Fri Oct 26, 2007 8:07 pm     Reply with quote

Hi Wayne_,

I have tried to use the following but get an error in bold:

if(strcmp(string_a, "EDC1") == 0) {

error: Bad expression syntax

I am using IDE version 3.43
PCB Version 3.205
PCM Version 3.205
PCH Version 3.205

Thanks for your help,

Scott
Ttelmah
Guest







PostPosted: Sun Oct 28, 2007 7:43 am     Reply with quote

Try reading the manual.
Note what the strings are allowed to be (not constants...).
I'm afraid you have to use:
Code:

char compare_string[8];


fgets(string_a,COM_A);
strcpy(compare_string,"EDC1");
if(strcmp(string_a, compare_string) == 0) {

etc..

Best Wishes
Scottl



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Sun Oct 28, 2007 5:43 pm     Reply with quote

Could I not use the following:

static char thermo[] = "EDC1";

then

if(strcmp(string_a, thermo)==0){
etc....................

or would strcpy be the best approach?

Scott
Ttelmah
Guest







PostPosted: Mon Oct 29, 2007 5:20 am     Reply with quote

You can. You don't even need the 'static'. However the downside is the amount of memory involved. If you declare a variable, and intialise it to a constant value, then RAM is allocated for the whole variable, and a ROM copy is built as well. If (for example, you have 30*8 character 'test' strings, then 270 characters of RAM are being used (remember there is an extra 'null' character to mark the end of the string). If you strcpy the strings as you need them, you only need one RAM buffer, no matter how many test stings are used. However the downside is that the strings have to be copied every time they are used.

Best Wishes
Scottl



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Mon Oct 29, 2007 7:35 am     Reply with quote

Got it!

Thanks for all your help!
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