/*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_RDAvoid 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
i-store muna siya sa buffer or anything na pwede mag-compare.in pseudocode:a = serial1.inputif a = "gather"serial2.output = "get"kung mahahabang string na or hahanapin sa mahabang string 'yung word kailangan naman ng parsing.
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"); . . . } . . . }}
char buffer[100];int data = FALSE;#int_RDAvoid 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"); } }}
#define ERROR -1#define NO_ERROR 0char buffer[100];int data = FALSE;#int_RDAvoid 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"); } }}
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; }}
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].
#define ERROR -1#define NO_ERROR 0char buffer[100];int data = FALSE;#int_RDAvoid 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"); } }}
tita marce,nasolve mo po ba ito?hehe ^_^ ang problem ko naman ditopag 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=)
#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.*/
Code: [Select]char marcelino[6]="pogi";
char marcelino[6]="pogi";
di kaya mas lalong magkaproblema dahil sa line na ito
impakta!!! yan na nga ang key para magwork! @gigz, di ko kabisado yung ibang functions under string.h. yan lang ang ginamit ko so far... kuha mo na yan for sure!