|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Problem using "strncmp" |
Posted: Thu Jul 13, 2017 8:59 am |
|
|
Hi
Hello, I'm trying to compare two char strings using "strncmp", but I can not get it to work properly. Someone could tell me what is my mistake..
Here is my code:
Code: | #include <18F4520.h>
#device PASS_STRINGS=IN_RAM
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOSTVREN
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#include <stdlib.h>
#define LED PIN_D0
#define ON output_high
#define OFF output_low
int const maxLongBuffRX=42;
char bufferRX[maxLongBuffRX];
int indBufferRX=0x00;
char XCommand[maxLongBuffRX];
char flag_RptaCommand;
int i;
void Add_bufferRX(char c);
void SendATCommand( char *pCommandTX );
#int_rda
void serial_isr() {
if(kbhit()){
Add_bufferRX(getc());
}
flag_RptaCommand = 1;
}
void Ini_buff_rec(void){
int i;
for(i=0;i<maxLongBuffRX;i++){
bufferRX[i]=0x00;
}
indBufferRX=0x00;
}
void Add_bufferRX(char c){
switch(c){
case 0x0D:
break;
case 0x0A:
break;
default:
bufferRX[indBufferRX++]=c;
}
}
void SendATCommand( char *pCommandTX, char *pRXCommand ){
char *ptr;
Ini_buff_rec();
flag_RptaCommand = 0;
printf ("%s",pCommandTX);
putc('\r');
while ( flag_RptaCommand == 0);
//ptr=strstr(bufferRX,pRXCommand);
if(strncmp(bufferRX,pRXCommand,strlen(pRXCommand))== 0)
ON (LED);
else
OFF (LED);
}
void main(){
OFF(LED);
enable_interrupts(INT_RDA);
clear_interrupt(INT_RDA);
enable_interrupts(GLOBAL);
SendATCommand("AT", "OK");
delay_ms(200);
while (TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Jul 13, 2017 9:09 am |
|
|
There is a basic problem that you don't really want to do the test until you have the entire reply.
Change your interrupt handler:
Code: |
#int_rda
void serial_isr() {
if(kbhit()){
Add_bufferRX(getc());
}
}
void Add_bufferRX(char c){
switch(c){
case 0x0D:
bufferRX[indBufferRX++]='\0';
flag_RptaCommand = TRUE;
break;
case 0x0A:
bufferRX[indBufferRX++]='\0';
flag_RptaCommand = TRUE;
break;
default:
bufferRX[indBufferRX++]=c;
}
}
|
This way the flag to say a command has been received, will only be set when the carriage return after the 'OK' is seen, not after just one character. You also then have a proper null terminated string, which currently you don't have. |
|
|
|
|
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
|