File transfer between two computers using communication ports

Started by Kalyan, Mar 23, 2008, 03:57 PM

Previous topic - Next topic

Kalyan

File transfer between two computers using communication ports

To transfer text and html files between two computers using three wire connections between com ports.

A little modification can also transfer image and multimedia files.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
#include<graphics.h>
#include<start.h>

void baudset(long);
void comparm(int,int,int);
int port_address;
union REGS i,o;
void main()
{
   int gd=DETECT,gm,maxx,maxy,x,y,button,ex=1,c,v=1,run=1;
   FILE *f;
   char chr;
   char *fname;
   int result,status,choice,port;
   unsigned int fstat,mstat,lstat;
   st();
   while(1)
   {
      initgraph(&gd,&gm," ");
      cleardevice();
      maxx=getmaxx();
      maxy=getmaxy();
      setbkcolor(9);
      if(run==1)
      {
         gotoxy(32,1);
         printf("PORT INACTIVE");
         run++;
      }
      else
      {
         gotoxy(32,1);
         printf("PORT ACTIVATED");
      }

      choice=0;
      rectangle(0,20,maxx,maxy);
      setviewport(1,5,maxx-1,maxy-1,1);
      if(initmouse()==0)
         {
            closegraph();
            restorecrtmode();
            outtextxy(100,100,"mouse driver not loaded");
            exit(1);
         }
      restrictmouseptr(1,20,maxx-1,maxy-1);
      showmouseptr();
      setcolor(6);
      outtextxy(115,30,"PROGRAM TO TRANSFER FILES BETWEEN TWO COMPUTERS");
      setcolor(3);
      outtextxy(430,45,"www.itacumens.com");
      setcolor(14);
      outtextxy(170,45,"IT Acumens");
      setcolor(10);
      outtextxy(200,100,"1    :    SEND FILE");
      outtextxy(200,120,"2    :    RECIEVE FILE");
      outtextxy(200,140,"3    :    EXIT");
      outtextxy(200,175,"enter your choice (1,2 or 3)::");
      rectangle(195,95,210,110);
      rectangle(195,115,210,130);
      rectangle(195,135,210,150);
      setcolor(13);
      outtextxy(20,450,"PRESS 'F1' TO ACTIVATE THE PORTS");
      setcolor(9);
      while(v==1)
      {
         if(kbhit())
         {
            chr=getch();
            if(chr==0)
               {
                  chr=getch();
                  c=(int)chr;
                  if(c==59)
                      {
                     gotoxy(31,1);
                     printf("PORT ACTIVATED");
                     v=2;
                     break;

                      }
               }
         }
      }
      while(ex==1)
      {
         getmousepos(&button,&x,&y);
         gotoxy(5,1);
         (button & 1)== 1 ? printf("DOWN"):printf("UP");

         gotoxy(20,1);
         (button & 2) == 2 ? printf("DOWN"):printf("UP");

         gotoxy(65,1);
         printf("X=%03d Y=%03d",x,y);
         if((button & 1)==1)
            {
               if((x>=195 && x<=210) && (y>=95 && y<=110))
                  {
                     choice=1;
                  }
               if((x>=195 && x<=210) && (y>=115 && y<= 130))
                  {
                     choice=2;
                  }
               if((x>=195 && x<=210) && (y>=135 && y<=150))
                  {
                     choice=3;
                  }
            }
         if(choice==1 || choice==2 || choice==3)
                  goto sos;
         if(kbhit())
            {
                  goto sob;
            }
      }
      fflush(stdin);
sob:      gotoxy(60,12);
      scanf("%d",&choice);
sos:      switch(choice)
         {
            case 1:         {
                     setcolor(12);
                     outtextxy(10,205,"select port (1: com1 2: com2) ::");
                     rectangle(470,200,485,215);
                     rectangle(510,200,525,215);
                     outtextxy(474,203,"1");
                     outtextxy(514,203,"2");
                     gotoxy(50,14);
                     scanf("%d",&port);
                     if(port==1)
                        port_address=0x3F8;
                     else
                        port_address=0x2F8;
                     baudset((long)2400);
                     comparm(0,1,8);
                     outport(port_address+4,1|2);
                     outtextxy(10,240,"enter the name of file to be sent::");
                     gotoxy(50,16);
                     scanf("%15s",&fname);
                     f=fopen(&fname,"r");
                     if(f==NULL)
                        {
                           setcolor(4);
                           outtextxy(10,300,"*** file do not exist ***");
                           setcolor(6);
                           outtextxy(10,320,"hit any key to continue........");
                           getch();
                           break;
                        }
                     closegraph();
                     restorecrtmode();
                     clrscr();
                     textcolor(2);
                     for( ;; )
                        {
                           if(kbhit())
                              {
                                 if(getch()==27)
                                 break;
                              }
                           lstat=inportb(port_address+5);
                           if(!(lstat & 32))
                              continue;
                           else
                           {
                              chr=fgetc(f);
                           }
                           if(chr==EOF)
                           {
                              cprintf("FILE TRANSMITTED<BR>);
                              outportb(port_address,EOF);
                              getch();
                              break;
                           }
                           outportb(port_address,chr);
                           putchar(chr);
                        }

                     fclose(f);
                     break;
                  }

            case 2:    {
                     setcolor(12);
                     outtextxy(10,205,"select port (1: com1  2: com2) ::");
                     gotoxy(50,14);
                     scanf("%d",&port);
                     if(port==1)
                     port_address=0x3F8;
                     else
                     port_address=0x2F8;
                     baudset((long)2400);
                     comparm(0,1,8);
                     outportb(port_address+4,1|2);
                     textcolor(4);
                     outtextxy(10,240,"save as file name::");
                     gotoxy(50,16);
                     scanf("%15s",&fname);

                     f=fopen(&fname,"w" );
                     if(f==NULL)
                     {
                        setcolor(4);
                        outtextxy(10,300,"*** cannot open file ***");
                        break;
                     }
                     closegraph();
                     restorecrtmode();
                     clrscr();
                     textcolor(4);
                     cprintf("waiting for i/p<BR>);
                     textcolor(7);
                     cprintf("press esc for exit<BR>);
                     gotoxy(1,4);
                     for(;;  )
                        {
                           if(kbhit())
                            {
                               if(getch()==27)
                               break;
                            }
                           fstat=inportb(port_address+5);
                           if(fstat & 2)
                           cprintf("over run time error<BR>);
                           if(fstat & 4 )
                           cprintf("parity error");
                           if(fstat & 8 )
                           cprintf("framing error<BR>);
                           if(fstat & 16)
                           cprintf("break recieved<BR>);
                           if(!(fstat & 1))
                              continue;
                           chr=inportb(port_address);
                           if(chr==EOF)
                               {
                                 textcolor(2);
                                 cprintf("FILE RECIEVED<BR>);
                                 exit(0);
                               }
                           putchar(chr);
                           fputc(chr,f);
                           }
                           outportb(port_address+4,0);
                           fclose(f);
                           break;
                  }

            case 3: exit(0);
            default:        sound(900);
                  delay(10000);
                  nosound();
                  break;
         }

   }

}


void baudset(long baudrate)
   {
      unsigned int divisor;
      unsigned char lsb,msb;
      divisor=1152001/baudrate;
      msb=divisor>>8;
      lsb=(divisor<<8 )>> 8;
      outportb(port_address+3,128);
      outportb(port_address,lsb);
      outportb(port_address+1,msb);

   }


void comparm(int parity,int stop,int databit)
   {
   int parmbyte;
   parmbyte=databit-5;
   if(stop==2)
      parmbyte|=4;
   if(parity!=0)
      parmbyte|=8;
   if(parity==2)
      parmbyte|=16;
   outportb(port_address+3,parmbyte );
   }

/*    MOUSE WORK    */

initmouse()
{
   i.x.ax=0;
   int86(0x33,&i,&o);
   return(o.x.ax);
}


showmouseptr()
{
   i.x.ax=1;
   int86(0x33,&i,&o);
}


restrictmouseptr(int x1,int y1,int x2,int y2)
{
   i.x.ax=7;
   i.x.cx=x1;
   i.x.dx=x2;
   int86(0x33,&i,&o);
   i.x.ax=8;
   i.x.cx=y1;
   i.x.dx=y2;
   int86(0x33,&i,&o);
}


getmousepos(int *button,int *x,int *y)

{
   i.x.ax=3;
   int86(0x33,&i,&o);
   *button=o.x.bx;
   *x=o.x.cx;
   *y=o.x.dx;
}