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

📄 gserial.cpp

📁 共享一个DOS下可重用串口通讯类源代码,易于实现串口通讯
💻 CPP
字号:

  /*------------------------------------------------------------------*
	  GSERIAL.CPP
          一个基于DOS平台的串口通讯类
	  适用于DOS环境下异步串口通信编程 多串口控制程序
          在Turbo C++3.0中选项设置 Options/Compiler/Entry中关闭Test Stack Overflow
          由钱莉东整理完成
          邮箱:copu123@163.com
          参考文献:Visual C++/Turbo C串口通信编程实践
  *------------------------------------------------------------------*/

#include "GSerial.h"
#include <dos.h>


//////////////////////////////////GSerial////////
GSerial::GSerial()
{
}

GSerial::~GSerial()
{
}

//*  get status of the port  */
int GSerial::ReadStatus(void)
{
  return(inp(m_unPortBase+5));
}

/*  send one valid char from the port  */
void GSerial::SendChar(unsigned char unCh)
{
   while ((ReadStatus() & 0x40) == 0);
   outportb(m_unPortBase,unCh);
}

/*  send one string from the port  */
void GSerial::SendString(int nStrlen, unsigned char *unChBuf)
{
	int k=0;	
	do {
		SendChar(*(unChBuf + k));
		k++;
	} while ((k < nStrlen));
}


/* Install our functions to handle communications */
void GSerial::SetVects(void interrupt(*New_Int)(...))
{
	disable();
	OldVects = getvect(InterruptNo[m_unPortNo-1]);
	setvect(InterruptNo[m_unPortNo-1], New_Int);
	enable();
}

/* Uninstall our vectors before exiting the program */
void GSerial::ResetVects(void)
{
	setvect(InterruptNo[m_unPortNo-1], OldVects);
}



/* Tell modem that we're ready to go */
void GSerial::CommOn(void)
{
	int temp;
	disable();
	//temp = inportb(m_unPortBase + MCR) | MCR_INT;
	//outportb(m_unPortBase + MCR, temp);
	outportb(m_unPortBase + MCR, MCR_INT);
	//temp = inportb(m_unPortBase + MCR) | MCR_DTR | MCR_RTS;
	//outportb(m_unPortBase + MCR, temp);
	temp = (inportb(m_unPortBase + IER)) | IER_RX_INT;//|IER_TX_INT;
	outportb(m_unPortBase + IER, temp);
	temp = inportb(PIC8259_IMR) & ComIRQ[m_unPortNo-1];
	outportb(PIC8259_IMR, temp);
	enable();
}

/* Go off-line */
void GSerial::CommOff(void)
{
	 int  temp;

	disable();
	temp = inportb(PIC8259_IMR) | ~ComIRQ[m_unPortNo-1];
	outportb(PIC8259_IMR, temp);
	outportb(m_unPortBase + IER, 0);
	outportb(m_unPortBase + MCR, 0);
	enable();
}



/* Set the port number to use */
int GSerial::SetPortBaseAddr(int Port)
{
	if((Port<1)||(Port>6))
		return(-1);
	m_unPortNo = Port;
	m_unPortBase = PortBaseAddr[m_unPortNo-1];
	return (0);
}

/* This routine sets the speed; will accept funny baud rates. */
/* Setting the speed requires that the DLAB be set on.        */
int GSerial::SetSpeed(int Speed)
{
	char	c;
	int		divisor;

	if (Speed == 0)            /* Avoid divide by zero */
		return (-1);
	else
		divisor = (int) (115200L/Speed);

	if (m_unPortBase == 0)
		return (-1);

	disable();
	c = inportb(m_unPortBase + LCR);
	outportb(m_unPortBase + LCR, (c | 0x80)); /* Set DLAB */
	outportb(m_unPortBase + DLL, (divisor & 0x00FF));
	outportb(m_unPortBase + DLH, ((divisor >> 8) & 0x00FF));
	outportb(m_unPortBase + LCR, c);          /* Reset DLAB */
	enable();

	return (0);
}

/* Set other DATA Format communications parameters */
int GSerial::SetDataFormat(int Parity, int Bits, int StopBit)
{
	int  setting;

	if (m_unPortBase == 0)
		return (-1);
	if (Bits < 5 || Bits > 8)
		return (-1);
	if (StopBit != 1 && StopBit != 2)
		return (-1);
	if (Parity != LCR_NO_PARITY && Parity != LCR_ODD_PARITY && Parity != LCR_EVEN_PARITY)
		return (-1);

	setting  = Bits-5;
	setting |= ((StopBit == 1) ? 0x00 : 0x04);
	setting |= Parity;

	disable();
	outportb(m_unPortBase + LCR, setting);
	enable();

	return (0);
}


void GSerial::CloseSerialPort(void)
{
	CommOff();
	ResetVects();
}

/* Set up the port */
int GSerial::InitSerialPort(int Port, int Speed, int Parity, int Bits, int StopBit)
{
	int flag = 0;
	if (SetPortBaseAddr(Port))
	  flag = -1;
	if (SetSpeed(Speed))
	  flag = -1;
	if (SetDataFormat(Parity, Bits, StopBit))
	  flag = -1;
	return(flag);
}





⌨️ 快捷键说明

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