postheadericon Bit Stuffing - Framing Technics

Sender



#include<stdio.h>
#include<string.h>
#include<fcntl.h>



int getBit(int frameData,int pos)
{
//get binary equavalient of our Frame Data by setting on bit by bit

int code=1; // take base of binary 1 [ 00000001 ]

code=code<<(pos-1); // do the left shift to find  postion

if(frameData & code)  // if both bits are 1 then set bit otherwise return ZERO
return 1;
else
return 0;

}


void bitStuffing(char myFrame[])
{
// stuffing procedure
int i=0,k=0,counter=0;
char temp[50];
strcpy(temp,myFrame);
while(temp[i]!='\0')
{
myFrame[k++]=temp[i]; // update Frame data by doing Stufing
if(temp[i]=='1')
{
// if we encounter continues 1 then do increment in counter
counter++;
}
else
{
// if continuty of 1's break, reset counter to ZERO
counter=0;
}
if(counter==5)
{
// after a FIVE consicutive ONE stuff a bit ZERO
myFrame[k++]='0';
}
i++;
}
myFrame[k]='\0';
}
void main()
{
int pid,i,no,k=0,frameData,frame_size=32;
char myFrame[50];


system(">pipe");


pid=open("pipe",O_WRONLY); // open named pipe for write mode

system("clear");

//Enter Frame Data in decimal Number
printf("\nEnter Frame Data : ");
scanf("%d",&frameData);

for(i=frame_size;i>0;i--)
{
myFrame[k++]=getBit(frameData,i)+48;
}
myFrame[k]='\0';

printf("\nBefore Stuffing Frame : %s",myFrame);

bitStuffing(myFrame); // do the process of bit Stuffing

printf("\nAfter Stuffing  Frame : %s\n\n",myFrame);
write(pid,&myFrame[0],sizeof(myFrame)); //write frame in to named pipe
}




Receiver



#include<stdio.h>
#include<string.h>
#include<fcntl.h>

void deStuffing(char myFrame[])
{
int i=0,counter=0,k=0;
char temp[50];

//take backup of original Frame Data in temp
strcpy(temp,myFrame);

while(temp[i]!='\0')
{
myFrame[k++]=temp[i]; // update Frame data after deStufing
if(temp[i]=='1')
{
// if we encounter continues 1 then do increment in counter
counter++;
}
else
{
// if continuty of 1's break, reset counter to ZERO
counter=0;
}
if(counter==5)
{
//to omit stuffed bit do increment in index
i++;
}
i++;
}
myFrame[k]='\0';
}

void main()
{
int pid;
char myFrame[50];

system("clear");

pid=open("pipe",O_RDONLY);// open named pipe for READ Only mode

read(pid,&myFrame,sizeof(myFrame)); //read from named pipe

printf("\nBefore destuffing Frame : %s",myFrame);
deStuffing(myFrame);
printf("\nAfter Destuffing Frame  : %s\n\n",myFrame);
}



0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.