caramoan tour package

caramoan tour package

Author Topic: string array comparison in CCS C... help!  (Read 2630 times)

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
string array comparison in CCS C... help!
« on: September 10, 2008, 11:27:30 PM »
may ginawa akong test program. nagpiprint sya sa kabilang serial port ng kung ano man ang ilagay mo sa isang serial port.
eto ang program:
Code: [Select]
/*
This program prints charaters to ser2 that are being feed to ser1.
string should start with 'C' and ends with return (0x0D).
ex. at ser1: Cmarcelino pogi mo! mwah!(enter)
    at ser2 prints:
            marcelino pogi mo! mwah!
           
sting array however is limited to 30 characters only.
*/

#include <18F4520.h>
#device adc=10
#FUSES NOWDT,HS,NOPUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=ser1)
#use rs232(baud=9600,parity=N,xmit=PIN_D1,rcv=PIN_D0,bits=8,stream=ser2)

#include <stdio.h>
#include <stdlib.h>

char c;
char string[30];
int data = FALSE;

#int_RDA
void  RDA_isr(void) {
   c=getc(ser1);
   data = TRUE;
}

void get(){
   int i = 0;
   int k = 0;
   do {
      while (data == FALSE);
      data = FALSE;
      string[i++]=c;
   }while (c!=0x0D);                      //string must stop at 0x0d(return or enter)
   for (k=0;k<30;k++){
      fprintf(ser2,"%c",string[k]);       //prints to ser2
   }
   for(i=0;i<30;i++){                     //clears array
      string[i] = 0;
   }
}

void main()
{

//   setup_adc_ports(AN0_TO_AN3|VSS_VDD);
//   setup_adc(ADC_CLOCK_DIV_4|ADC_TAD_MUL_8);

   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   while(true){
      if (data){
         data = FALSE;
         if (c=='C') get();        //string being inputted should start with 'C'
      }
   /*   
     ...................
   */
   }
}
//reference: http://www.ccsinfo.com/forum/viewtopic.php?t=34712&postdays=0&postorder=asc&start=15

eto nga din ang testing nyan sa proteus:


however, di yan actually ang gusto kong mangyari... gusto ko sana, kung may marecieve sa ser1 ng isang string may katumbas din na ifefeed sa kabila.
example:
ser1 receives: "gather", magpiprint naman sa ser2 ng kunyari "get".
ang problem ko actually ay kung pano ko mababasa ang string array na nasave ko... at pagkatapos icompare yun as a whole string.
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

string array comparison in CCS C... help!
« on: September 10, 2008, 11:27:30 PM »

Offline rdpzycho

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 10728
  • Pogi/Ganda Points: 632
  • Gender: Male
  • Respect Begets Respect
    • rdpzycho
Re: string array comparison in CCS C... help!
« Reply #1 on: September 11, 2008, 12:31:29 AM »
i-store muna siya sa buffer or anything na pwede mag-compare.

in pseudocode:

a = serial1.input
if a = "gather"
serial2.output = "get"

kung mahahabang string na or hahanapin sa mahabang string 'yung word kailangan naman ng parsing.

‎"Divide each difficulty into as many parts as is feasible and necessary to resolve it."
- Rene Descartes

"For every difficult problem there is always a simple answer and most of them are wrong."
- Clayton Paul

Philippine Electronics Forum

Re: string array comparison in CCS C... help!
« Reply #1 on: September 11, 2008, 12:31:29 AM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #2 on: September 11, 2008, 12:50:19 AM »
i-store muna siya sa buffer or anything na pwede mag-compare.

in pseudocode:

a = serial1.input
if a = "gather"
serial2.output = "get"

kung mahahabang string na or hahanapin sa mahabang string 'yung word kailangan naman ng parsing.



pwede in C-code?
hehehe
naiintindihan ko naman ang psuedo kaso, di ko alam kung pano ang manipulation ng array or string itself.
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: string array comparison in CCS C... help!
« Reply #2 on: September 11, 2008, 12:50:19 AM »

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: string array comparison in CCS C... help!
« Reply #3 on: September 11, 2008, 07:57:48 AM »
A large buffer is needed for storage large enough to accept max characters up to '\n'

Example:
Code: [Select]
char buffer[100];

