|
|
View previous topic :: View next topic |
Author |
Message |
Scottl
Joined: 21 Feb 2005 Posts: 25
|
Using fputs |
Posted: Wed Oct 24, 2007 6:35 pm |
|
|
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
|
|
Posted: Thu Oct 25, 2007 3:01 am |
|
|
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
|
|
Posted: Thu Oct 25, 2007 5:10 pm |
|
|
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
|
|
Posted: Fri Oct 26, 2007 2:09 am |
|
|
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
|
|
Posted: Fri Oct 26, 2007 8:07 pm |
|
|
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
|
|
Posted: Sun Oct 28, 2007 7:43 am |
|
|
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
|
|
Posted: Sun Oct 28, 2007 5:43 pm |
|
|
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
|
|
Posted: Mon Oct 29, 2007 5:20 am |
|
|
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
|
|
Posted: Mon Oct 29, 2007 7:35 am |
|
|
Got it!
Thanks for all your help! |
|
|
|
|
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
|