Home Forums C Programming Communcation between 2 pcs using port

Viewing 1 reply thread
  • Author
    Posts
    • #1883
      JAZYY18
      Participant

      /* Communcation between 2 pcs using port */
      #include
      #include
      #include
      #include
      #include
      #include
      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,"ver 1.0 ");
        setcolor(14);
        outtextxy(170,45,"Sanjoy Saha & Avik Debnath");
        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",&po rt);
             if(port==1)
              port_address =0x3F8;
             else
              port_address =0x2F8;
             baudset((long)2400 );
             comparm(0,1,8);
             outport(port_addre ss+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)
              {
               setcol or(4);
               outtex txy(10,300,"*** file do not exist ***");
               setcol or(6);
               outtex txy(10,320,"hit any key to continue........");
               getch( );
               break;
              }
             closegraph();
             restorecrtmode();
             clrscr();
             textcolor(2);
             for(;;)
              {
               if(kbh it())
                 {
                   if(getch()==27)
                   break;
                 }
               lstat= inportb(port_address+5);
               if(!(l stat & 32))
                 continue;
               else
               {
                 chr=fgetc(f);
               }
               if(chr ==EOF)
               {
                 cprintf("FILE TRANSMITTED");
                 outportb(port_address,EOF);
                 getch( );
                 break;
               }
               outpor tb(port_address,chr);
               putcha r(chr);
              }
             fclose(f);
             break;
            }
          case 2:  {
             setcolor(12);
             outtextxy(10,205," select port (1: com1  2: com2) ::");
             gotoxy(50,14);
             scanf("%d",&po rt);
             if(port==1)
             port_address=0x3F8 ;
             else
             port_address=0x2F8 ;
             baudset((long)2400 );
             comparm(0,1,8);
             outportb(port_addr ess+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");
             textcolor(7);
             cprintf("press esc for exit");
             gotoxy(1,4);
             for(;;)
              {
               if(kbh it())
                {
                 if(getch()==27)
                 break;
                }
               fstat= inportb(port_address+5);
               if(fst at & 2)
               cprint f("over run time error");
               if(fst at & 4)
               cprint f("parity error");
               if(fst at & 8)
               cprint f("framing error");
               if(fst at & 16)
               cprint f("break recieved");
               if(!(f stat & 1))
                 continue;
               chr=in portb(port_address);
               if(chr ==EOF)
                 {
                   textcolor(2);
                    cprintf("FILE RECIEVED");
                   exit(0);
                 }
               putcha r(chr);
               fputc( chr,f);
                     }
                     outportb(port_address+4,0);
                     fclose(f);
                     break;
            }
          case 3: exit(0);
          default:    & ;nbs p;   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;
      }

    • #3138
      msaqib
      Participant

      No description for the code?
      Also i could not run the code as it says can not find the header file start.h.
      Can you please write the brief description so that I can use this program.
      Thanks
      Saqib

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.