unsigned char get(void)
{
    char *ptr, c;
   
    ptr = buffer;
   
    while(1)
    {
        //wait for char
        while (!data)
            continue;
           
        c = getc(ser1);
       
        if (c=='C')
            ptr = buffer;                   //restart
        else if (c=='\n')
            break;                          //end of msg
           
        if (ptr >= buffer + (100 - 1))      //buffer overflow, quit with error
            return ERROR;
       
        *ptr++ = c;                         //save in buffer and increment pointer
       
    }
    *ptr = 0;
    return NO_ERROR;
}


void main()
{
    .
    .
    .
   
    while (1)
    {
        if (get() == NO_ERROR )
        {
            //do comparisons here
            if (strcmp(buffer, "gather") == 0)
                printf("get");
            else if (strcmp(buffer, "push") == 0)
                printf("pull");
            .
            .
            .
        }
        .
        .
        .
    }
}
The Cebuano Geek

Philippine Electronics Forum

Re: string array comparison in CCS C... help!
« Reply #3 on: September 11, 2008, 07:57:48 AM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #4 on: September 11, 2008, 03:35:25 PM »
@zer0wing, ginamit ko pa din yung serial interrupt, para maactivate yung variable na data...

kaya eto na yung adjustments ko, based sa example mo.
Code: [Select]
char buffer[100];
int data = FALSE;

#int_RDA
void  RDA_isr(void) {
   data = TRUE;                           //switch to ON
}

unsigned char get(void){
   char *ptr,c;
   int i = 0;

   while(true){
      while (!data)
         continue;
      c=getc(ser1);
      if (c=='C')

         ptr=buffer;
      else if (c==0x0D)
         break;
      if (ptr >= buffer +(100-1))
         return "ERROR";
            *ptr++ = c;
   }
   *ptr = 0;
   return "NO_ERROR";   
}

void main()
{
   int i;
   char *cmp0,*cmp1;
//   setup_adc_ports(AN0_TO_AN3|VSS_VDD);
//   setup_adc(ADC_CLOCK_DIV_4|ADC_TAD_MUL_8);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   while(true){
      if (data){
         data = FALSE;
         get();       
      }
      if (get()== "NO_ERROR"){
         strcpy(cmp0,"gather");
         strcpy(cmp1,"marcelino");
         if (strcmp(buffer,cmp0)==0)
            fprintf(ser2,"get\n\r");
         else if(strcmp(buffer,cmp1)==0)
            fprintf(ser2,"pogi\n\r");
         
      }
   }
}

nacocompile naman yan... pero di gumagana sa proteus... saan kaya ako nakakamali?
nga pala, yung strcmp pala, di pala yung pwede diretso gamitan ng constant... so gumamit pa ako ng strcpy para maiload ko sa isang variable.
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: string array comparison in CCS C... help!
« Reply #4 on: September 11, 2008, 03:35:25 PM »

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: string array comparison in CCS C... help!
« Reply #5 on: September 11, 2008, 03:59:59 PM »
There are pointer issues on your program.
Part of them is my mistake of not defined ERROR and NO_ERROR constants.
Its cmp1 and cmp0 that should have space to hold the copied strings.

Find below the code with corrections:
Code: [Select]
#define ERROR    -1
#define NO_ERROR    0

char buffer[100];
int data = FALSE;

#int_RDA
void  RDA_isr(void) {
   data = TRUE;                           //switch to ON
}

unsigned char get(void){
   char *ptr,c;
   int i = 0;

   while(true){
      while (!data)
         continue;
      c=getc(ser1);
      data = FALSE;   //clear data flag
      if (c=='C')
         ptr=buffer;
      else if (c==0x0D)
         break;
      if (ptr >= buffer +(100-1))
         return ERROR;
            *ptr++ = c;
   }
   *ptr = 0;
   return NO_ERROR;   
}

void main()
{
   int i;
//   char *cmp0,*cmp1;
   char cmp0[20], cmp1[20];
//   setup_adc_ports(AN0_TO_AN3|VSS_VDD);
//   setup_adc(ADC_CLOCK_DIV_4|ADC_TAD_MUL_8);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   while(true){
      if (data){
         data = FALSE;
         get();       
      }
      if (get() == NO_ERROR) {
         strcpy(cmp0,"gather");
         strcpy(cmp1,"marcelino");
         if (strcmp(buffer,cmp0)==0)
            fprintf(ser2,"get\n\r");
         else if(strcmp(buffer,cmp1)==0)
            fprintf(ser2,"pogi\n\r");
         
      }
   }
}

