⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comtest.c

📁 DOS环境下,使用Borlandc开发的串口通讯程序
💻 C
字号:
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<string.h>
#include<bios.h>

int flag=0,g,t=0,tt,t1=0,irq,irqnum,irqreg,irqs,irqe;
unsigned int tport;
int i,ti,loop=30;
void interrupt far new_int();
void interrupt(*old_int)();


int dd(char a)
{
   if(a == '0')  return 0;
   else if(a == '1') return 1;
   else if(a == '2') return 2;
   else if(a == '3') return 3;
   else if(a == '4') return 4;
   else if(a == '5') return 5;
   else if(a == '6') return 6;
   else if(a == '7') return 7;
   else if(a == '8') return 8;
   else if(a == '9') return 9;
   else if(a == 'a' || a == 'A') return 10;
   else if(a == 'b' || a == 'B') return 11;
   else if(a == 'c' || a == 'C') return 12;
   else if(a == 'd' || a == 'D') return 13;
   else if(a == 'e' || a == 'E') return 14;
   else if(a == 'f' || a == 'F') return 15;
   else return -1;
}



	  /*  Disable interrupt  */

void Close_Rece()
{

	    disable();
		outportb(tport+1,0);
		outportb(tport+4,0);
		outportb(irqreg,inportb(irqreg)|irqe);
	    enable();
	    setvect(irqnum,old_int);
}



	      /*  Interrupt subprogramm  */

void interrupt far new_int()
{
		   flag=1;
		   g=inportb(tport) & 0xff;

		   outportb(0x20,0x20);
		   outportb(0xa0,0x20);
}

	    /*  Interrupt receive  */

void Inter_Rece()

	irqnum=0x71;
	irqreg=0xa1;
	irqs=0xfd;
	irqe=0x02;

{
	old_int=getvect(irqnum);
	disable();
	outportb(irqreg,inportb(irqreg) & irqs);
	setvect(irqnum,new_int);
	enable();
}


