CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PB to decode frame

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aydi



Joined: 11 Aug 2010
Posts: 12

View user's profile Send private message

PB to decode frame
PostPosted: Thu Aug 12, 2010 6:14 am     Reply with quote

hello,

I use this elements to develop my program.

PIC :18F2550
compiler: PICC
simulator:ISIS

I develop a program to decode a frame sent through the port rs232.

Format of the frame: @0AX,Fx,Y,Fy,Z1,Fz1,Z2,Fz2

I want to decode this frame and to have the value of each element lonly
X=...
Y=....
Fx=....
.
.
This program it just give me the first element which is the value of X.

And the other values is empty. I didn't know why it works very well for X and not for the others elements.

For example: I write like that @0A 34,67,89,90,45,34,56,34

The result:
X=34
Fx=
Y=
Fy=
.
.

Thanks in advance.

The PROGRAM:
Code:

#include <18F2550.h>
#include <string.h>
#fuses HS,NOWDT,NOPROTECT,PLL5,CPUDIV1
#use delay(clock=20000000)//Xtal frequency = 20MHz
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void wait() //Subroutine to wait for 2ms or until a character is received
{
int countdown;       

countdown=200;//Charge value to 200 times 10us

while((--countdown!=0)&&!kbhit()) //kbhit returns true when rs232 receives a character

delay_us(10);
}

void main()
{
char chaine[35], X[35], Y[35], Z1[35], Z2[35], Fx[35], Fy[35], Fz1[35], Fz2[35], chaine1[35];
  int i=0,cmpt=0;

 //Do a loop
  while(true)
{
wait();//Wait for visualization or another character
if(kbhit())
  {
   gets(chaine);   
   
   WHILE(chaine[i]!='\0')
   {
    chaine[i]=chaine[i+3];
    if (chaine[i]==',')
      cmpt=cmpt+1;
     
    switch(cmpt)
     {
      case 0:X[i]=chaine[i];break;
     
      case 1:Fx[i]=chaine[i];break;

      case 2:Y[i]=chaine[i];break;

      case 3:Fy[i]=chaine[i];break;

      case 4:Z1[i]=chaine[i];break;

      case 5:Fz1[i]=chaine[i];break;

      case 6:Z2[i]=chaine[i];break;

      case 7:Fz2[i]=chaine[i];break;
     
      default:        break;
     }
   i++;
   }
     
   printf("\r\n");
   printf("chaine_caractere=%s",chaine);
   printf("\r\n");
   printf("cmpt=%d",cmpt);
   printf("\r\n");
   printf("X=%s",X);
   printf("\r\n");
   printf("Fx=%s",Fx);
   printf("\r\n");
   printf("Y=%s",Y);
   printf("\r\n");
   printf("Fy=%s",Fy);
   printf("\r\n");
   printf("Z1=%s",Z1);
   printf("\r\n");
   printf("Fz1=%s",Fz1);
   printf("\r\n");
   printf("Z2=%s",Z2);
   printf("\r\n");
   printf("Fz2=%s",Fz2);
  }
}
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 12, 2010 6:47 am     Reply with quote

I don't know ISIS, but debugging software requires you to use the right tools. For example in the free MPLAB toolsuite there is a Simulator which allows you to step through your program and examine the value of variables at each state.

In your program it goes well for x:
x[0] = chaine[0] // space
x[1] = chaine[1] // 3
x[2] = chaine[2] // 4
y[3] = chaine[3] // , <-- Here it goes wrong, you should write to Y[0] and you want to skip the '.'

I recommend you to study the standard C function strtok() which was designed for the purpose of extracting parameters from a string. It is not the most easy function to understand but many examples can be found on the internet.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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