Friday 30 September 2011

RECEIVER PROGRAM FOR RS-232

#include<stdio.h>
#include<conio.h>
#include<bios.h>
#define SETTINGS (_COM_9600 | _COM_CHR8 | _COM_NOPARITY |_COM_STOP1)
/* baud rate = 9600, 8 data bits, no parity bit, 1 stop bit */
void main(void)
{
unsigned in,out,status;
int port;
clrscr();
printf(“Select Port(Enter ‘0’ for COM1 and ‘1’ for COM2):”);
scanf(“%d”,&port);
printf(“Press ESC to exit”);
textcolor(GREEN);
cprintf(“\n\rData Received:\n”);
_bios_serialcom(_COM_INIT,port,SETTINGS);
for(;;)
{
status=_bios_serialcom(_COM_STATUS,port,0);
if (status & 512)
printf(“\n\t\a Overrun Error”);
if (status & 1024)
printf(“\n\t\a Parity Error”);
if (status & 2048)
printf(“\n\t\a Framing Error”);
if(status & (512|1024|2048)) /* if any error */
break;
if(status & 256) /* if data ready */
{
if((out=_bios_serialcom(_COM_RECEIVE,port,0) & 255)!=0)
putch(out);
}
if(kbhit()) /* if a keystroke is currently available */
{
in=getch(); /* get a character without echoing onto the screen */
if(in==27) /* if ESC */
break;
_bios_serialcom(_COM_SEND,port,in);
}
}
}

SLIDING WINDOW PROTOCOL PROGRAM

#include<stdio.h>
#include<conio.h>
#include<string.h>
int sx[10],rx[20];
int n=0,k=0,nk=0,sls=0,j=0,l=0,count=1,i=0;
void main()
{
clrscr();
printf("\n*****SLIDING WINDOW FLOW CONTROL PROTOCOL******\n");
printf("\nEnter the no of data:\n ");
scanf("%d",&n);
printf("\nenter the data at one by one:\n");
for(i=0;i<n;i++)
{
scanf("%d",&sx[i]);
}
printf("\nEnter the slide size:\n ");
scanf("%d",&sls);
printf("\n****The frame size is calculated [framesize=slidesize-1]****");
nk=sls-1;
printf("\nThe frame size is: %d \n",nk);
printf("\n\tSENDER \t\tRECEIVER\n\t*******\t\t*********\n");
for(i=0;i<n;i++)
{
rx[i]=sx[i];
k++;
printf("\t");
for(j=i;j<nk;j++)
printf("%d",sx[j]);
printf("\t\t");
for(l=0;l<k;l++)
printf("%d",rx[l]);
printf("\t\n");
if(i==count)
{
getch();
printf("\n\t   ACK%d is accered\n\n",count+1);
count+=2;
nk+=2;
}
getch();
}
printf("\n***The final zeros are consider as empty****");
getch();

}


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
}

THE SIMPLE HAMMING CODE


/* SIMPLE HAMMING CODE*/
#include<stdio.h>
#include<math.h>
#include<ctype.h>
void assign(void);
void compare(void);
void rr1(void);
void rr2(void);
void rr4(void);
void rr8(void);
void print(void);
int i,ch=0;
int h[12],e[12],f[8];
int r1,r2,r4,r8;

void main()
{
printf("\n\n\tHAMMING CODE\n");
while(1)
{
  printf("\n1.transmitter side.\t 2.receiver side.\t3.exit\n");
  scanf("%d",&ch);  //GET THE INPUT OF CHOICE
  switch(ch)
  {
   case 1:
    printf("\n hamming code at transmittr side\n");  //TRANSMITTER
    printf("enter the 7 bit data:\n");
    for(i=7;i>=1;i--)
      scanf("%d",&f[i]);  //GET THE 7 BIT DATA
    assign();                              //GENERATE THE HAMMING CODE
    printf("HAMMING CODE\n");
    print();                 //PRINT THE RESULT
    rr1();              
    rr2();
    rr4();
    rr8();
    break;
   case 2:
     printf("\n hamming code at receiver side\n");  //RECEIVER
    printf("enter the receiver side data\n");
    for(i=11;i>=1;i--)
      scanf("%d",&e[i]);      //GET THE RECEIVER SIDE DATA
    printf("\n HAMMING CODE IN RECEIVER SIDE\n");
     for(i=11;i>=1;i--)
      printf("%d",e[i]);  //PRINT THE HAMMING CODE
    compare();   //COMPARE THE RECEIVER HAMMING CODE AND THE RESULT OF                                                                                                                           //R1,R2,R4,R8
    break;
   default:
     exit(1);
     break;
  }
}

}
void print() //DEFINITION OF PRINT() FUNCTION
{
 for(i=11;i>=1;i--)
  printf("%d",h[i]);//PRINT THE VALUE
}
void assign()//DEFINITION OF ASSIGN() FUNCTION
{                       //TO GENERATE THE 11 DIGIT HAMMING CODE
   h[11]=f[7];
   h[10]=f[6];
   h[9]=f[5];
   h[8]=0;
   h[7]=f[4];
   h[6]=f[3];
   h[5]=f[2];
   h[4]=0;
   h[3]=f[1];
   h[2]=0;
   h[1]=0;

}
void rr1() //DEFINITION OF RR1() FUNCTION
{
  printf("\n r1\n");

   r1=h[1]+h[3]+h[5]+h[7]+h[9]+h[11]; //ADD THE POSITION 1,3,5,7,9,11
  if((r1==6) || (r1==4) || (r1==2) || (r1==0))  //IF IT IS EVEN
    h[1]=0;                                            //SET 0
  else
    h[1]=1;             //OTHERWISE SET 1
  print();  //CALLING FOR PRINT() FUNCTION
}
void rr2()   //DEFINITION OF RR2()
{
  printf("\n r2\n");
  r2=h[2]+h[3]+h[6]+h[7]+h[10]+h[11];  //ADD THE POSITION 2,3,6,7,10,11
  if((r2==6) || (r2==4) || (r2==2) ||(r2==0))  //IF IT IS EVEN SET 0
    h[2]=0;
  else
    h[2]=1;            //OTHERWISE SET 1
  print();
}
void rr4()         //DEFINITION OF RR4()
{
  printf("\n r4\n");
  r4=h[4]+h[5]+h[6]+h[7];   //ADD THE POSITION 4,5,6,7
  if((r4==2) || (r4==0)|| (r4==4))  //IF IT IS EVEN SET 0
    h[4]=0;
  else                                 //OTHERWISE SET 1
    h[4]=1;
  print();
}
void rr8()  //DEFINITION OF RR8()
{
  printf("\n r8\n");
  r8=h[8]+h[9]+h[10]+h[11];  //ADD THE POSITION 8,9,10,11
  if((r8==4) || (r8==2) || (r8==0))  //IF IT IS EVEN SET 0
    h[8]=0;
  else         /OTHERWISE SET 1
    h[8]=1;
  print();  //CALLING THE PRINT();
}
void compare()  //DEFINITION OF COMPARE()
{
   int b=0;
   for(i=11;i>=1;i--)
     if(h[i]!=e[i])  
       b=i;
   if(b==0)
     printf("\n there is no error in receiver side");
   else
     printf("error is present at %dth bit",b);
}