怎么用C语言中断方式实现串口的读写?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用C语言中断方式实现串口的读写?相关的知识,希望对你有一定的参考价值。

我想对计算机串口进行中断操作,不知怎样设置寄存器,大虾们可否提供思路?

参考技术A #include <stdio.h>
#include <dos.h>
#include <time.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;
volatile uchar inputdata[256]; /*设设置接收缓冲区大小*/
volatile unsigned int count; /*中断接收的数据数目*/
int datacount;/*当前已发送数据条数*/

void interrupt (*OldVect)(); /*函数是用来获取中断处理程序的入口地址的*/
void interrupt SerialISR();

void InitCom()

uchar inttemp;
/*设置波特率什么的*/
outportb(Com1_base+LCR,0x80); /*使LCR的高位为1,以便读取其它寄存器*/
outportb(Com1_base+LSB,0x0c); /*除数锁存器(低8位)DLL*/
outportb(Com1_base+MSB,0x00); /*除数锁存器(高8位)DLH 产生2400波特率*/
outportb(Com1_base+LCR,0x03); /*8位数据,1位停止位,无校验*/
outportb(Com1_base+IER,0x01); /*接收采用中断方式*/

/*设置中断向量*/
OldVect = getvect(0x0c); /*函数是用来获取中断处理程序的入口地址的*/
disable();
inttemp = inportb(0x21)&0xef;
outportb(0x21,inttemp);
setvect(0x0c,SerialISR); /*设置SerialISR的中断入口地址为0X0C*/
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;
inputdata[count]=inputData;
count++;
/*服务代码结束*/
outportb(0x20,0x20); /*中断结束的代码*/


void SendChar(uchar key)

while( ((inportb(Com1_base + LSR)) & 0x40) == 0);
outportb(Com1_base + TXR,key);

/*...........延时函数......................*/
void delay(unsigned int n)

unsigned int i,k;
for(k=0;k<n;k++)

for(i=1;i<1142;i++)
;



/*........CDMA命令发送函数.................*/
void CDMA_CommandSend(char *p,unsigned int n)


int i,k,m,l;
int flag=0;
int selse_flag=0; int selse_flag_1=0;
int selse_flag1=0;int selse_flag1_1=0;
time_t start,end;
double dif=0;
char a;

uchar bExit_flag = 0x00;
count=0;
while(!bExit_flag)

count=0;
for(i=0;i<n;i++)
SendChar(*p);delay(9000);p++;

/*..........等待2S..................*/
time (&start);
dif=0;
while(dif<2)

delay(9000);
time (&end);
dif = difftime (end,start);

if(count>14)

time (&start);
dif=0;
while(dif<3)

delay(9000);
time (&end);
dif = difftime (end,start);



/*..........判断发送命令是否成功..................*/
for(m=0;m<count;m++)

if(count>10)

for(l=0;l<count;l++)

if(inputdata[l-1]=='N'&&inputdata[l-2]=='E'&&inputdata[l-3]=='P'&&inputdata[l-4]=='O'&&inputdata[l-5]=='P'&&inputdata[l-6]=='P'&&inputdata[l-7]=='P'&&inputdata[l-8]=='V') selse_flag_1=1;
if(inputdata[l-1]=='N'&&inputdata[l-2]=='E'&&inputdata[l-3]=='P'&&inputdata[l-4]=='O'&&inputdata[l-5]=='P'&&inputdata[l-6]=='C'&&inputdata[l-7]=='T'&&inputdata[l-8]=='V') selse_flag1_1=1;


if(selse_flag_1==1)if(inputdata[m-1]=='3'&&inputdata[m-2]=='3'&&inputdata[m-3]==':'&&inputdata[m-4]=='T') selse_flag=1;
if(selse_flag1_1==1)if(inputdata[m-1]=='0'&&inputdata[m-2]==','&&inputdata[m-3]=='1'&&inputdata[m-4]==':'&&inputdata[m-5]=='N') selse_flag1=1;

if(selse_flag_1==1||selse_flag1_1==1)

if(selse_flag==1) if(inputdata[m-1]=='0'&&inputdata[m-2]==':'&&inputdata[m-3]=='N'&&inputdata[m-4]=='E') flag=1;
if(selse_flag1==1) if(inputdata[m-1]=='0'&&inputdata[m-2]==','&&inputdata[m-3]=='1'&&inputdata[m-4]==':'&&inputdata[m-5]=='S') flag=1;

else if(inputdata[m-1]=='O'&&inputdata[m]=='K') flag=1;

else if(inputdata[m-1]=='O'&&inputdata[m]=='K') flag=1;

