Wednesday, June 5, 2013

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



FULL 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
void main()
{
            int ch;
            printf("enter your choice");
            printf("\n 1:transmit \n 2:receive");
            switch(ch)
            {
                        case 1:
                        {
                                    transfer();
                                    break;
                         }
                        case 2:
                        {
                                    receive();
                                    break;
                         }
            }
}
                                    transfer()
{
            char tfile;
            int status,counter=0;
            char ch;
            long size;
            char setting,baud = 0xe0,parity = 0x08,data = 0x04,stop = 0x03;
            FILE *fpt;
            setting = baud | parity | data | stop;
            clrscr();
            bioscom(0,setting,COM1);
            printf("enetr filename to transfer:");
            scanf("%s",tfile);
            fpt = fopen("c:\\TC\\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();
                                                }
                        }
                       receive()
                       {
                                   char ch1,rfile;
                                   int status;
                                   char setting,baud = 0xe0,parity = 0x08,data = 0x04,stop = 0x03;
                                   FILE *fpr;
                                   setting = baud | parity | data | stop;
                                   clrscr();
                                   printf("\n enter file to receive");
                                   scanf("%s",rfile);
                                   fpr=fopen(rflie,"w");
                                   if(fpr==NULL)
                                   printf("\n\t Error... File cannot be opened:");
                                   else
                                   {
                                               bioscom(0,setting,COM1);
                                               while(1)
                                               {
                                                           status=bioscom(3,0,COM1);
                                                           if(status & DATA_READY)
                                                           {
                                                                       ch1=inport(COM1ADDR);
                                                                       fputc(ch1,fpr);
                                                                       if(ch1==0x1b)
                                                                       {
                                                                                   break;
                                                                                   printf("%c",ch1);
                                                                       }
                                                           }
                                               }
                                   }
                       }

No comments:

Post a Comment