69,336
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <dos.h>
#define LSB 0
#define MSB 1
#define RXR 0
#define TXR 0
#define IER 1
#define IIR 2
#define LCR 3
#define MCR 4
#define LSR 5
#define MSR 6
#define Com1_base 0x3f8
#define uchar unsigned char
volatile uchar inputData;
volatile uchar onInput = 0x00;
volatile uchar onOutput = 0x00;
void interrupt (*OldVect)();
void interrupt SerialISR();
void InitCom()
{
uchar inttemp;
/*设置波特率什么的*/
outportb(Com1_base+LCR,0x80);
outportb(Com1_base+LSB,0x0c);
outportb(Com1_base+MSB,0x00);
outportb(Com1_base+LCR,0x03);
outportb(Com1_base+IER,0x01);
/*设置中断向量*/
OldVect = getvect(0x0c);
disable();
inttemp = inportb(0x21)&0xef;
outportb(0x21,inttemp);
setvect(0x0c,SerialISR);
enable();
}
void CloseCom()
{
disable();
outportb(Com1_base+IER,0x00);
outportb(Com1_base,0x00);
outportb(0x21,inportb(0x21)|~(0xef));
setvect(0x0c,OldVect);
}
void interrupt SerialISR()
{
/*串口中断服务代码*/
inputData = inportb(Com1_base+RXR);
onInput = 0x01;
/*服务代码结束*/
outportb(0x20,0x20); /*中断结束的代码*/
}
void SendChar(uchar key)
{
while( ((inportb(Com1_base + LSR)) & 0x40) == 0);
{
outportb(Com1_base + TXR,key);
}
}
void main()
{
uchar key;
uchar bExit_flag = 0x00;
InitCom();
while(!bExit_flag)
{
if(onInput == 0x01)
{
printf("c:%c",inputData);
printf("d:%d",inputData);
printf("s:%s \n",inputData);
onInput = 0x00;
}
if(kbhit())
{
switch(key = getch())
{
case 0x1b:
bExit_flag = 0x01;
break;
}
if(!bExit_flag)
SendChar(key);
}
}
CloseCom();
printf("Bye~.\n");
}
/*读串口函数,现在就是这里读串口数据有问题,不清楚c里面读串口数据的原理*/
void interrupt SerialISR()
{
/*串口中断服务代码*/
inputData = inportb(Com1_base+RXR);
onInput = 0x01;
/*服务代码结束*/
outportb(0x20,0x20); /*中断结束的代码*/
}
/*发送指令到串口*/
void SendChar(uchar key)
{
while( ((inportb(Com1_base + LSR)) & 0x40) == 0);
{
outportb(Com1_base + TXR,key);
}
}