if(flag==1)bExit_flag = 0x01;
else p=p-n;

for(i=0;i<count;i++) printf("%c",inputdata[i]);
/*........按空格停止测试.............................*/
if(kbhit())a=getch(); if(a==' ')CloseCom(); exit(1);




/*........数据测试发送任务.....................*/
void Data_Send(char *pcomd,int n,char *pdata,int m)

int i,k,l;time_t start,end;
double dif=0;
count=0;
for(i=0;i<n;i++)

SendChar(*pcomd);delay(9000);pcomd++;

time (&start);
dif=0;
while(dif<0.2)

delay(9000);
time (&end);
dif = difftime (end,start);


for(l=0;l<m;l++)

SendChar(*pdata);pdata++;


printf("The data num:%d",datacount);
datacount++;


void main()
int i;char a;
/*........模块测试命令..................................*/
uchar CDMA_COMMAND_1[]="at\r";
uchar CDMA_COMMAND_2[]="atz\r";
uchar CDMA_COMMAND_3[]="ate1v1\r";
uchar CDMA_COMMAND_4[]="AT+CRM=1;+CPS=33;+CMUX=1;+CTA=0\r";
uchar CDMA_COMMAND_5[]="AT+VPPPOPEN\r";
uchar CDMA_COMMAND_6[]="AT+VTCPOPEN=1,\"60.63.42.129\",1234\r";
uchar CDMA_COMMAND_7[]="AT+VTCPSEND=1,20\r";
uchar CDMA_DATA[]="ABCDEFGHIJKLMNOPQRST";
uchar CDMA_COMMAND_9[]="AT+VPPPCLOSE\r";
uchar CDMA_COMMAND_8[]="AT+VTCPCLOSE=1\r";
uchar bExit_flag1 = 0x00;
time_t start,end;
double dif=0;
InitCom();/*初始化端口1*/
inputData = inportb(Com1_base+RXR);
while(!bExit_flag1)

/*.................................................................*/
if(kbhit()) /*按任意键开始测试CDMA*/

/*.............................................................. ...*/

/*.........发送TCP断开命令.....................................*/
CDMA_CommandSend(CDMA_COMMAND_8,strlen(CDMA_COMMAND_8));
/*.........发送PPP断开命令......................................*/
CDMA_CommandSend(CDMA_COMMAND_9,strlen(CDMA_COMMAND_9));

/*.........发送AT命令..........................................*/
CDMA_CommandSend(CDMA_COMMAND_1,strlen(CDMA_COMMAND_1));
/*.........发送ATZ命令.........................................*/
CDMA_CommandSend(CDMA_COMMAND_2,strlen(CDMA_COMMAND_2));
/*.........发送ATE1V1命令......................................*/
CDMA_CommandSend(CDMA_COMMAND_3,strlen(CDMA_COMMAND_3));
/*.........发送AT初始化命令....................................*/
CDMA_CommandSend(CDMA_COMMAND_4,strlen(CDMA_COMMAND_4));
/*.........发送PPP拔号命令.....................................*/
CDMA_CommandSend(CDMA_COMMAND_5,strlen(CDMA_COMMAND_5));
/*.........发送网络连接命令....................................*/
CDMA_CommandSend(CDMA_COMMAND_6,strlen(CDMA_COMMAND_6));
/*.........发送数据............................................*/
datacount=0;
while(1)

Data_Send(CDMA_COMMAND_7,strlen(CDMA_COMMAND_7),CDMA_DATA,strlen(CDMA_DATA));

time (&start);
dif=0;
while(dif<0.2)

delay(9000);
time (&end);
dif = difftime (end,start);


if(kbhit())a=getch(); if(a==' ')bExit_flag1 = 0x01; break;

/*.........发送TCP断开命令.....................................*/
for(i=0;i<3;i++)
CDMA_CommandSend(CDMA_COMMAND_8,strlen(CDMA_COMMAND_8));
/*.........发送PPP断开命令......................................*/
for(i=0;i<3;i++)
CDMA_CommandSend(CDMA_COMMAND_9,strlen(CDMA_COMMAND_9));

bExit_flag1 = 0x01;


/*.................................................................*/


CloseCom(); /*关闭端口1*/
printf("Bye~.\n");
本回答被提问者采纳

以上是关于怎么用C语言中断方式实现串口的读写?的主要内容,如果未能解决你的问题,请参考以下文章

用C语言实现文件读写操作

怎样获取串口地址

怎么用C语言写一个简单的XML文件

VS或者VC6.0编写的C语言程序,怎样能够实现串口数据的收发?

windows下怎么用C语言读取串口里的数据?

c语言win7 USB插上后怎么查找所在的串口?