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

📄 ji ou.cpp

📁 实现RS232 DB9的串口通信并且能够实现奇偶效验
💻 CPP
字号:

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>

#define BUFFLEN 1024

void InitCOM();  //初始化串口
void OpenPort();  //打开串口
void ClosePort(); //关闭串口
//新的中断函数
void interrupt far  asyncint(...);
//中断向量:用于保存中断现场
void interrupt(*asyncoldvect)(...);

unsigned char Buffer[BUFFLEN];
int buffin=0;
int buffout=0;
//unsigned char ch;


//打开COM1
void OpenPort()
{
	unsigned char ucTemp;
	InitCOM();  //初始化串口

	//读入由参数给定的中断向量值
	asyncoldvect=getvect(0x0c);
	disable();       //关中断
	inportb(0x3f8);
	inportb(0x3fe);
	inportb(0x3fb);
	inportb(0x3fa);
	outportb(0x3fc,0x08|0x0b);
	outportb(0x3f9,0x01);
	ucTemp=inportb(0x21)&0xef;
	outportb(0x21,ucTemp);
	setvect(0x0c,asyncint);
	enable();       //开中断
}

//中断服务程序,从COM1接收数据

void interrupt far asyncint(...)
{
	//unsigned char ch;
	Buffer[buffin++] = inportb(0x3f8);// 读字符到缓冲区
	if (buffin >= BUFFLEN)  // 缓冲区满
		buffin=0;           // 指针复位
	outportb(0x20,0x20);
}

void ClosePort(void) //关闭中断
{
	disable();
	outportb(0x3f9,0x00);
	outportb(0x3fc,0x00);
	outportb(0x21,inportb(0x21)&0x10);
	enable();
	setvect(0x0c,asyncoldvect);
}

void InitCOM()// 对COM1串口初始化,设置串口参数
{

	outportb(0x3fb,0x80);  //将设置波特率

	outportb(0x3f8,0x0C);   //波特率为9600bps
	outportb(0x3f9,0x00);


	outportb(0x3fb,0x03);   //8个数据位,1个停止位、无奇偶校验

	outportb(0x3fc,0x08|0x0b);
	outportb(0x3f9,0x01);
}


unsigned char read_char(void)
{
	unsigned unch;
	if(buffout != buffin)
	{
		unch = Buffer[buffout];
		buffout++;
		if(buffout >= BUFFLEN)
			buffout=0;
		return(unch);
	}
	else
		return(0xff);
}



void send_char(unsigned char unch)
{
   while ( ((inp( 0x3f8 + 5)) & 0x40 ) == 0); //和0x40相与 可取出D6位进行判断
   outportb(0x3f8 , unch);
}


//以下为主函数
void main()     //*******************************************************
{
	unsigned char unChar;
	int Exit_mark=0;
	int check=0;
	char a1,a2,a3;
	int b1,b2,b3,j=0;
	char choice='s';
	int marksend=0;
	int markreceive=0;
	unsigned  char shen[5];
	clrscr();
	OpenPort(); //打开串口

	fprintf(stdout, "\n\nPlease input your choice!\n"
			"1.press [ESC] to quit...\n2.press 's' to send!\n3.press 'r' to receive! \n\n");
       while(1)
      {	choice=getch();
	if(choice=='s')
	 {marksend=1;
	  printf("%cend:\n",choice);
	 break; }

	else if(choice=='r')
	 {markreceive=1;
	   printf("%ceceive:\n",choice);
	 break;}

	else if(choice==0x1B)                          //ESC key
		  exit(0);
	 else printf("You must make a choice!");
	 }    //while is over!!

	//xuan zhe tongxun mode is over!!!!!!!!!!!!!!!!!!!!!!!!!

	do {

		if(marksend)
		{   unChar=getch();
		  if(unChar==0x1B)
		 // exit(0);                       //or use break!!!!!%^&
		 break;
		if(unChar=='1') check++;
		send_char(unChar); //send digital
		printf("%c",unChar);
		}




		if(markreceive)   //run the receiver work!!

		{

		do {

		if (kbhit())
		{
			unChar=getch();

			switch (unChar)
			{
			case 0x1B:   //ESC的ASCII值为27
				Exit_mark = 1;  /* Exit program */
				break;

			}

		}

		unChar = read_char();  //从缓冲区中读数
		if (unChar!='0'&&unChar!='1'){shen[j]=unChar; j++;}
			       if(j==4){Exit_mark=1; }


		if (unChar != 0xff)
		{        if(unChar=='1') {check++;}

			fprintf(stdout,"%c",unChar);
		}
	} while (!Exit_mark);    //do the receive work!!!!

	       }
	} while (!Exit_mark);    //do while sentence is over!!!!!!!


b1=check/100;
b2=check-b1*100;
b2=b2/10;
b3=check-b1*100-b2*10;


       switch(b1)
{
       case 1  :  a1='a';  break;
       case 2  :  a1='b';  break;
       case 3  :  a1='c';  break;
       case 4  :  a1='d';  break;
       case 5  :  a1='e';  break;
       case 6  :  a1='f';  break;
       case 7  :  a1='g';  break;
       case 8  :  a1='h';  break;
       case 9  :  a1='i';  break;

       default:    a1='z';

}
switch(b2)
{
       case 1  :  a2='a';  break;
       case 2  :  a2='b';  break;
       case 3  :  a2='c';  break;
       case 4  :  a2='d';  break;
       case 5  :  a2='e';  break;
       case 6  :  a2='f';  break;
       case 7  :  a2='g';  break;
       case 8  :  a2='h';  break;
       case 9  :  a2='i';  break;

       default:    a2='z';

}

switch(b3)
{
       case 1  :  a3='a';  break;
       case 2  :  a3='b';  break;
       case 3  :  a3='c';  break;
       case 4  :  a3='d';  break;
       case 5  :  a3='e';  break;
       case 6  :  a3='f';  break;
       case 7  :  a3='g';  break;
       case 8  :  a3='h';  break;
       case 9  :  a3='i';  break;

       default:    a3='z';

}
 if(choice=='s')
 {
 fprintf(stdout,"%c%c%c",a1,a2,a3);
	send_char(a1);
	send_char(a2);
	send_char(a3);
      getch();                //send is all over!!!!!

	do {                 //receive the result!!

		if (kbhit())
		{
			unChar=getch();

			switch (unChar)
			{
			case 0x1B:   //ESC的ASCII值为27
				Exit_mark = 1;  /* Exit program */
				break;

			}

		}

		unChar = read_char();  //从缓冲区中读数
		if (unChar != 0xff)
		{       if(unChar=='1')
		       { check++;}
			fprintf(stdout,"%c",unChar);
		}
	} while (!Exit_mark);    //do the receive work!!!!
       }// send mode!!
	if(choice=='r')
	    {if(shen[0]==a1&&shen[1]==a2&&shen[2]==a3) {send_char('R');  }
	       else {send_char('F');}

	    }


	ClosePort(); //关闭串口
}

⌨️ 快捷键说明

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