home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / COMPORTS.ZIP / COMPORTS.C next >
Text File  |  1990-12-13  |  4KB  |  206 lines

  1.  
  2. // Quick Hack in MS C 6.0 to Access Com Port 3
  3. // OS/2 Device driver will not recognize it.
  4. // This disregards the OS and goes straight for the Port.
  5. // (Just Like its usually done in DOS)
  6. //
  7. // Mark Lussier
  8. // December 12, 1990
  9.  
  10.  
  11. #define INCL_BASE
  12. #define INCL_COMMON
  13. #define INCL_NOPM
  14. #define INCL_DOSPROCESS
  15. #include <os2.h>
  16. #include <malloc.h>
  17. #include <conio.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #define   STK_SIZE    2048
  22.  
  23. #define COM1 0x03f8
  24. #define COM2 0x02f8
  25. #define COM3 0x03e8
  26. #define COM4 0x02e8
  27.  
  28. #define BAUD_115200  1
  29. #define BAUD_57600   2
  30. #define BAUD_38400   3
  31. #define BAUD_19200   4
  32. #define BAUD_9600    6
  33. #define BAUD_4800    12
  34. #define BAUD_2400    48
  35. #define BAUD_1200    96
  36.  
  37.  
  38. #define PAR_NONE     0x00
  39. #define PAR_EVEN     0x18
  40. #define PAR_ODD      0x04
  41.  
  42. #define DATA7        0x02
  43. #define DATA8        0x03
  44.  
  45. #define STOP1        0x00
  46. #define STOP2        0x04
  47.  
  48.  
  49.  
  50. ULONG FAR main_sem, FAR ctd_sem;
  51. USHORT ktcid, ctdid;
  52. INT ComPort = COM3;
  53.  
  54.  
  55. VOID FAR WritePort ( CHAR Value )
  56. {
  57.  
  58.   while ( ! ( inp(ComPort+5) & 0x20 ) );
  59.   outp (ComPort, Value); 
  60.  
  61. }
  62.  
  63. INT getuserinput( CHAR *prompt, CHAR *text, INT length)
  64. {
  65.   STRINGINBUF inbuf;
  66.   VioWrtTTY("\r\n\n",3,0);
  67.   VioWrtTTY(prompt,strlen(prompt),0);
  68.   inbuf.cchIn = 0;
  69.   inbuf.cb = length;
  70.   KbdStringIn(text,&inbuf,0,0);
  71.   VioWrtTTY("\r",1,0);
  72.   text[inbuf.cchIn] = '\0';   /* terminate string */
  73.   return 0;
  74. }
  75.  
  76. VOID FAR keytocom()
  77. {
  78.  KBDKEYINFO keyinfo;
  79.  UCHAR charcode, scancode;
  80.  UINT written, ioctlerr;
  81.  CHAR comerr, xname[64], YNresponse[5];
  82.  
  83.  while ( -1 )  /* Do forever: */
  84.  {
  85.    /* Get character from keyboard: */
  86.    KbdCharIn (&keyinfo,0,0);
  87.    charcode = keyinfo.chChar;
  88.    scancode = keyinfo.chScan;
  89.  
  90.    /* Alt-X indicates End-Of-Processing: */
  91.    if (charcode == 0x00 && scancode == 0x2D)
  92.    {
  93.      DosSemRequest(&ctd_sem,-1L);
  94.      getuserinput("Exit to OS/2? ",YNresponse,4);
  95.      DosSemClear(&ctd_sem);
  96.      YNresponse[0] = toupper(YNresponse[0]);
  97.      if (YNresponse[0] == 'Y')
  98.           /* Clear Main semaphore:        */
  99.        DosSemClear(&main_sem);
  100.      continue;
  101.     }
  102.     WritePort ( charcode );
  103.   }
  104. }
  105.  
  106. VOID FAR comtodsp ()
  107. {
  108.   CHAR CharReady;
  109.   CHAR Str[2];
  110.   Str[1] = '\0';
  111.   while ( -1 )
  112.   {
  113.     DosSemRequest(&ctd_sem,-1L);
  114.     CharReady = inp(ComPort+5);
  115.   
  116.     if ( CharReady & 0x01 )
  117.     {
  118.       CharReady = inp(ComPort);
  119.       Str[0] = CharReady;
  120.       VioWrtTTY(Str,1,0);
  121.     }
  122.     DosSemClear(&ctd_sem);
  123.   }
  124. }
  125. VOID SetBaud ( INT BaudRate, INT Parity, INT Data, INT Stop )
  126. {
  127.   outp ( ComPort + 3, 0x80 );
  128.   outp ( ComPort , BaudRate );
  129.   outp ( ComPort + 1, 0 );
  130.   outp ( ComPort + 3, Parity | Data | Stop );
  131. }
  132. VOID CheckPortAddresses ()
  133. {
  134.   UCHAR Code;
  135.   INT i;
  136.   static INT StandardAddress[4] = {0x03f8,0x02f8,0x03e8,0x02e8};
  137.  
  138.   printf("\n\n Checking COM PORT 'Standard Addresses' \n\n");
  139.  
  140.   for (i=0; i < 4; i++)
  141.   {
  142.  
  143.     Code = inp(StandardAddress[i]+2);
  144.     Code =  ( Code == 0 || Code == 2 || Code == 4 || Code == 6 || Code == 1) ? 1 : 0;
  145.  
  146.     if ( Code )
  147.       printf("Com%d: UART8250 Responding At Address %04x\n",i+1,StandardAddress[i]);
  148.     else
  149.       printf("Com%d: UART8250 is NOT Responding At Address %04x\n",i+1,StandardAddress[i]);
  150.   }
  151.  
  152. }
  153.  
  154. main ()
  155. {
  156.   CHAR FAR * ctdstack, far * ktcstack;
  157.  
  158.   printf(" Com Reality.\n");
  159.   printf("Use Com1-Com4 in OS/2\n");
  160.   printf("By Mark.  (c) December 1990 \n\n");
  161.  
  162.   CheckPortAddresses ();
  163.  
  164.   ctdstack = malloc(STK_SIZE);
  165.   ktcstack = malloc(STK_SIZE);
  166.  
  167.   if (ctdstack == NULL || ktcstack == NULL)
  168.   {
  169.     puts ("Unable to allocate stacks");
  170.     exit(2);
  171.   }
  172.  
  173.  SetBaud ( BAUD_2400, PAR_NONE, DATA8, STOP1 );
  174.  
  175.  /* Create receive and display thread: */
  176.  if (DosCreateThread(comtodsp,&ctdid,
  177.                      ctdstack+STK_SIZE))
  178.  {
  179.    puts("Can't create COM receive thread");
  180.    exit(1);
  181.  }
  182.  
  183.  /* Set semaphore to block main thread: */
  184.  DosSemSet(&main_sem);
  185.  
  186.  /* Create transmit thread: */
  187.  if (DosCreateThread(keytocom,&ktcid,ktcstack+STK_SIZE))
  188.  {
  189.    puts("Can't create COM transmit thread");
  190.    exit(1);
  191.  }
  192.  
  193.  DosSemWait(&main_sem,-1L);
  194.  
  195.  DosSuspendThread(ktcid);
  196.  DosSuspendThread(ctdid);
  197.  
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204. 
  205. 
  206.