Wednesday, June 5, 2013

SIMPLEX COMMUNICATION USING RS-232 IN C (FILE PASSING)



         SIMPLEX COMMUNICATION USING RS-232 IN C (FILE PASSING)

SIMPLEX FILE TRANSMITER


#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
             /*#define SETTING 0xE0|0x08|0x03|0x00;  baud9600 none parity  8data 1stop bit */
   main()
   {
char tfile;
int status,counter=0;/*,v=1;*/
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("\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);
                                        if(ch==EOF)
                                       {
                                                outport
                                                break;
                                        }
                                       outport(COM1ADDR,ch);
                                       counter++;

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

                                      SIMPLEX FILE RECEIVER

#include<stdio.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 rfile,ch;
int status;
char setting,baud = 0xa0,parity = 0x08,data = 0x04,stop = 0x03;
FILE *fpr;
setting = baud | parity | data | stop;
clrscr();
             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