COMTEST.C
上传用户:wyswfw
上传日期:2007-05-18
资源大小:2k
文件大小:7k
源码类别:

串口编程

开发平台:

DOS

  1. #include<process.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<dos.h>
  5. #include<conio.h>
  6. #include<string.h>
  7. #include<bios.h>
  8. int flag=0,g,t=0,tt,t1=0,irq,irqnum,irqreg,irqs,irqe;
  9. unsigned int tport;
  10. int i,ti,loop=30;
  11. void interrupt far new_int();
  12. void interrupt(*old_int)();
  13. int dd(char a)
  14. {
  15.    if(a == '0')  return 0;
  16.    else if(a == '1') return 1;
  17.    else if(a == '2') return 2;
  18.    else if(a == '3') return 3;
  19.    else if(a == '4') return 4;
  20.    else if(a == '5') return 5;
  21.    else if(a == '6') return 6;
  22.    else if(a == '7') return 7;
  23.    else if(a == '8') return 8;
  24.    else if(a == '9') return 9;
  25.    else if(a == 'a' || a == 'A') return 10;
  26.    else if(a == 'b' || a == 'B') return 11;
  27.    else if(a == 'c' || a == 'C') return 12;
  28.    else if(a == 'd' || a == 'D') return 13;
  29.    else if(a == 'e' || a == 'E') return 14;
  30.    else if(a == 'f' || a == 'F') return 15;
  31.    else return -1;
  32. }
  33.   /*  Disable interrupt  */
  34. void Close_Rece()
  35. {
  36.     disable();
  37. outportb(tport+1,0);
  38. outportb(tport+4,0);
  39. outportb(irqreg,inportb(irqreg)|irqe);
  40.     enable();
  41.     setvect(irqnum,old_int);
  42. }
  43.       /*  Interrupt subprogramm  */
  44. void interrupt far new_int()
  45. {
  46.    flag=1;
  47.    g=inportb(tport) & 0xff;
  48.    outportb(0x20,0x20);
  49.    outportb(0xa0,0x20);
  50. }
  51.     /*  Interrupt receive  */
  52. void Inter_Rece()
  53. irqnum=0x71;
  54. irqreg=0xa1;
  55. irqs=0xfd;
  56. irqe=0x02;
  57. {
  58. old_int=getvect(irqnum);
  59. disable();
  60. outportb(irqreg,inportb(irqreg) & irqs);
  61. setvect(irqnum,new_int);
  62. enable();
  63. }
  64. main(argc,argv)
  65. int argc;
  66. char *argv[];
  67. {
  68.   int z=0,z1;
  69.   int st,st1,st2,st3,st4,ik,r;
  70.   int st1flag=0,st2flag=0,st3flag=0,st4flag=0;
  71.   int tiflag=0;
  72.   if(argc==1){
  73.   printf("Serial port test kit for all pin.nr");
  74.   printf("You can choice RS232 or RS422 port to test.nr");
  75.   printf("Usage:n  COMTEST [port address(hex)] [IRQ number(dec)]");
  76.   printf("nPress comtest/? to show more help screen.");
  77.   exit(0);
  78.  }
  79.   else if(argc>4){
  80.  printf("Parameter Error!");
  81.  printf("nPress comtest to show help screen.");
  82.  printf("nPress comtest/? to show more help screen.");
  83.  exit(0);
  84.  }
  85.   if((argv[1][0] == '/') && (argv[1][1] == '?'))
  86.   {
  87.  printf("Serial port test kit for all pin.nr");
  88.  printf("You can choice RS232 or RS422 port to test.nr");
  89.  printf("Usage:n  COMTEST [port address(hex)] [IRQ number(dec)]");
  90.  printf("nRS232 Loop Back Box:");
  91.  printf("n      RTS - CTS - DCD");
  92.  printf("n      TXD - RXD");
  93.  printf("n      DTR - DSR - RI");
  94.  printf("nRS422 Loop Back Box:");
  95.  printf("n      TX+ - RX+");
  96.  printf("n      TX- - RX-");
  97.  exit(0);
  98.   }
  99.   tport=dd(argv[1][0])*256+dd(argv[1][1])*16+dd(argv[1][2]);
  100.   if(!(tport>=0x100 && tport<=0x400))
  101.   {
  102.  printf("Parameter Error!");
  103.  printf("nPress comtest to show help screen.");
  104.  printf("nPress comtest/? to show more help screen.");
  105.  exit(0);
  106.   }
  107.   z=dd(argv[2][0]);
  108.   z1=dd(argv[2][1]);
  109.   if(z == 1)
  110.   {
  111. if(z1 == 0) irq=10;
  112. else if(z1 == 1) irq=11;
  113. else if(z1 == 2) irq=12;
  114. else if(z1 == 5) irq=15;
  115.   }
  116.   else  irq=z;
  117.   if(irq == 2)
  118.   {
  119.     irqnum=0x0a;
  120.     irqreg=0x21;
  121.     irqs=0xfb;
  122.     irqe=0x04;
  123.   }
  124.   else if(irq == 3)
  125.   {
  126.     irqnum=0x0b;
  127.     irqreg=0x21;
  128.     irqs=0xf7;
  129.     irqe=0x08;
  130.   }
  131.   else if(irq == 4)
  132.   {
  133.     irqnum=0x0c;
  134.     irqreg=0x21;
  135.     irqs=0xef;
  136.     irqe=0x10;
  137.   }
  138.   else if(irq == 5)
  139.   {
  140.     irqnum=0x0d;
  141.     irqreg=0x21;
  142.     irqs=0xdf;
  143.     irqe=0x20;
  144.   }
  145.   else if(irq == 6)
  146.   {
  147.     irqnum=0x0e;
  148.     irqreg=0x21;
  149.     irqs=0xbf;
  150.     irqe=0x40;
  151.   }
  152.   else if(irq == 7)
  153.   {
  154.     irqnum=0x0f;
  155.     irqreg=0x21;
  156.     irqs=0x7f;
  157.     irqe=0x80;
  158.   }
  159.   else if(irq == 9)
  160.   {
  161. irqnum=0x71;
  162. irqreg=0xa1;
  163. irqs=0xfd;
  164. irqe=0x02;
  165.   }
  166.   else if(irq == 10)
  167.   {
  168.     irqnum=0x72;
  169.     irqreg=0xa1;
  170.     irqs=0xfb;
  171. irqe=0x04;
  172.   }
  173.   else if(irq == 11)
  174.   {
  175.     irqnum=0x73;
  176.     irqreg=0xa1;
  177.     irqs=0xf7;
  178.     irqe=0x08;
  179.   }
  180.   else if(irq == 12)
  181.   {
  182.     irqnum=0x74;
  183.     irqreg=0xa1;
  184.     irqs=0xef;
  185.     irqe=0x10;
  186.   }
  187.   else if(irq == 15)
  188.   {
  189.     irqnum=0x77;
  190.     irqreg=0xa1;
  191.     irqs=0x7f;
  192.     irqe=0x80;
  193.   }
  194.   outportb(tport+3,0x80);
  195.   outportb(tport,0x01);
  196.   outportb(tport+1,0x00);
  197.   outportb(tport+3,0x03);
  198.   outportb(tport+4,0x0b);
  199.   outportb(tport+1,0x01);
  200.   clrscr();
  201.   gotoxy(2,2);
  202.   cprintf(" Serial port loop back tester (RS232 or RS422)       Ver:1.0");
  203.   gotoxy(2,4);
  204.   cprintf(" PORT:");
  205.   gotoxy(12,4);
  206.   if(z > 0)
  207.   cprintf(" IRQ:");
  208.   gotoxy(8,4);
  209.   textcolor(CYAN);
  210.   cprintf(" %X",tport);
  211.   if(z > 0)
  212.   {
  213. gotoxy(17,4);
  214. cprintf(" %d",irq);
  215.   }
  216.   for(ik=0;ik<10;ik++)
  217.   {
  218.   outportb(tport+4,0x0b);
  219.   gotoxy(10,5);
  220.   textcolor(CYAN);
  221.   cprintf("nr             /DCD      /RI      /DSR      /CTS");
  222.   st=inportb(tport+6) & 0xff;
  223.   st1=(st & 0x80)>>7;
  224.   st2=(st & 0x40)>>6;
  225.   st3=(st & 0x20)>>5;
  226.   st4=(st & 0x10)>>4;
  227.   textcolor(GREEN);
  228.   cprintf("nr              %d         %d         %d         %d",st1,st2,st3,st4);
  229.   if(st1 != 1) st1flag=1;
  230.   if(st2 != 1) st2flag=1;
  231.   if(st3 != 1) st3flag=1;
  232.   if(st4 != 1) st4flag=1;
  233.   outportb(tport+4,0x08);
  234.   textcolor(CYAN);
  235.   cprintf("nr             /DCD      /RI      /DSR      /CTS");
  236.   st=inportb(tport+6) & 0xff;
  237.   st1=(st & 0x80)>>7;
  238.   st2=(st & 0x40)>>6;
  239.   st3=(st & 0x20)>>5;
  240.   st4=(st & 0x10)>>4;
  241.   textcolor(GREEN);
  242.   cprintf("nr              %d         %d         %d         %dn",st1,st2,st3,st4);
  243.   if(st1 != 0) st1flag=1;
  244.   if(st2 != 0) st2flag=1;
  245.   if(st3 != 0) st3flag=1;
  246.   if(st4 != 0) st4flag=1;
  247.   }
  248.   if(st1flag == 1)  {  textcolor(BLINK|RED); gotoxy(13,10); cprintf("ERROR"); }
  249.   else              {  textcolor(YELLOW);gotoxy(13,10); cprintf("PASS");  }
  250.   if(st2flag == 1)  {  textcolor(BLINK|RED); gotoxy(23,10); cprintf("ERROR"); }
  251.   else              {  textcolor(YELLOW);gotoxy(23,10); cprintf("PASS");  }
  252.   if(st3flag == 1)  {  textcolor(BLINK|RED); gotoxy(33,10); cprintf("ERROR"); }
  253.   else              {  textcolor(YELLOW);gotoxy(33,10); cprintf("PASS");  }
  254.   if(st4flag == 1)  {  textcolor(BLINK|RED); gotoxy(43,10); cprintf("ERROR"); }
  255.   else              {  textcolor(YELLOW);gotoxy(43,10); cprintf("PASS");  }
  256.   if((inportb(tport+5) & 0x01) == 1)
  257.   {
  258.      inportb(tport);
  259.   }
  260.   t=0;
  261.   gotoxy(2,13);
  262.   textcolor(CYAN);
  263.   cprintf("Check:");
  264.   textcolor(GREEN);
  265.   for(i=0;i<loop;i++)
  266.   {
  267. gotoxy(12,13);
  268. for(ik=0;ik<25;ik++)
  269. {
  270.   outportb(tport,t);
  271. /*   while((inportb(tport+5) & 0x40) != 0x40);  */
  272.   ti=t++;
  273.   if(t>255) t=0;
  274.   delay(10);
  275.   if((inportb(tport+5) & 0x01) == 1)
  276.   {
  277. r=inportb(tport);
  278. cprintf("%2X",r);
  279. if(r != ti) { tiflag=1; break; }
  280.   }
  281.   else
  282.   {
  283. tiflag=1; break;
  284.   }
  285. }
  286.    }
  287.    gotoxy(65,13);
  288.    if(tiflag == 0) { textcolor(YELLOW); cprintf("  PASS"); }
  289.    else            { textcolor(BLINK|RED); cprintf("  ERROR");}
  290.   Inter_Rece();
  291.   if(z > 0)
  292.   {
  293.    tiflag=0;
  294.    t=0;
  295.    gotoxy(1,15);
  296.    textcolor(CYAN);
  297.    cprintf("Interrupt:");
  298.    textcolor(GREEN);
  299.    for(i=0;i<loop;i++)
  300.    {
  301. gotoxy(12,15);
  302. for(ik=0;ik<25;ik++)
  303. {
  304.   outportb(tport,t);
  305.   ti=t++;
  306.   if(t>255) t=0;
  307.   delay(10);
  308.   if(flag == 1)
  309.   {
  310. flag=0;
  311. cprintf("%2X",g);
  312. if(g != ti) { tiflag=1; break; }
  313.   }
  314.   else
  315.   {
  316. tiflag=1; break;
  317.   }
  318.  }
  319. }
  320. gotoxy(65,15);
  321. if(tiflag == 0) { textcolor(YELLOW); cprintf("  PASS"); }
  322. else            { textcolor(BLINK|RED); cprintf("  ERROR");}
  323.    }
  324.    Close_Rece();
  325. }