I am writing a program on serial port where iam sending a string Code: $GPGGA,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*47" On receive side I am getting the string on sResult properly (code below )but after the if condtion i.e.if((tmp[3]=='G')&&(tmp[4]=='G')&&(tmp[5]=='A')) value of gpsFrame is incorrect Code: void *MTSerialRxThread() { unsigned char *resp; char a='$';// For Comparsion of Last CHAR GPS Data i.e.(New Line) unsigned char RMsg[74]; unsigned char tmp[6],val[1]; int first =1; int rc = 0; int n=0; tmp[0]='$'; readfd = open("/dev/ttyUSB0", O_RDONLY); if (readfd == -1) { perror("READ: open_port: Unable to open /dev/ttyUSB0- \n"); return 1; } else { printf("PORT OPENED\n"); fcntl(readfd, F_SETFL, 0); } initport(readfd); cfsetispeed(&options, B9600); /// Set the baud rates while(a!=sResult) { if(read(readfd,sResult,1)) { val[0]=sResult[n]; if(tmp[0]==val[0]) //For First Char $ ,if not $ read next char till its not equal to $ { rc=1; RMsg[rc] = sResult[n]; } else { RMsg[rc] = sResult[n]; rc++; } tmp[3]=RMsg[3]; tmp[4]=RMsg[4]; tmp[5]=RMsg[5]; printf("%s",sResult); [B]//STRING IS CORRECT[/B] } //printf("\nTMP VAR:%c %c %c\n ",tmp[3],tmp[4],tmp[5]); if((tmp[3]=='G')&&(tmp[4]=='G')&&(tmp[5]=='A'))value is correct { memset(gpsFrame,0,74); memcpy(gpsFrame,sResult,flength);flength is 75 printf("\n %s",gpsFrame);[B]//STRING IS NOT CORRECT[/B] } } } Code: [B]OUTPUT OF gpsFRAME is A,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*4[/B] NO idea why it coming like this Kindly give some solution
is this what you try to do in your code? Code: #include <stdio.h> #include <string.h> int main(){ unsigned char gpsFrame[74]; unsigned char sResult[]="$GPGGA,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*47"; memset(gpsFrame,0,74); printf("\n#%s#",gpsFrame); memcpy(gpsFrame,sResult,75); printf("\n #%s#",gpsFrame); getchar(); return 0; }
Are gpsFrame,sResult,flength global variables? If so how are gpsFrame and sResult defined? What is the array size? What is the value fo flength inside your function? Jim
Code: Are gpsFrame,sResult,flength global variables? Yes Code: If so how are gpsFrame and sResult defined? What is the array size? unsigned char gpsFrame[74] unsigned char sResult[flength]//flength (macro)value 75 I checked further when Iam printing sResult value inside if condition it is coming same as gpsFrame i.e Code: if((tmp[3]=='G')&&(tmp[4]=='G')&&(tmp[5]=='A'))value is correct { memset(gpsFrame,0,74); memcpy(gpsFrame,sResult,flength);flength is 75 printf("\n %s",gpsFrame);//STRING IS NOT CORRECT printf("\n %s",sResult);//STRING IS NOT CORRECT } OUTPUT OF gpsFRAME is A,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*4 OUTPUT OF sResult is A,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*4 Than means there is the problem inside if condtion but iam not able to get it
the error is outside the if clause and more specific in the creation of sResult try this to realiaze it Code: ............ printf("\n before=%s",sResult);//is it correct? if((tmp[3]=='G')&&(tmp[4]=='G')&&(tmp[5]=='A')) { printf("\n after=%s",sResult);//is it correct? ............ and try this also.You will see that memset,memcopy works Code: #include <stdio.h> #include <string.h> int main(){ unsigned char gpsFrame[74]; unsigned char sResult[75]; memcpy(sResult,"$GPGGA,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*47",75); printf("\n before=#%s#",sResult);//correct memset(gpsFrame,0,74); memcpy(gpsFrame,sResult,75); printf("\n #%s#",gpsFrame);//correct printf("\n after=#%s#",sResult);//correct getchar(); return 0; } p.s. what's your compiler?
Code: ............ printf("\n before=%s",sResult);//is it correct? [B]//HERE PERFECTLY OK[/B] if((tmp[3]=='G')&&(tmp[4]=='G')&&(tmp[5]=='A')) { [B] printf("\n after=%s",sResult);//is it correct? //ERROR IS HERE[/B] ............ #include <stdio.h> #include <string.h> Code: int main(){ unsigned char gpsFrame[74]; unsigned char sResult[75]; memcpy(sResult,"$GPGGA,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*47",75); printf("\n before=#%s#",sResult);//correct memset(gpsFrame,0,74); memcpy(gpsFrame,sResult,75); printf("\n #%s#",gpsFrame);//correct printf("\n after=#%s#",sResult);//correct getchar(); return 0; } YES ABOVE THIS WORKS I got the problem now problem is here Code: if(tmp[0]==val[0]) //For First Char $ ,if not $ read next char till its not equal to $ { rc=1; RMsg[rc] = sResult[n]; } else { [B]RMsg[rc] = sResult;[/B] [B]//PROBLEM IS HERE[/B] rc++; } where sResult is unsigned char sResult[74] sResult is string and RMsg is unsigned char Rmesg[74].I believe this is a wrong way
if i have got it right you want to do something like this? Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ int i; unsigned char gpsFrame[74]; unsigned char sResult[75]; unsigned char RMsg[74]; int n=0; memcpy(sResult,"123$GPGGA,123519,0.12343,N,0.19607,W,1,08,0.9,545.4,M,46.9,M,,*47",75); printf("\nbefore--->#%s#",sResult); while(sResult[n]!='$'){ n++; } int len=sizeof(sResult)/sizeof(unsigned char); for (i=0;i<len-n;i++){ RMsg[i]=sResult[n+i]; } RMsg[i]='\0'; printf("\n\n RMsg--->#%s#",RMsg); memset(sResult,0,75); memcpy(sResult,RMsg,75); printf("\n#%s#",sResult); //STRING IS CORRECT if((RMsg[3]=='G')&&(RMsg[4]=='G')&&(RMsg[5]=='A')){ memset(gpsFrame,0,74); memcpy(gpsFrame,sResult,75); printf("\n #%s#",gpsFrame); printf("\n after=#%s#",sResult); } getchar(); return 0; }
Ya u are correct this is what i want to do but 1.data is coming from serial port which is unsigned char sResult[75] 2.When Iam printing RMsg value .I am getting String always on 0th index .i.e on 0th value i get G then remain 74index it is all zero's ,then once again for loop starts I get next char P then 74 value zero's & so on I checked the sResult output also its taking value in sResult[0] index only. Code: int len=sizeof(sResult)/sizeof(unsigned char); for (i=0;i<len-n;i++){ RMsg[i]=sResult[n+i]; [B]printf("%c",RMsg[i]);[/B] }