Thursday, 15 September 2011

SIMPLE CRC WITH COMMAND

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void calc();
void getd();
int i,j,n,g,a,arr[20],gen[20],b[20],q[20],x[20],check=0,s,ch=0;
main()
{
printf("\n\n\t ****** CYCLIC REDUNDANCY CHECK ****** ");
while(1)
{
printf("\n\n\tENTER THE NUMBER TO PERFORM THE OPERATION");
printf("\n\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\n\t1.SENDER SIDE CALCULATION\n\n\t2.RECEIVER SIDE CALCULATION\n\n\t3.EXIT");
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
/* SENDER SIDE CALCULATION */
printf("\n\t Transmitter side:");
getd(); //CALLING FUNCTION FOR GETTING DATA
printf("\n\t Enter no. of divisor bits to calculate: ");
scanf("%d",&g); //GET THE NO.OF BITS TO TRANSMIT
do //MUST INCLUDE THE FIRST BIT IS 1
{
printf("\n\t Enter the generator data: \n");
for(j=0;j<g;j++)
scanf("%d",&gen[j]);
}while(gen[0]!=1); //IF TRUE MEANS THE STARTING BIT OF THE DEVISER IS 0
printf("\n\t The divisor is:");
for(j=0;j<g;j++)
printf("%d",gen[j]);//PRINT THE DEVISER
a=n+(g-1); //CALCULATE THE TOTAL NO.OF BIT TO DERIVATION
printf("\n\t now the total[no.of data+(no.of diviser-1)] no.of bit is:%d",a);
printf("\n\t The transmitter side data is:");
for(i=0;i<j;++i)
arr[n+i]=0; //FILL UP THE 0 FOR REMAINING SPACE (EXTRA BIT)
for(i=0;i<a;++i)
printf("%d",arr[i]); //PRINT THE DATA WITH EXTRA BITS
calc(); // CALLING FUNCTION FOR CALCULATE THE CRC BITS
printf("\n\t THE RESULT IS:");
for(i=0;i<a;i++)
printf("%d",q[i]); //PRINT THE SENDER SIDE RESULT
break;
case 2:
/* RECEIVER SIDE CALCULATION*/
printf("\n\t Receiver side:");
//getd(); //CALLING FUNCTION FOR GET THE DATA FOR RECEIVER SIDE
for(i=0;i<n;++i)
arr[i] =q[i];
for(i=n,j=g-1;i<a,j<0;i++,j--)
arr[i]=q[j]; //ARRANGE THE CRC BITS INTO REVRSE ORDER (CALCULATE THE RECEIVER SIDE CRC BIT)
printf("\n\t The receiver side CRC bit is:");
for(i=0;i<g-1;++i)
printf("%d",q[i]); //PRINT RECEIVER SIDE CRC BIT
printf("\n\t The original data is:");
for(i=0;i<a;++i)
printf("%d",arr[i]);
printf("\n\t The receiver side data with crc is:");
for(i=0;i<a;++i)
printf("%d",arr[i]); //PRINT THE RECEIVER SIDE DATA WITH CRC BIT
calc(); //CALLING FUNCTION FOR CALCULATE OR IDENTIFY THE ERROR
printf("\n");
i=0;
while(i<a)
{
if(q[i]==0)
check=0;
else
check=1;
i++;
}
printf("\n\n Result of CRC Error detection is: ");
if(check==0)
printf("\n\t Data is accepted successfully!");
else
printf("\n Resend the data again!");
break;
case 3:
exit(0);
break;
}
}

}
void calc()
{
for(i=0;i<n;++i)
q[i]= arr[i]; //TAKE THE COPY TO OUR ORIGINAL DATA
for(i=0;i<n;++i)
{
if(arr[i]==0)//IF TRUE MEANS THE ORIGINAL DATA OR CURRRENT STAGE RESULT FIRST BIT IS 0
{
printf("\n\t The first bit is o:");
for(j=i;j<g+i;++j)
arr[j] = arr[j]^0; //PERFORM THE EX-OR OPERATION BY OOOO
printf("\n\t The answer is :\t");
for(j=i;j<g+i;++j)
printf(" %d",arr[j]);//PRINT THE CURRENT STAGE RESULT
}
else //IF TRUE MEANS THE ORIGINAL DATA OR CURRRENT STAGE RESULT FIRST BIT IS 0
{
arr[i] = arr[i]^gen[0]; //} THE PERFORMENCE OF THE EX- OR OPERATION
arr[i+1]=arr[i+1]^gen[1]; //} IN THE DATA BY USING THE DIVISER BITS
arr[i+2]=arr[i+2]^gen[2]; //}
arr[i+3]=arr[i+3]^gen[3]; //}
printf("\n\n\t The answer is :\t");
for(j=i;j<g+i;++j)
printf(" %d",arr[j]); //PRINT THE CURRENT STAGE RESULT
}
}
printf("\n\t The CRC bit is :");
for(i=n;i<a;++i)
printf("%d",arr[i]); //PRINT THE FINAL RESULT OF AFTER THE CRC CALCULATION
for(i=0;i<g-1;++i)
x[i]=arr[i];
s=n+a;
for(i=n;i<s;i++)
q[i]=arr[i];
}
void getd()
{
printf("\n\t Enter no. of data bits: ");
scanf("%d",&n); //GET THE DATA SIZE
printf("\n\t Enter the data bits:\n ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]); //GET THE ORIGINAL DATA
printf("\n\t The given data is:");
for(i=0;i<n;++i)
printf("%d",arr[i]); //PRINT THE DATA
}

No comments:

Post a Comment