08 March, 2014

VRC

VRC Receiver

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

void displayBit(char c)
{
    int no=128,i;

    for(i=0;i<8;i++)
    {
        if((no&c)>0)
            printf("1");
        else
            printf("0");
        no=no>>1;
    }
}
int getBits(char c,int pos)
{
    int no=128;
   
    no=no>>(pos-1);

    if((no&c)>0)
        return 1;
    else
        return 0;
}
char unsetBits(char c,int pos)
{
    int no=128;

    no=no>>(pos-1);

    no=~no;

    return (no&c);
}
main()
{
        int fd,fd1,i,count=0,bit;
        char ch;

        if((fd=open("testfile",O_RDONLY)) < 0 && (fd1=open("output.txt",O_WRONLY)<0))
        {
                perror("Pipe open failed");
        }
        printf("Received message : ");

        for(;;)
        {
               count=0;

               if((i=read(fd,&ch,1)) > 0)
                {
                        //printf("%c\t",ch);

                        //displayBit(ch);

                        for(i=1;i<=8;i++)
                        {

                                bit=getBits(ch,i);

                                if(bit==1)
                                {
                                        count++;
                                }
                        }
                        if(count%2==0)
                        {
                                bit=getBits(ch,1);

                                if(bit==1)
                                {
                                        ch=unsetBits(ch,1);
                                }
                        }
                        else
                        {
                                printf("Error detected at char %c",ch);
                                break;
                        }

                        printf("%c",ch);

                        //displayBit(ch);

                        write(fd1,&ch,1);

                }

        }
}

 VRC Sender
#include<stdio.h>
#include<fcntl.h>

void displayBit(char c)
{
    int no=128,i;

    for(i=0;i<8;i++)
    {
        if((no&c)>0)
            printf("1");
        else
            printf("0");
        no=no>>1;
    }
}
int getBits(char c,int pos)
{
    int no=128;
   
    no=no>>(pos-1);

    if((no&c)>0)
        return 1;
    else
        return 0;
}
char setBits(char c,int pos)
{
    int no=128;

    no=no>>(pos-1);

    return (no|c);

}

main()
{
        char ch;

        int fd1,fd2,b,count=0,i;


        if((fd1=open("input.txt",O_RDONLY))<0)
        {

                perror("File open failed");
        }

        if((fd2=open("testfile",O_WRONLY))<0)
        {
                perror("File open failed");
        }

        printf("Sender Message : ");
        while(1)
        {
                count=0;

                if((i=read(fd1,&ch,1))<=0)
                {
                        break;
                }

                printf("\nOld:\t");
                displayBit(ch);

                for(i=1;i<=8;i++)
                {
                        b=getBits(ch,i);

                        if(b==1)
                        {
                                count++;
                        }
                }

                if(count%2!=0)
                {

                        ch=setBits(ch,1);

                }

                printf("\tNew:\t");
                displayBit(ch);

                printf("\t- %c",ch);

                write(fd2,&ch,1);


        }

}