Yes, CCS does not support constant compare.
Replace strcmp with this:

Code: [Select]

char strcmp_constant(char *s1, const char *s2)
{
    char c1, c2;

    while(1)
    {
        c1 = *s1++;
        c2 = *s2++;

        if (c1 == 0 || c2 == 0)
            return 0;
   
        if (c1 == C2)
            continue;

        return c1 - c2;
    }
}

The Cebuano Geek

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #6 on: September 11, 2008, 05:05:12 PM »
Still, I am problems...

I carefully followed the corrections made to the program and I also used the strcmp_constant function. Still got no luck.
I removed the const in the line : char strcmp_constant(char *s1, const char *s2) because it hinders the compiling.

Sa tingin ko,  nagwowork naman yung pagcopy. Nagkakaroon nalang nang problem yung comapring, kasi di na nagrerespond ang printing to ser2.

I even increased the size of cmp0 and cmp1 to [100] thinking that it is also causing the comparison with buffer which have a size of [100].
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: string array comparison in CCS C... help!
« Reply #7 on: September 11, 2008, 05:15:55 PM »
Still, I am problems...

I carefully followed the corrections made to the program and I also used the strcmp_constant function. Still got no luck.
I removed the const in the line : char strcmp_constant(char *s1, const char *s2) because it hinders the compiling.

Sa tingin ko,  nagwowork naman yung pagcopy. Nagkakaroon nalang nang problem yung comapring, kasi di na nagrerespond ang printing to ser2.

I even increased the size of cmp0 and cmp1 to [100] thinking that it is also causing the comparison with buffer which have a size of [100].


Try printing also the buffer, maybe no chars are received.

But "const" directive should work on ANSI compilers.  This is why I hate CCS very much, its non-standard and non-portable.

I have CCS here and will try to compile it when time permits.
The Cebuano Geek

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: string array comparison in CCS C... help!
« Reply #8 on: September 11, 2008, 05:32:40 PM »
If you think than strcpy is the problem, another way to solve this is to pre-assign cmp0 and cmp1 with values.
Like this:
Code: [Select]
#define ERROR    -1
#define NO_ERROR    0

char buffer[100];
int data = FALSE;

#int_RDA
void  RDA_isr(void) {
   data = TRUE;                           //switch to ON
}

unsigned char get(void){
   char *ptr,c;
   int i = 0;

   while(true){
      while (!data)
         continue;
      c=getc(ser1);
      data = FALSE;   //clear data flag
      if (c=='C')
         ptr=buffer;
      else if (c==0x0D)
         break;
      if (ptr >= buffer +(100-1))
         return ERROR;
            *ptr++ = c;
   }
   *ptr = 0;
   return NO_ERROR;   
}

void main()
{
   int i;
//   char *cmp0,*cmp1;
// char cmp0[20], cmp1[20];

    char *cmp0 = "gather";
    char *cmp1 = "marcelino";

//   setup_adc_ports(AN0_TO_AN3|VSS_VDD);
//   setup_adc(ADC_CLOCK_DIV_4|ADC_TAD_MUL_8);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   while(true){
      if (data){
         data = FALSE;
         get();       
      }
      if (get() == NO_ERROR) {
         //strcpy(cmp0,"gather");
         //strcpy(cmp1,"marcelino");
         if (strcmp(buffer,cmp0)==0)
            fprintf(ser2,"get\n\r");
         else if(strcmp(buffer,cmp1)==0)
            fprintf(ser2,"pogi\n\r");
         
      }
   }
}
The Cebuano Geek

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #9 on: September 11, 2008, 06:10:07 PM »
Thank you for the effort and time.

I tried to print to ser2 the buffer after executing get();, but it won't print...

Is it the concept of reading a string array is by capturing each array? This is what I thought that is why in my initial program, I actually printed a character each cycle.
for (k=0;k<30;k++){
     fprintf(ser2,"%c",string[k]);       
}
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #10 on: September 15, 2008, 08:25:31 PM »
I am still having problem with my code... di ko nga naitatransfer sa main() ang string na narerecieve ko...

