home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / PCL4C50.ZIP / SELFTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-23  |  8.1 KB  |  313 lines

  1. /*
  2. **                ---  selftest.c ---
  3. **
  4. **  SELFTEST requires two serial ports on the same computer. The
  5. **  program transmits a test string on one port (FirstCOM) and
  6. **  receives on a second port (SecondCOM), where the two ports are
  7. **  connected via a null modem adapter. The received string is tested
  8. **  against the transmit string (they should be idenical).
  9. **
  10. **  Connect the two serial ports (on a single computer) together
  11. **  using a null modem cable. Be sure to modify the configuration
  12. **  section for non-standard PC ports or to setup your multiport
  13. **  board. Note that many multiport boards are either Digiboard or
  14. **  BOCA board compatible.
  15. **
  16. **  IMPORTANT: You may have to modify the port address & IRQ to match
  17. **  your Digiboard or BOCA board installation.
  18. **
  19. **  Compile with /DDPMI for protected mode testing.
  20. */
  21.  
  22. #ifdef DPMI
  23. #include "windows.h"
  24. #include "use_dpmi.h"
  25. #endif
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <malloc.h>
  29. #include <dos.h>
  30. #include <string.h>
  31. #include <conio.h>
  32. #include "pcl4c.h"
  33.  
  34. #define ESC 0x1b
  35.  
  36. #define READ_PORT(P) inportb(X)
  37.  
  38. #define PC 1
  39. #define DB 2
  40. #define BB 3
  41.  
  42. /*** Global Variables ***/
  43.  
  44. int BaudCode = Baud9600;  /* Code for 9600 baud  */
  45. char *TestString = "This is a test string";
  46. int TestLength;
  47. int FirstCOM;
  48. int SecondCOM;
  49.  
  50. int MultiUART = 0;
  51. int MultiIRQ = 0;
  52. int MultiStatus = 0;
  53.  
  54. void AllDone(void);
  55. void Display(int);
  56. void CheckAlloc(int,char *);
  57. int  ErrorCheck(int);
  58. int  AllocSeg(int);
  59.  
  60. static int  RxSeg1 = 0;
  61. static int  TxSeg1 = 0;
  62. static int  RxSeg2 = 0;
  63. static int  TxSeg2 = 0;
  64.  
  65. char *Ptr[4] = {"SMALL","COMPACT","MEDIUM","LARGE"};
  66.  
  67. /* standard PC port configuration */
  68.  
  69. static int StdAdrPC[4] = {0x3f8,0x2f8,0x3e8,0x2e8};
  70. static int StdIrqPC[4] = {4,3,4,3};
  71.  
  72. static int Selection = 0;  /* PC, DB, or BB */
  73.  
  74. void main(int argc, char *argv[])
  75. {int Port;
  76.  char *P;
  77.  int ComLimit = 0;
  78.  char c;
  79.  int Version;
  80.  int Model;
  81.  int i, rc;
  82.  if(argc!=4)
  83.   {printf("Usage: selftest {pc|db|bb} 1stCom 2ndCom\n");
  84.    exit(1);
  85.   }
  86.  P = argv[1];
  87.  if((strcmp(P,"pc")==0)||(strcmp(P,"PC")==0)) Selection = PC;
  88.  if((strcmp(P,"db")==0)||(strcmp(P,"DB")==0)) Selection = DB;
  89.  if((strcmp(P,"bb")==0)||(strcmp(P,"BB")==0)) Selection = BB;
  90.  if(Selection==0)
  91.    {puts("Must specify 'PC', 'DB' or 'BB' as 1st argument");
  92.     puts("EG:  SELFTEST PC 1 4");
  93.     exit(1);
  94.    }
  95.  if(Selection==PC) ComLimit = COM4;
  96.  if(Selection==DB) ComLimit = COM8;
  97.  if(Selection==BB) ComLimit = COM16;
  98.  FirstCOM = atoi(argv[2]) -1;
  99.  SecondCOM = atoi(argv[3]) -1;
  100.  printf("FirstCOM  = COM%d\n",1+FirstCOM);
  101.  printf("SecondCOM = COM%d\n",1+SecondCOM);
  102.  if(FirstCOM<COM1)
  103.    {puts("1stCom must be >= COM1");
  104.     exit(1);
  105.    }
  106.  if(SecondCOM>ComLimit)
  107.    {printf("2ndCom must be <= COM%d\n",1+ComLimit);
  108.     exit(1);
  109.    }
  110.  if(FirstCOM>=SecondCOM)
  111.    {puts("1stCom must be < 2ndCom");
  112.     exit(1);
  113.    }
  114.  if(Selection==DB)
  115.    {/*** Custom Configuration: DigiBoard PC/8 ***/
  116.     MultiUART = 0x180; MultiIRQ = IRQ5; MultiStatus = 0x1C0;
  117.     printf("[ Configuring DigiBoard as COM1-COM8 (IRQ%d) @ 0x%x ]\n",
  118.        MultiIRQ,MultiUART);
  119.     SioPorts(8,COM1,MultiStatus,DIGIBOARD);
  120.     for(Port=COM1;Port<=COM8;Port++)
  121.       {/* set DigiBoard UART addresses */
  122.        ErrorCheck( SioUART(Port,MultiUART+8*Port) );
  123.        /* set DigiBoard IRQ */
  124.        ErrorCheck( SioIRQ(Port,MultiIRQ) );
  125.       }
  126.    }
  127.  if(Selection==BB)
  128.    {/*** Custom Configuration: BOCA BB2016 ***/
  129.     MultiUART = 0x100; MultiIRQ = IRQ10; MultiStatus = MultiUART + 7;
  130.     printf("[ Configuring BOCA Board as COM1-COM16 (IRQ%d) @ 0x%x ]\n",
  131.        MultiIRQ,MultiUART);
  132.     SioPorts(16,COM1,MultiStatus,BOCABOARD);
  133.     for(Port=COM1;Port<=COM16;Port++)
  134.       {/* set BOCA Board UART addresses */
  135.        ErrorCheck( SioUART(Port,MultiUART+8*Port) );
  136.        /* set BOCA Board IRQ */
  137.        ErrorCheck( SioIRQ(Port,MultiIRQ) );
  138.       }
  139.    }
  140.  if(Selection==PC)
  141.    {/*** Standard Configuration: 4 port card ***/
  142.     puts("[ Configuring for PC ]");
  143.     for(i=COM1;i<=COM4;i++)
  144.       {SioUART(i,StdAdrPC[i]);
  145.        SioIRQ(i,StdIrqPC[i]);
  146.       }
  147.    }
  148.  /* setup transmit & receive buffer */
  149.  CheckAlloc(RxSeg1 = AllocSeg(128), "RX1");
  150.  CheckAlloc(RxSeg2 = AllocSeg(128), "RX2");
  151.  ErrorCheck( SioRxBuf(FirstCOM,RxSeg1,Size128) );
  152.  ErrorCheck( SioRxBuf(SecondCOM,RxSeg2,Size128) );
  153.  if(SioInfo('I'))
  154.    {CheckAlloc(TxSeg1 = AllocSeg(128), "TX1");
  155.     CheckAlloc(TxSeg2 = AllocSeg(128), "TX2");
  156.     ErrorCheck( SioTxBuf(FirstCOM,TxSeg1,Size128) );
  157.     ErrorCheck( SioTxBuf(SecondCOM,TxSeg2,Size128) );
  158.     printf("Transmit buffers created\n");
  159.    }
  160.  /* set port parmameters */
  161.  ErrorCheck( SioParms(FirstCOM,NoParity,OneStopBit,WordLength8) );
  162.  ErrorCheck( SioParms(SecondCOM,NoParity,OneStopBit,WordLength8) );
  163.  /* reset the ports */
  164.  ErrorCheck( SioReset(FirstCOM,BaudCode) );
  165.  ErrorCheck( SioReset(SecondCOM,BaudCode) );
  166.  printf("***\n");
  167.  printf("*** SELFTEST 4.0\n");
  168.  printf("***\n");
  169.  Version = SioInfo('V');
  170.  Model = SioInfo('M');
  171.  printf("*** Lib Ver : %d.%d\n",Version/16,Version%16);
  172.  printf("***   Model : %s \n", Ptr[Model&3] );
  173.  printf("*** TX Intr : ");
  174.  if(SioInfo('I')) puts("Enabled.");
  175.  else puts("Disabled.");
  176.  printf("*** DPMI    : ");
  177.  if(SioInfo('P')) printf("YES\n");
  178.  else printf("NO\n");
  179.  /* display port info */
  180.  Display(FirstCOM);
  181.  Display(SecondCOM);
  182.  printf("***\n");
  183.  
  184.  printf("Starting @ 9600 baud. COM%d to COM%d\n",FirstCOM+1,SecondCOM+1);
  185.  
  186.  TestLength = strlen(TestString);
  187.  /* send string */
  188.  printf("  Sending: ");
  189.  for(i=0;i<TestLength;i++)
  190.    {c = TestString[i];
  191.     SioPutc(FirstCOM,c);
  192.     putch(c);
  193.    }
  194.  SioDelay(3);
  195.  /* receive string */
  196.  printf("\nReceiving: ");
  197.  for(i=0;i<TestLength;i++)
  198.    {rc = SioGetc(SecondCOM,18);
  199.     if(rc<0)
  200.       {printf("\nERROR: ");
  201.        SioError(rc);
  202.        SioDone(FirstCOM);
  203.        SioDone(SecondCOM);
  204.        exit(1);
  205.       }
  206.     /* echo just received char */
  207.     putch((char)rc);
  208.     /* compare character */
  209.     if((char)rc!=TestString[i])
  210.       {printf("\nERROR: Expecting '%c'(0x%x), received '%c'(0x%x)\n",
  211.           (char)rc,(char)rc,TestString[i],TestString[i]);
  212.        SioDone(FirstCOM);
  213.        SioDone(SecondCOM);
  214.        exit(1);
  215.       }
  216.    } /* end for */
  217.  puts("\nSUCCESS: Test AOK !");
  218.  AllDone();
  219.  exit(0);
  220. } /* end main */
  221.  
  222. int ErrorCheck(int Code)
  223. {/* trap PCL error codes */
  224.  if(Code<0)
  225.      {SioError(Code);
  226.       AllDone();
  227.       exit(1);
  228.      }
  229.  return(0);
  230. } /* end ErrorCheck */
  231.  
  232.  
  233. void Display(int Port)
  234. {printf("***    COM%d : ",1+Port);
  235.  if(Selection==PC) printf("Adr=%3x IRQ=%d",StdAdrPC[Port],StdIrqPC[Port]);
  236.  else printf("Adr=%3xh IRQ=%d Status=%xh",MultiUART+8*(Port-COM1),MultiIRQ,MultiStatus);
  237.  if( SioFIFO(Port,LEVEL_8) ) printf(" [16550]\n");
  238.  else printf(" [8250/16450]\n");
  239. }
  240.  
  241.  
  242. #ifdef DPMI
  243.  
  244. int AllocSeg(int Size)
  245. {long hMem;
  246.  int  Selector = 0;
  247.  hMem = (long) GlobalDosAlloc( (long)Size );
  248.  if(hMem)
  249.    {/* get selector */
  250.     Selector = LOWORD(hMem);
  251.     GlobalPageLock(Selector);
  252.     return Selector;
  253.    }
  254.  else return 0;
  255. }
  256.  
  257. #else
  258.  
  259. int AllocSeg(int Size)
  260. {int Seg;
  261.  char far *Ptr;
  262.  /* allocate far heap */
  263.  Ptr = (char far *) _fmalloc(Size+16);
  264.  if(Ptr==NULL) return 0;
  265.  /* SEG:0 points to buffer */
  266.  Seg = FP_SEG(Ptr) + ((FP_OFF(Ptr)+15)>>4);
  267.  return Seg;
  268. }
  269.  
  270. #endif
  271.  
  272. void AllDone(void)
  273. {printf("Shutting down COM%d, ",1+FirstCOM);
  274.  SioDone(FirstCOM);
  275.  printf("COM%d.\n",1+SecondCOM);
  276.  SioDone(SecondCOM);
  277.  /* some more info */
  278.  printf("%d RX Interrupts, ", SioInfo('R') );
  279.  printf("%d TX Interrupts\n", SioInfo('T') );
  280. #ifdef DPMI
  281.  printf("Freeing DOS memory: ");
  282.  /* free DOS memory */
  283.  if(RxSeg1)
  284.     {printf("RX1 ");
  285.      GlobalPageUnlock(RxSeg1);
  286.      GlobalDosFree(RxSeg1);
  287.     }
  288.  if(TxSeg1)
  289.     {printf("TX1 ");
  290.      GlobalPageUnlock(TxSeg1);
  291.      GlobalDosFree(TxSeg1);
  292.     }
  293.  if(RxSeg2)
  294.     {printf("RX2 ");
  295.      GlobalPageUnlock(RxSeg2);
  296.      GlobalDosFree(RxSeg2);
  297.     }
  298.  if(TxSeg2)
  299.     {printf("TX2 ");
  300.      GlobalPageUnlock(TxSeg2);
  301.      GlobalDosFree(TxSeg2);
  302.     }
  303.  #endif
  304.  exit(0);
  305. }
  306.  
  307. void CheckAlloc(int Seg,char *Msg)
  308. {if(Seg==0)
  309.   {printf("Cannot allocate memory for %s\n",Msg);
  310.    AllDone();
  311.    exit(1);
  312.   }
  313. }