Mar 19 2014
Single Parity Bit Checker VRC
The simplest and oldest error detection method. A binary digit called parity is used to indicate whether the number of bits with “1” in a given set of bits is even or odd. The parity bit is then appended to original data.
Usually used to detect transmission error . Sender adds the parity bit to existing data bits before transmission.
Receiver checks for the expected parity, If wrong parity found, the received data is discarded and retransmission is requested.
vrcsen.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | //VRC Sender Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions int main() { char *myfifo = "myfifo"; //FIFO File Path int fp; //Open Pipe Return Value unsigned char ch,tch,parity=0; //Unsigned character because we will use first bit as parity bit printf("Creating Pipe...Waiting for receiver for Process...\n"); //Creating Named Pipe //mkfifo(<filepath>,<permission>) if(mkfifo(myfifo,0666) < 0) { perror("FIFO (Named Pipe) could not be created.\n"); exit(-1); } else printf("FIFO (Named Pipe) Created...\n"); //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_WRONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened.\n"); exit(-2); } else printf("FIFO (Named Pipe) Opened...\n"); /* VRC LOGIC STARTS */ printf("Enter Any Valid Character : "); scanf("%c",&ch); tch=ch; while(tch > 0) { parity ^= tch & 1; //Extract one by one bit and XOR it with parity tch >>= 1; } printf("\nOriginal Character => %c",ch); if(parity) //If Parity is 1 means total number of 1's is ODD and we need to set parity 1 ch += 128; //10000000 will set parity 1 printf("\nStuffed Character => %c\n",ch); //Writing to Named Pipe //write(<open_pipe_return_value>,<message>,<msgsize>); if(write(fp,&ch,sizeof(ch)) < 0) { perror("Error writing to FIFO (Named Pipe)\n"); exit(-3); } else printf("Message has been sent to FIFO (Named Pipe)\n"); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\n"); exit(-4); } return 0; } |
vrcrec.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | //VRC Receiver Example #include<stdio.h> //Standard Input Functions #include<sys/types.h> //Named Pipes (FIFO) Types #include<sys/stat.h> // Named Pipes (FIFO) Functions #include<fcntl.h> //File Control Unix Functions #include<unistd.h> //Unix Standard Functions int main() { char *myfifo = "myfifo"; //FIFO File Path int fp; //Open Pipe Return Value unsigned char ch,tch,parity=0; //Opening Named Pipe //open(<filepath>,<openingmode>); fp = open(myfifo,O_RDONLY); if(fp < 0) { perror("FIFO (Named Pipe) could not be opened for reading.\n"); exit(-1); } else printf("FIFO (Named Pipe) Opened... Trying to read from\n"); //Reading From Named Pipe //read(<open_pipe_return_value>,<message>,<msgsize>); if(read(fp,&ch,sizeof(ch)) < 0) { perror("Error reading from FIFO (Named Pipe)\n"); exit(-2); } else printf("Message Received from FIFO (Named Pipe)\n"); printf("\nRecieved Character => %c",ch); tch = ch; while(tch > 0) { parity ^= tch & 1; //Extract last bit and XOR it with Parity tch >>= 1; //Remove last bit using right shift } if(parity){ //If parity is 1 means total no of 1's is ODD printf("\nMessage Currupted..."); exit(-5); } if(ch > 128) //If ASCII is > 128 then it means we added 128 to make character parity even ch-= 128; printf("\nOriginal Character => %c\n",ch); //Closing Named Pipe //close(<open_pipe_return_value>); if(close(fp) < 0) { perror("Error closing FIFO (Named Pipe)\b"); exit(-3); } if(unlink(myfifo) < 0) { perror("Error deleting FIFO (Named Pipe)\b"); exit(-4); } return 0; } |