You are on page 1of 4

Bit Stuffing

Experiment No.:- Date:-

Aim:- Write a program to perform a bit stuffing using RS-232.

Program:-

#include<bios.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
clrscr();
int n,i,j,k,status,count=0,l;
char vs[50],vr[50],s[50]="01111110",d[50];
bioscom(0,0xeb,0);
printf("\n1:Sending\n2:Receving\n3:Exit\n Your Choice:-");
scanf("%d",&n);
strcpy(d,s);
switch(n)
{
case 1:printf("\nEnter data:-"); //Sending Data
scanf("%s",&vs);
l=strlen(vs);
j=8;
for(i=0;i<l;i++)
{
s[j]=vs[i];
if(vs[i]=='1')
{
count++;
if(count==5)
{
j++;
s[j]='0';
count=0;
}
}
else
count=0;
j++;
}
strcat(s,d);
printf("\nStuffed Data:-%s",s);
l=strlen(s);
for(i=0;i<=l;i++)
outport(0x3f8,s[i]);
break;
case 2:i=0; //Receiving Data
do
{
status=bioscom(3,0,0);
if(status & 0x100)
{
vr[i]=inport(0x3f8);
if(vr[i]=='\0')
goto f;
i++;
}
}while(1);
f:printf("Stuffed Data:-%s",vr);
k=strlen(vr);
j=0;
for(i=8;i<k;i++)
{
if(vr[i]=='0' && vr[i+1]=='1' && vr[i+2]=='1' && vr[i+3]=='1' &&
vr[i+4]=='1' && vr[i+5]=='1' && vr[i+6]=='1' && vr[i+7]=='0')
{
goto e;
}
s[j]=vr[i];
if(vr[i]=='1')
{
count++;
if(count==5)
{
i++;
count=0;
}
}
else
count=0;
j++;
}
e:s[j]='\0';
printf("\nData:-%s",s);
break;
case 3:exit(0); //Exit
default:break;
}
getch();
}
Flow Chart:-

You might also like