📄 initserial.cpp
字号:
/*
用于串口类的初始化过程
*/
#include"gserial.h"
#include "stdio.h"
#include "conio.h"
#include "dos.h"
struct COMPORT_VAR{
char inbuf[IBUF_LEN]; // in buffer
char outbuf[OBUF_LEN]; // out buffer
unsigned int startbuf ;
unsigned int endbuf ;
unsigned int inhead ;
unsigned int intail ;
unsigned int outhead ;
unsigned int outtail ;
unsigned int PortBase ;
};
COMPORT_VAR comport1,comport2;
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////COM1//////////////////////////////////////////////////////////
void interrupt ComIntServ_comport1(...)
{
int temp;
disable();
temp = (inportb(comport1.PortBase+IIR)) & IIR_MASK; // why interrupt was called
switch(temp)
{
case 0x00: // modem status changed
inportb(comport1.PortBase+MSR); // read in useless char
break;
case 0x02: // Request To Send char
if (comport1.outhead != comport1.outtail) // there's a char to send
{
outportb(comport1.PortBase+TXR,comport1.outbuf[comport1.outhead++]); // send the character
if (comport1.outhead == OBUF_LEN)
comport1.outhead=0; // if at end of buffer, reset pointer
}
break;
case 0x04: // character ready to be read in
//inbuf[inhead++] = inportb(m_unPortBase+RXR);// read character into inbuffer
comport1.inbuf[comport1.inhead] = inportb(comport1.PortBase+RXR);// read character into inbuffer
comport1.inhead++;
if (comport1.inhead == IBUF_LEN) // if at end of buffer
comport1.inhead=0; // reset pointer
break;
case 0x06: // line status has changed
inportb(comport1.PortBase+LSR); // read in useless char
break;
default:
break;
}
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
enable(); // reenable interrupts at the end of the handler
}
//COM1 串口1
char ReadChar_comport1(void)
{
char ch;
if (comport1.inhead != comport1.intail) // there is a character
{
disable(); // disable irqs while getting char
ch = comport1.inbuf[comport1.intail++]; // get character from buffer
if (comport1.intail == IBUF_LEN) // if at end of in buffer
comport1.intail=0; // reset pointer
enable(); // re-enable interrupt
return(ch); // return the char
}
ch = -1;
return(ch); // return nothing
}
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////COM2//////////////////////////////////////////////////////////
void interrupt ComIntServ_comport2(...)
{
int temp;
disable();
temp = (inportb(comport2.PortBase+IIR)) & IIR_MASK; // why interrupt was called
switch(temp)
{
case 0x00: // modem status changed
inportb(comport2.PortBase+MSR); // read in useless char
break;
case 0x02: // Request To Send char
if (comport2.outhead != comport2.outtail) // there's a char to send
{
outportb(comport2.PortBase+TXR,comport2.outbuf[comport2.outhead++]); // send the character
if (comport2.outhead == OBUF_LEN)
comport2.outhead=0; // if at end of buffer, reset pointer
}
break;
case 0x04: // character ready to be read in
//inbuf[inhead++] = inportb(m_unPortBase+RXR);// read character into inbuffer
comport2.inbuf[comport2.inhead] = inportb(comport2.PortBase+RXR);// read character into inbuffer
comport2.inhead++;
if (comport2.inhead == IBUF_LEN) // if at end of buffer
comport2.inhead=0; // reset pointer
break;
case 0x06: // line status has changed
inportb(comport2.PortBase+LSR); // read in useless char
break;
default:
break;
}
outportb(PIC8259_ICR, PIC8259_EOI); // Signal end of hardware interrupt
enable(); // reenable interrupts at the end of the handler
}
//COM2 串口2
char ReadChar_comport2(void)
{
char ch;
if (comport2.inhead != comport2.intail) // there is a character
{
disable(); // disable irqs while getting char
ch = comport2.inbuf[comport2.intail++]; // get character from buffer
if (comport2.intail == IBUF_LEN) // if at end of in buffer
comport2.intail=0; // reset pointer
enable(); // re-enable interrupt
return(ch); // return the char
}
ch = -1;
return(ch); // return nothing
}
///////
main() //sdfhksg
{
/* Communications parameters */
comport1.startbuf =0;
comport1.endbuf =0;
comport1.inhead =0;
comport1.intail =0;
comport1.outhead =0;
comport1.outtail =0;
comport1.PortBase =0;
comport2.startbuf =0;
comport2.endbuf =0;
comport2.inhead =0;
comport2.intail =0;
comport2.outhead =0;
comport2.outtail =0;
comport2.PortBase =0;
int port = COM1;
int speed = 9600;
int parity = LCR_NO_PARITY;
int bits = 8;
int stopbits = 1;
int done = FALSE;
char c;
int temp;
int SError=0;
GSerial gsCOM1, gsCOM2; //定义两个GSerial对象
//初始化COM1,串口1
if (!gsCOM1.InitSerialPort(port, speed, parity, bits, stopbits))
{
comport1.PortBase = PortBaseAddr[port-1];
gsCOM1.SetVects(ComIntServ_comport1);
gsCOM1.CommOn();
}
else
SError=2;
//初始化COM2,串口2
port = COM2;
if (!gsCOM2.InitSerialPort(port, speed, parity, bits, stopbits))
{
comport2.PortBase = PortBaseAddr[port-1];
gsCOM2.SetVects(ComIntServ_comport2);
gsCOM2.CommOn();
}
else
SError=2;
do {
if (kbhit())
{
c = getch();
/* Look for an Escape key */
switch (c)
{
case ESC:
done = TRUE; /* Exit program */
break;
}
if (!done)
{
gsCOM1.SendChar( c );
fprintf(stdout,"\n\n[COM1:TX]: %c\n",c);
}
}
c = ReadChar_comport1();
if (c != -1) //'-1' is the END signal of a string
{
fprintf(stdout,"[COM1:RX]: %c\n",c);
}
delay(50);
c = ReadChar_comport2();
if (c != -1) //'-1' is the END signal of a string
{
fprintf(stdout,"[COM2:RX]: %c\n",c);
gsCOM2.SendChar( c );
fprintf(stdout,"[COM2:TX]: %c\n",c);
}
} while ((!done) && (!SError));
//关闭打开的串口
gsCOM1.CloseSerialPort();
gsCOM2.CloseSerialPort();
/* Check for errors */
switch (SError)
{
case NO_ERROR: fprintf(stderr, "\nbye.\n");
return (0);
case BUF_OVFL: fprintf(stderr, "\nBuffer Overflow.\n");
return (99);
case 2: fprintf(stderr,"\n Cannot init serial port");
return(2);
default: fprintf(stderr, "\nUnknown Error, SError = %d\n",
SError);
return (99);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -