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

📄 打开串口操作.txt

📁 这是用C++作的
💻 TXT
字号:
/*
* 函数介绍:打开串口操作
* 输入参数:struct SerialPort *port
* 输出参数:无
* 返回值  :0-〉正常,1-〉异常
*/
int ComPortOpen(struct SerialPort *port)
{
unsigned char Parameters;
unsigned int nMaxPorts;
const unsigned int BaudTable [] = { 0x0417, 0x0300, 0x0180, 0x00C0, 0x0060,
0x0030, 0x0018, 0x000C, 0x0006, 0x0003 };

port->m_nParity = 0;/*none*/
port->m_nStopBits = 1;
port->m_nDataBits = 8;

port->m_nIntInstalled = 0;
port->m_nInHead = 0;
port->m_nInTail = 0;
port->m_nCarrier = 0;


if (port->m_nPort == 0)/*如果是com1*/
{
port->m_nTHR = 0x3F8;
port->m_nRHR = 0x3F8;
port->m_nDLL = 0x3F8;
port->m_nIER = 0x3F9;
port->m_nDLM = 0x3F9;
port->m_nIIR = 0x3FA;
port->m_nLCR = 0x3FB;
port->m_nMCR = 0x3FC;
port->m_nLSR = 0x3FD;
port->m_nMSR = 0x3FE;
}
else if (port->m_nPort == 1)/*如果是com2*/
{
port->m_nTHR = 0x2f8;
port->m_nRHR = 0x2f8;
port->m_nDLL = 0x2F8;
port->m_nIER = 0x2F9;
port->m_nDLM = 0x2F9;
port->m_nIIR = 0x2FA;
port->m_nLCR = 0x2FB;
port->m_nMCR = 0x2FC;
port->m_nLSR = 0x2FD;
port->m_nMSR = 0x2FE;
}
else
{
printf("unsupported serial port:com%d\n",port->m_nPort+1);
return 1;
}



nMaxPorts = (biosequip () & 0x0E00) >> 9;

if (port->m_nPort < nMaxPorts)
{
disable ();
outportb (port->m_nMCR, 0x00);
outportb (port->m_nLCR, inportb (port->m_nLCR) | 0x80);
outportb (port->m_nDLL, BaudTable [port->m_nBaudIndex] & 0x00FF);
outportb (port->m_nDLM, (BaudTable [port->m_nBaudIndex] & 0xFF00) >> 0x08);
Parameters = (port->m_nDataBits - 5) & 0x03;
Parameters = Parameters | (((port->m_nStopBits - 1) << 2) & 0x04);
Parameters = Parameters | ((port->m_nParity << 3) & 0x38);
outportb (port->m_nLCR, Parameters);
outportb (port->m_nMCR, 0x0B);
enable ();
}
else
{
printf ("Error!  COM%u not available\n", port->m_nPort+1);
return 1;
}

if ((port->m_nIntInstalled == 0) && (port->m_nPort < nMaxPorts))
{
disable ();
port->m_nInTail = 0;
port->m_nInHead = 0;
port->m_nCarrier = ((0x80 & inportb (port->m_nMSR)) == 0x80);
outportb (port->m_nLCR, inportb (port->m_nLCR) & 0x7F);
outportb (port->m_nIER, 0x00);
if (inportb (port->m_nLSR) != 0) /* Nothing */
;
if (inportb (port->m_nRHR) != 0) /* Nothing */
;
port->OldIntVector = getvect (0x0C - port->m_nPort);
setvect (0x0C - port->m_nPort, ComPortIntHandler);
port->m_nIntInstalled = 1;
switch (port->m_nPort)
{
case 0:
outportb (0x21, inportb (0x21) & 0xEF);
break;
case 1 :
outportb (0x21, inportb (0x21) & 0xF7);
break;
default :
break;
}

outportb (port->m_nLCR, inportb (port->m_nLCR) & 0x7F);
outportb (port->m_nIER, 0x09);
outportb (port->m_nMCR, 0x0B);
enable ();

printf ("COM%u Opened succeed,m_nBaudIndex=%d\n",
port->m_nPort + 1,port->m_nBaudIndex);
return 0;
}
else
{
printf ("Error!  COM%u \n", port->m_nPort + 1);
if (port->m_nIntInstalled == 1)
{
puts ("interrupt already installed\n");
}
else
{
puts ("not available\n");
}
return 1;
}
}
/*
* 函数介绍:关闭串口操作
* 输入参数:struct SerialPort *port
* 输出参数:无
* 返回值  :0-〉正常,1-〉异常
*/
int ComPortClose(struct SerialPort *port)
{
if (port->m_nIntInstalled)
{
disable ();
switch (port->m_nPort)
{
case 0 :
outportb (0x21, inportb (0x21) | 0x10);
break;
case 1 :
outportb (0x21, inportb (0x21) | 0x08);
break;
}
outportb (port->m_nLCR, inportb (port->m_nLCR) & 0x7F);
outportb (port->m_nIER, 0x00);
outportb (port->m_nMCR, 0x00);
setvect (0x0C - port->m_nPort, port->OldIntVector);
port->m_nIntInstalled = 0;
enable ();

printf ("COM%u Closed succeed!\n", port->m_nPort + 1);
return 0;
}
else
{
printf ("Error!  COM%u interrupt is not installed\n", port->m_nPort+1);
return 1;
}
}


/*
* 函数介绍:向串口发送数据
* 输入参数:struct SerialPort *port,const unsigned char* chData,unsigned int nlen
* 输出参数:无
* 返回值  :0-〉正常,1-〉异常
*/
int ComPortTX(const struct SerialPort *port,unsigned char key)
{

unsigned int TimeLoop;
TimeLoop = 0;
while ((TimeLoop < 0xFFFF)&&((inportb (port->m_nLSR) & 0x20) != 0x20))
{
TimeLoop += 1;
}
if (TimeLoop != 0xFFFF)
{
disable ();
outportb (port->m_nLCR, inportb (port->m_nLCR) & 0x7F);
outportb (port->m_nTHR, key);
printf("%d ",key);
enable ();
}
else
{

printf ("Timeout on COM%u\n", port->m_nPort+1);
return 1;
}


return 0;
}


/*
* 函数介绍:从串口接收数据
* 输入参数:struct SerialPort *port
* 输出参数:unsigned char* chData,unsigned int *nRXLen接收数据的长度
* 返回值  :0-〉正常,1-〉异常
*/
int ComPortRX(struct SerialPort *port,unsigned char* chData,unsigned int *nRXLen)
{
unsigned int i;

*nRXLen = 0;

/*delay(10);*/
if (port->m_nInTail != port->m_nInHead)/*如果当前串口有数据过来*/
{
while (port->m_nInHead != port->m_nInTail)/*把读到的每个字节保存在buffer中*/
{
chData[*nRXLen] = port->m_InBuffer[port->m_nInHead];
port->m_nInHead = (port->m_nInHead+1)%BUFSZ;
(*nRXLen)++;/*每接收到一个字符,*nRXLen就加一*/
}


printf("Received data from COM%d:\n",port->m_nPort+1);
for (i=0; i<*nRXLen; i++)
{
printf("%d ",chData[i]);
}
printf("%d\n",*nRXLen);
return 0;
}
else
{
/*printf("Error:Received NO data from COM%d:\n",port->m_nPort+1);*/
return 1;
}

}
main()
{   unsigned char *chData,key;
    unsigned int *nRXLen;

    com->m_nPort=0;
    com->m_nBaudIndex=0;
   ComPortOpen(com);
   randomize();
    do{

key=kbhit();
    if(key!=0)
key='q';
        ComPortRX(com,chData,nRXLen);
    }
    while (key!='q');

   ComPortClose(com);
   getch();
}

⌨️ 快捷键说明

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