main(argc,argv)
int argc;
char *argv[];
{
  int z=0,z1;
  int st,st1,st2,st3,st4,ik,r;
  int st1flag=0,st2flag=0,st3flag=0,st4flag=0;
  int tiflag=0;
  if(argc==1){
			  printf("Serial port test kit for all pin.\n\r");
			  printf("You can choice RS232 or RS422 port to test.\n\r");
			  printf("Usage:\n  COMTEST [port address(hex)] [IRQ number(dec)]");
			  printf("\nPress comtest/? to show more help screen.");
			  exit(0);
		 }

  else if(argc>4){
		 printf("Parameter Error!");
		 printf("\nPress comtest to show help screen.");
		 printf("\nPress comtest/? to show more help screen.");
		 exit(0);
		 }

  if((argv[1][0] == '/') && (argv[1][1] == '?'))
  {
		 printf("Serial port test kit for all pin.\n\r");
		 printf("You can choice RS232 or RS422 port to test.\n\r");
		 printf("Usage:\n  COMTEST [port address(hex)] [IRQ number(dec)]");
		 printf("\nRS232 Loop Back Box:");
		 printf("\n      RTS - CTS - DCD");
		 printf("\n      TXD - RXD");
		 printf("\n      DTR - DSR - RI");
		 printf("\nRS422 Loop Back Box:");
		 printf("\n      TX+ - RX+");
		 printf("\n      TX- - RX-");
		 exit(0);
  }
  tport=dd(argv[1][0])*256+dd(argv[1][1])*16+dd(argv[1][2]);
  if(!(tport>=0x100 && tport<=0x400))
  {
		 printf("Parameter Error!");
		 printf("\nPress comtest to show help screen.");
		 printf("\nPress comtest/? to show more help screen.");
		 exit(0);
  }

  z=dd(argv[2][0]);
  z1=dd(argv[2][1]);

  if(z == 1)
  {
	if(z1 == 0) irq=10;
	else if(z1 == 1) irq=11;
	else if(z1 == 2) irq=12;
	else if(z1 == 5) irq=15;
  }
  else  irq=z;

  if(irq == 2)
  {
    irqnum=0x0a;
    irqreg=0x21;
    irqs=0xfb;
    irqe=0x04;
  }
  else if(irq == 3)
  {
    irqnum=0x0b;
    irqreg=0x21;
    irqs=0xf7;
    irqe=0x08;
  }
  else if(irq == 4)
  {
    irqnum=0x0c;
    irqreg=0x21;
    irqs=0xef;
    irqe=0x10;
  }
  else if(irq == 5)
  {
    irqnum=0x0d;
    irqreg=0x21;
    irqs=0xdf;
    irqe=0x20;
  }
  else if(irq == 6)
  {
    irqnum=0x0e;
    irqreg=0x21;
    irqs=0xbf;
    irqe=0x40;
  }
  else if(irq == 7)
  {
    irqnum=0x0f;
    irqreg=0x21;
    irqs=0x7f;
    irqe=0x80;
  }
  else if(irq == 9)
  {
	irqnum=0x71;
	irqreg=0xa1;
	irqs=0xfd;
	irqe=0x02;
  }
  else if(irq == 10)
  {
    irqnum=0x72;
    irqreg=0xa1;
    irqs=0xfb;
	irqe=0x04;
  }
  else if(irq == 11)
  {
    irqnum=0x73;
    irqreg=0xa1;
    irqs=0xf7;
    irqe=0x08;
  }
  else if(irq == 12)
  {
    irqnum=0x74;
    irqreg=0xa1;
    irqs=0xef;
    irqe=0x10;
  }
  else if(irq == 15)
  {
    irqnum=0x77;
    irqreg=0xa1;
    irqs=0x7f;
    irqe=0x80;
  }

  outportb(tport+3,0x80);
  outportb(tport,0x01);
  outportb(tport+1,0x00);
  outportb(tport+3,0x03);
  outportb(tport+4,0x0b);
  outportb(tport+1,0x01);

  clrscr();
  gotoxy(2,2);
  cprintf(" Serial port loop back tester (RS232 or RS422)       Ver:1.0");
  gotoxy(2,4);
  cprintf(" PORT:");
  gotoxy(12,4);
  if(z > 0)
  cprintf(" IRQ:");
  gotoxy(8,4);
  textcolor(CYAN);
  cprintf(" %X",tport);
  if(z > 0)
  {
	gotoxy(17,4);
	cprintf(" %d",irq);
  }
  for(ik=0;ik<10;ik++)
  {
  outportb(tport+4,0x0b);
  gotoxy(10,5);
  textcolor(CYAN);
  cprintf("\n\r             /DCD      /RI      /DSR      /CTS");
  st=inportb(tport+6) & 0xff;
  st1=(st & 0x80)>>7;
  st2=(st & 0x40)>>6;
  st3=(st & 0x20)>>5;
  st4=(st & 0x10)>>4;
  textcolor(GREEN);
  cprintf("\n\r              %d         %d         %d         %d",st1,st2,st3,st4);
  if(st1 != 1) st1flag=1;
  if(st2 != 1) st2flag=1;
  if(st3 != 1) st3flag=1;
  if(st4 != 1) st4flag=1;
  outportb(tport+4,0x08);
  textcolor(CYAN);
  cprintf("\n\r             /DCD      /RI      /DSR      /CTS");
  st=inportb(tport+6) & 0xff;
  st1=(st & 0x80)>>7;
  st2=(st & 0x40)>>6;
  st3=(st & 0x20)>>5;
  st4=(st & 0x10)>>4;
  textcolor(GREEN);
  cprintf("\n\r              %d         %d         %d         %d\n",st1,st2,st3,st4);
  if(st1 != 0) st1flag=1;
  if(st2 != 0) st2flag=1;
  if(st3 != 0) st3flag=1;
  if(st4 != 0) st4flag=1;
  }
  if(st1flag == 1)  {  textcolor(BLINK|RED); gotoxy(13,10); cprintf("ERROR"); }
  else              {  textcolor(YELLOW);gotoxy(13,10); cprintf("PASS");  }
  if(st2flag == 1)  {  textcolor(BLINK|RED); gotoxy(23,10); cprintf("ERROR"); }
  else              {  textcolor(YELLOW);gotoxy(23,10); cprintf("PASS");  }
  if(st3flag == 1)  {  textcolor(BLINK|RED); gotoxy(33,10); cprintf("ERROR"); }
  else              {  textcolor(YELLOW);gotoxy(33,10); cprintf("PASS");  }
  if(st4flag == 1)  {  textcolor(BLINK|RED); gotoxy(43,10); cprintf("ERROR"); }
  else              {  textcolor(YELLOW);gotoxy(43,10); cprintf("PASS");  }

  if((inportb(tport+5) & 0x01) == 1)
  {
     inportb(tport);
  }

  t=0;
  gotoxy(2,13);
  textcolor(CYAN);
  cprintf("Check:");
  textcolor(GREEN);
  for(i=0;i<loop;i++)
  {
	gotoxy(12,13);
	for(ik=0;ik<25;ik++)
	{
	  outportb(tport,t);
/*	  while((inportb(tport+5) & 0x40) != 0x40);  */
	  ti=t++;
	  if(t>255) t=0;
	  delay(10);
	  if((inportb(tport+5) & 0x01) == 1)
	  {
		r=inportb(tport);
		cprintf("%2X",r);
		if(r != ti) { tiflag=1; break; }
	  }
	  else
	  {
		tiflag=1; break;
	  }
	}
   }
   gotoxy(65,13);
   if(tiflag == 0) { textcolor(YELLOW); cprintf("  PASS"); }
   else            { textcolor(BLINK|RED); cprintf("  ERROR");}

  Inter_Rece();

  if(z > 0)
  {
   tiflag=0;
   t=0;
   gotoxy(1,15);
   textcolor(CYAN);
   cprintf("Interrupt:");
   textcolor(GREEN);
   for(i=0;i<loop;i++)
   {
	gotoxy(12,15);
	for(ik=0;ik<25;ik++)
	{
	  outportb(tport,t);
	  ti=t++;
	  if(t>255) t=0;
	  delay(10);
	  if(flag == 1)
	  {
		flag=0;
		cprintf("%2X",g);
		if(g != ti) { tiflag=1; break; }
	  }
	  else
	  {
		tiflag=1; break;
	  }

	 }
	}
	gotoxy(65,15);
	if(tiflag == 0) { textcolor(YELLOW); cprintf("  PASS"); }
	else            { textcolor(BLINK|RED); cprintf("  ERROR");}
   }
   Close_Rece();
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -