Wednesday, June 5, 2013

HALF DUPLEX COMMUNICATION USING RS-232 IN C (FILE PASSING)



          HALF DUPLEX COMMUNICATION USING RS-232 IN C (FILE PASSING)


                             FILE TRANSMITTER -RECEIVER


#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<conio.h>
#include<bios.h>
#define COM1 0
#define COM1ADDR 0x3F8
#define DATA_READY 0x100
#define TRANS_READY 0x2000
main()
{
              char tfile,ch,rfile;
              int status,counter=0;
              long size;
              char setting,baud = 0xa0,parity = 0x08,data = 0x04,stop = 0x03;
              FILE *fpt;
              FILE *fpr;
              setting = baud | parity | data | stop;
              clrscr();
              bioscom(0,setting,COM1);
              printf("\n\tEnter Filename To Transfer : ");
              scanf("%s",tfile);
              fpt = fopen("c:\\1.txt" ,"r");
              if(fpt == NULL)
              {
                        printf("\n\tFile Not Found.....\n");
               }
              else
              {
                        fseek(fpt,0,SEEK_END);
                        size = ftell(fpt);
                        printf("\n\tFile size is %ld",size);
                        rewind(fpt);
                        printf("\n\tFile is being transferred\n\tWait...\n");
                        while(!feof(fpt))
                        {
                                  status=bioscom(3,0,COM1);
                                  if(status & TRANS_READY)
                                  {
                                           ch=fgetc(fpt);
                                           outport(COM1ADDR,ch);
                                           counter++;
                                           gotoxy(20,20);

                                           printf("%d Bytes has been transfered",counter);                                       }
                                     }
                                     outport(COM1ADDR,0x1b);
                                     printf("\n\tFile transferred successfully");
                                     getch();
                            }



                            printf("\n\tEnter file to receive: ");
                            scanf("%s",rfile);
                            fpr = fopen( rfile,"w");
                            if(fpr == NULL)
                            printf("\n\tError...File cannot be opened");
                            else
                            {
                                     bioscom(0,setting,COM1);
                                     while(1)
                                     {
                                              status=bioscom(3,0,COM1);
                                              if(status & DATA_READY)
                                              {
                                                        ch = inport(COM1ADDR);
                                                        fputc(ch,fpr);
                                                         if(ch== 0x1b)
                                                         break;
                                                         printf("%c",ch);
                                              }
                                     }
                                       }
                  }

No comments:

Post a Comment