home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / MINIMO2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-25  |  10.9 KB  |  329 lines

  1. /* MINIMO2.C - From page 512 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. This is a full modem program,     */
  3. /* although rather limited. I slightly changed this program.    */
  4. /* The #define BUFSIZE is changed from 8192 to 60416 and the    */
  5. /* check() function is called after all commands to the modem   */
  6. /* instead of only once as it is in the book. This program is   */
  7. /* a modification of MINIMO.C in that it uses the system clock  */
  8. /* rather than relying on timimg loops. Modifying it is des-    */
  9. /* cribed on page 512 of the book. After adding the function    */
  10. /* initime() to the program, each call to initime() with a      */
  11. /* decimal number argument must have that number cast to a long */
  12. /* in the call (3 different places) or the program won't work.  */
  13. /* The original function initime() was developed in the program */
  14. /* TIMER.C on page 511. In this program the call to initime()   */
  15. /* was made with a variable as an argument and the variable     */
  16. /* was declared a long hence no cast was needed.                */
  17. /****************************************************************/
  18.  
  19. #include <dos.h>        /*for union REGS*/
  20. #include <fcntl.h>      /*for system lovel I/O*/
  21. #include <types.h>      /* " */  /*extracted from subdir \include\sys*/
  22. #include <stat.h>       /* " */  /* " */
  23. #define COM 1           /*serial port no. (0 = COM1, 1 = COM2)*/
  24. #define CONF 0x83       /*1200 bps, no par, 1 stop, 8 bits*/
  25. #define RS232 0x14      /*RS232 serial port int number*/
  26. #define VIDEO 0x10      /*video int number*/
  27. #define DATAR 0x100     /*data ready bit in status word*/
  28. #define DELAY 500       /*make larger for faster computers*/
  29. #define TRUE 1
  30. #define FALSE 0
  31. #define OK 0            /*modem result code*/
  32. #define BUFSIZ 60416    /*size of disk file buffer*/
  33. union REGS inregs, outregs;   /* for ROM BIOS calls*/
  34. int far *fptr;          /*pointer to screen memory*/
  35. int row, col;           /*row and col of cursor position*/
  36. char buff[BUFSIZ];      /*disk file buffer*/
  37. int hnd;                /*disk file handle*/
  38. int bytes;              /*number of bytes stored in buffer*/
  39. int saveflag = FALSE;   /*TRUE when data being saved to disk*/
  40.  
  41. main()
  42. {
  43. char ch, receive();
  44.  
  45.    init(CONF);          /*initialize port*/
  46.    reset();             /*initialize modem*/
  47.    while(TRUE)  {
  48.       printf("\nPress  [F1] to dial a call");
  49.       printf("\n       [F2] to await call");
  50.       printf("\n       [F3] to resume call");
  51.       printf("\n       [F4] save to disk");
  52.       printf("\n       [F5] close disk file");
  53.       printf("\n       [F10] return to function menu");
  54.       printf("\nCommand: ");
  55.       if(getche() == 0)  {       /*if extended code*/
  56.          switch(getch())  {
  57.             case 59:             /* [F1] dial & call */
  58.                dial();
  59.                sr();
  60.                break;
  61.             case 60:             /* [F2] await call */
  62.                answ();
  63.                sr();
  64.                break;
  65.             case 61:             /* [F3] resume call */
  66.                sr();
  67.                break;
  68.             case 62:             /* [F4] save to disk */
  69.                save();
  70.                break;
  71.             case 63:             /* [F5] close file */
  72.                clo();
  73.                break;
  74.          }     /*end switch*/
  75.       }     /*end if*/
  76.    }     /*end while*/
  77. }     /*end main*/
  78.  
  79. /* reset() */  /* reset modem parameters */
  80. reset()
  81. {
  82.  
  83.    command("AT Z\r");            /*reset modem*/
  84.    check();                      /*check result code*/
  85.    /***  echo on, full-duplex, send result codes, numer codes ***/
  86.    command("AT E1 F1 Q0 V0\r");  /*initialize modem*/
  87.    check();                      /*check result code*/
  88.    command("AT\r");              /*reset bps rate*/
  89.    check();                      /*check result code*/
  90. }
  91.  
  92. /* clscr() */     /*clear the screen*/
  93. clscr()
  94. {
  95. int addr;
  96.  
  97.    fptr = (int far *) 0xB0000000;
  98.    for(addr = 0; addr < 2000; addr++)
  99.       *(fptr + addr) = 0x0700;
  100.    row = 0; col = 0;          /*initialize for putscr()*/
  101.    inregs.h.ah = 2;           /*'set cursor position' service*/
  102.    inregs.h.dh = row;
  103.    inregs.h.dl = col;
  104.    inregs.h.bh = 0;           /*page number*/
  105.    int86(VIDEO, &inregs, &outregs);
  106. }
  107.  
  108. /* dial() */   /* dials number */
  109. dial()
  110. {
  111. char phonum[30];              /*space for phone number*/
  112.  
  113.    reset();                   /*resets modem*/
  114.    printf("\n\nEnter phone number (example: 555-1212):  ");
  115.    gets(phonum);
  116.    command("AT DT ");         /*set up modem*/
  117.    check();                      /*check result code*/
  118.    command(phonum);           /*send number to modem*/
  119.    check();                      /*check result code*/
  120.    command("\r");             /*terminate command*/
  121.    check();                      /*check result code*/
  122. }
  123.  
  124. /* answ() */   /* sets up answer mode */
  125. answ()
  126. {
  127.  
  128.    reset();                   /*reset modem*/
  129.    command("AT S0 = 1\r");    /*answer on first ring*/
  130.    check();                      /*check result code*/
  131. }
  132.  
  133. /* sr() */     /*send-receive, full-duplex communication*/
  134. sr()
  135. {
  136. char ch, receive();
  137.  
  138.    clscr();
  139.    while(TRUE)  {
  140.       if(kbhit())  {                /*keyboard char ?*/
  141.          if((ch = getch()) != 0)    /*get char from keybd*/
  142.             send(ch);               /*send normal char*/
  143.          else if(getch() == 68)     /*if F[10] function key*/
  144.             return;                 /*return to menu*/
  145.       }
  146.       if(ready())  {             /*port has char ? */
  147.          ch = receive();
  148.          putscr(ch);
  149.          if(saveflag)  {         /*if saving to disk*/
  150.             buff[bytes++] = ch;  /*put in disk buffer*/
  151.             if(bytes > BUFSIZ)  {      /*if buffer full*/
  152.                printf("\n\nBuffer full.\n");
  153.                return;           /*return to menu*/
  154.             }
  155.          }
  156.       }  /*end if(ready())*/
  157.    }  /*end while*/
  158. }  /*end sr*/
  159.  
  160. /* save() */   /* opens disk file, sets up saveflag to write disk */
  161. save()
  162. {
  163. char fname[20];
  164.  
  165.    printf("\n\nName of file to save to: ");
  166.    gets(fname);
  167.    if((hnd = open(fname,
  168.                O_CREAT | O_WRONLY | O_TEXT, S_IWRITE)) < 0)
  169.       printf("\n\nCan't open file.\n");
  170.    else  {
  171.       printf("\n\nFile opened. Press [F5] to close.\n");
  172.       bytes = 0;              /*start of buffer*/
  173.       saveflag = TRUE;        /* 'write to disk' flag */
  174.    }
  175. }
  176.  
  177. /* clo() */    /* writes data from buffer to file & closes file */
  178. clo()
  179. {
  180.  
  181.    write(hnd, buff, bytes);      /*write buffer to file*/
  182.    close(hnd);
  183.    saveflag = FALSE;             /*turn off flag*/
  184. }
  185.  
  186. /* command */  /* send command string to modem */
  187. command(comstr)
  188. char *comstr;                    /*pointer to command*/
  189. {
  190. int j;
  191. char ch;
  192.  
  193.    putch('*');                /*reassure user*/
  194.    initime((long)100);        /*wait 1 sec to insure*/
  195.    while(!timeout())          /*last command is done*/
  196.       ;
  197.    for(j = 0; (ch = *(comstr + j)) != '\0'; j++)  {
  198.       send(ch);               /*send each char in turn*/
  199.       initime((long)12);      /*set timer to 12/100 sec*/
  200.       while(!timeout() && !ready())    /*wait for echo*/
  201.          ;                    /*before doing next one*/
  202.       ch = receive();
  203.    }
  204. }
  205.  
  206. /* check() */  /* check that modem returns '0' for OK */
  207. check()
  208. {
  209. char ch;
  210. int j;
  211.  
  212.    for(j = 0; j < 10; j++)  {       /*read up to 10 chars ? */
  213.       initime((long)12);            /*set timer to 12/100 sec*/
  214.       while(!timeout() && !ready())    /*wait for ready*/
  215.          ;                    /*before doing next one*/
  216.       ch = receive();               /*if we get an OK*/
  217.       if(ch == OK)
  218.          return;
  219.    }                 /*otherwise error*/
  220.    printf("\n\nCan't initialize modem.\n");
  221. }
  222.  
  223. /* putscr() */    /* puts char on screen at row, col */
  224. putscr(ch)
  225. char ch;
  226. {
  227. int j;
  228.  
  229.    switch(ch)  {
  230.       case(0x0A):             /*if return*/
  231.          col = 0;             /*start at beginning of line*/
  232.          break;
  233.       case(0x0D):             /*if linefeed*/
  234.          ++row;               /*go to next row*/
  235.          break;
  236.       case(0x08):             /*if backspace*/
  237.          if(col > 0)
  238.             --col;            /*backspace cursor*/
  239.          *(fptr + col + row*80) = 0x0700;  /*print space*/
  240.          break;
  241.       default:
  242.          *(fptr + col + row*80) = (int)ch | 0x0700;
  243.          ++col;               /*display & go to next col*/
  244.    }
  245.    if(col >= 80)  {           /*if past end of row*/
  246.       col = 0;                /*do carriage return and*/
  247.       ++row;                  /*linefeed*/
  248.    }
  249.    if(row >= 25)              /*if below bottom row*/
  250.       row = 0;                /*start at top*/
  251.    if(col == 0)               /*if at beginning of line*/
  252.       for(j = 0; j < 80; j++)    /*clear line*/
  253.          *(fptr + j + row*80) = 0x0700;
  254.    inregs.h.ah = 2;           /* 'set cursor position' service*/
  255.    inregs.h.dh = row;
  256.    inregs.h.dl = col;
  257.    inregs.h.bh = 0;           /*page number*/
  258.    int86(VIDEO, &inregs, &outregs);
  259. }
  260.  
  261. /* init() */      /* initialize serial port */
  262. init(conf)
  263. char conf;        /*addr of configuration code*/
  264. {
  265.    inregs.h.ah = 0;        /* 'initialize port' service */
  266.    inregs.x.dx = COM;      /*port number*/
  267.    inregs.h.al = CONF;     /*configuration*/
  268.    int86(RS232, &inregs, &outregs);
  269. }
  270.  
  271. /* send() */      /* send char to serial port */
  272. send(c)
  273. char c;
  274. {
  275.    inregs.h.ah = 1;        /* 'send char' service */
  276.    inregs.x.dx = COM;      /*port number*/
  277.    inregs.h.al = c;        /*character*/
  278.    int86(RS232, &inregs, &outregs);
  279. }
  280.  
  281. /* ready */    /* get serial port ready status */
  282. ready()
  283. {
  284.    inregs.h.ah = 3;        /* 'get status' service */
  285.    inregs.x.dx = COM;      /*port number*/
  286.    int86(RS232, &inregs, &outregs);
  287.    return(outregs.x.ax & DATAR);    /*return 'data ready' status*/
  288. }
  289.  
  290. /* receive */     /* get character from serial port */
  291. char receive()
  292. {
  293.    inregs.h.ah = 2;        /* 'receive char' service */
  294.    inregs.x.dx = COM;      /*port number*/
  295.    int86(RS232, &inregs, &outregs);
  296.    return(outregs.h.al & 0x7F);     /*return character*/
  297.                                     /* (AND off 8th bit*/
  298. }
  299.  
  300. /* initime() */   /* initializes time delay for timeout() function */
  301. /* delays number of hundredths of a second in argument */
  302. /* increments every 5 or 6 hundredths sec */
  303.  
  304. #include <types.h>      /*defines time_b type in timeb*/
  305. #include <timeb.h>      /*defines time_b structure*/
  306. struct timeb xtime;     /*structure for time info*/
  307. long time1, time2;      /*start & running times*/
  308. long intval;            /*interval in hundredths*/
  309.  
  310. initime(hsecs)
  311. long hsecs;
  312. {
  313.  
  314.    intval = (hsecs < 12) ? 12 : hsecs;    /*minimum is 12*/
  315.    ftime(&xtime);
  316.    time1 = (long)xtime.millitm/10 + xtime.time*100;
  317. }
  318.  
  319. /* timeout() */   /* Returns 1 to function timer() */
  320. /* if time interval exceeded, 0 otherwise. */
  321. timeout()
  322. {
  323.  
  324.    ftime(&xtime);
  325.    time2 = (long)xtime.millitm/10 + xtime.time*100;
  326.    return((time2-time1 > intval) ? 1 : 0);
  327. }
  328.  
  329.