HELP!!!
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline Kaizer03

  • Nuclear Reactor
  • ****
  • Posts: 4847
  • Pogi/Ganda Points: 225
  • C#<-->Android<-->Java
    • PhilRobotics
Re: string array comparison in CCS C... help!
« Reply #11 on: April 28, 2010, 03:12:00 PM »
tita marce,

nasolve mo po ba ito?

hehe ^_^ ang problem ko naman dito

pag pre-init ang string ok.. pero once nareinitialize mo sya.. error na sa pagcompare..

wala kong problema sa pagcompare sa constants since may solution naman dun sa compiler=)
Lend a hand for those who are in need!=)

Stop Hijacking!=) More Technical Posts!=)
 ;)

Kaizer Killer EX Pre-Alpha

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #12 on: April 28, 2010, 03:53:11 PM »
tita marce,

nasolve mo po ba ito?

hehe ^_^ ang problem ko naman dito

pag pre-init ang string ok.. pero once nareinitialize mo sya.. error na sa pagcompare..

wala kong problema sa pagcompare sa constants since may solution naman dun sa compiler=)

di ko alam kung saan ang nagigign problem mo, pero eto yung basic structure na ginawa ko...

Code: [Select]
#include <string.h>
.
.

char marcelino[6]="pogi";

void main(void)
{
char uvar[6];
while(1)
{
gets(uvar);

if (strcmp(uvar,marcelino)==0)
printf("yeah!\n\r");
else
printf("palitan mo na keyboard mo!");
}
}

/*
STRCMP(str1,str2)
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2;
And a value less than zero indicates the opposite.
*/

di ko alam kung may mas magandang paraan sa ginawa ko...
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: string array comparison in CCS C... help!
« Reply #13 on: April 28, 2010, 03:56:49 PM »
Code: [Select]
char marcelino[6]="pogi";

di kaya mas lalong magkaproblema dahil sa line na ito ;D ;D ;D
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Offline Kaizer03

  • Nuclear Reactor
  • ****
  • Posts: 4847
  • Pogi/Ganda Points: 225
  • C#<-->Android<-->Java
    • PhilRobotics
Re: string array comparison in CCS C... help!
« Reply #14 on: April 28, 2010, 04:08:56 PM »
dito sis

Code: [Select]
char marcelino[6]="pogi";
:D

heeh ayun nga.. say jan sa code mo.. for instances nagbago ang laman ng marcelino na array.. say "jeje" then the variable 'uvar' also contains "jeje"

pagdating sa strncmp(uvar, marcelino, 4) == 0

ayan.. wala na.. :D

sige recheck ko ulet ^_^


nagtry din ako ng

strncmp(uvar, "jeje", 4)  ok din eh ^_^

dinifine ko lang sa device na sa RAM magwrite pag strings=)
by adding

#device PASS_STRINGS = IN_RAM
Lend a hand for those who are in need!=)

Stop Hijacking!=) More Technical Posts!=)
 ;)

Kaizer Killer EX Pre-Alpha

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: string array comparison in CCS C... help!
« Reply #15 on: April 28, 2010, 05:11:10 PM »
di kaya mas lalong magkaproblema dahil sa line na ito ;D ;D ;D

impakta!!! ;D ;D ;D

yan na nga ang key para magwork! ;D

@gigz, di ko kabisado yung ibang functions under string.h. yan lang ang ginamit ko so far...
kuha mo na yan for sure! ;D
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline Kaizer03

  • Nuclear Reactor
  • ****
  • Posts: 4847
  • Pogi/Ganda Points: 225
  • C#<-->Android<-->Java
    • PhilRobotics
Re: string array comparison in CCS C... help!
« Reply #16 on: April 28, 2010, 05:16:33 PM »
impakta!!! ;D ;D ;D

yan na nga ang key para magwork! ;D

@gigz, di ko kabisado yung ibang functions under string.h. yan lang ang ginamit ko so far...
kuha mo na yan for sure! ;D

hehe sige^_^ goodluck saken.. hope yung matututunan ko sa part na ito eh makatulong sa iba :D if ever :D thanks=)
Lend a hand for those who are in need!=)

Stop Hijacking!=) More Technical Posts!=)
 ;)

Kaizer Killer EX Pre-Alpha

Philippine Electronics Forum

Re: string array comparison in CCS C... help!
« Reply #16 on: April 28, 2010, 05:16:33 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com