本文共 7943 字,大约阅读时间需要 26 分钟。
深入浅出串口编程(2)
宋宝华 [email]21cnbao@21cn.com[/email] 在DOS
平台下,操作串口主要有下列方式:通过BIOS
调用、通过串口的硬件中断或通过对串口硬件进行轮询,本章将对以上三种方式进行具体的介绍并给出例子。 1.BIOS中断
在DOS
操作系统下,IBM PC
及其兼容机提供了一种灵活的串口I/O
访问方法,即通过INT 14H
调用ROM BIOS
串行通讯例行程序。当设置AH
为不同的值时,产生不同的功能: 初始化端口时(即当AH
=0
时),需要在AL
寄存器中赋一字节初始化参数,其各项意义如图1
; 当向串口写字符时(即当AH
=1
时),AL
寄存器中的字符是需要写入的字符; 当向串口写字符时(即当AH
=2
时),AL
寄存器中的字符是需要读取的字符。 union REGS inregs,outregs; write_rs232(STR,strlen(STR)); inregs.h.ah=0; //AH=0 表示初始化端口 int86(0x14, &inregs, &outregs); }while(outregs.h.ah>=0x80); write_rs232(char *string, int len) inregs.h.ah=1;// 发送 AL 寄存器的字符 int86(0x14, &inregs, &outregs); }while(outregs.h.al>=0x80); int86(0x14, &inregs, &outregs); inregs.h.ah=2; // 读取 AL 寄存器中的字符 int86(0x14, &inregs, &outregs); }while(outregs.h.al!=3||outregs.h.ah>=0x80); int _Cdecl int86(int intno, union REGS *inregs, union REGS *outregs); int86()
函数可以调用BIOS
功能,现在的程序员们已经很少接触这个函数,80%
的程序员甚至都未曾见过这个函数。其实,在茹毛饮血的DOS
时代,int86()
函数几乎是最常用和最核心的函数之一。几乎可以说,在那个时代,不会int86()
就等于不会编程。而与int86
配合使用的,就是REGS
这样一个联合体,定义为: unsigned int ax, bx, cx, dx, si, di, unsigned char al, ah, bl, bh, cl, ch, dl, dh; 原来WORDREGS
和BYTEREGS
是16
位的8086
处理器内部的寄存器啊!因此,当CPU
发展到286
、386
以后,再安装DOS
也是建立在利用CPU
实模式的基础上的! Int _Cdecl int86x(int intno, union REGS inregs, union REGS outregs, struct SREGS segregs) ; int86
和int86x
这两个函数的功能都是执行一个由参数intno
指定的8086
软中断。在执行软中断之前,两个函数都把inregs
中的内容放置到各寄存器中(int86x
还把segregs.x.es
和segregs.x.ds
的值存到相应的段寄存器中),软中断返回后,这两个函数都把当前寄存器的值存到outregs
,并把系统进位标志拷贝到outregs.s.cflag
中,把8086
标志寄存器值存到outregs.x.flag
中(int86x
还恢复DS
,并设置Segregs.es
和Segregs.ds
的值为对应段寄存器的值)。 查阅BIOS
中断调用手册,发现绝大多数调用都未用到ES
和DS
段寄存器,故在程序设计中经常只利用了int86
函数。 2.硬件中断
为了给读者一个直观的印象,我们通过在Windows
操作系统中查看COM
的资源属性获得某COM
对应的中断号,如图2
(该对话框中设备管理器中开启)。 实际上COM
的确直接对应于一个中断,而系统也按照一定的规律为各类硬件分配了一个较固定的中断号,如表1
。 通过编写COM
对应的中断服务程序,我们也可以操作串口,涉及到的相关函数有: void _Cdecl setvect (int interruptno, void interrupt (*isr) ()); 例如,COM3
对应的中断号是4
,那么对应中断向量表中的地址是0x0C
,设置0x0C
对应中断程序的函数为: void interrupt PORT1INT() buffer[bufferin] = inportb(PORT1); 上述中断服务程序检查是否有字符可接收,其后将其通过inportb(PORT1)
语句将其从UART
中读出并放入输入buffer
。持续的检查UART
,以便能在一次中断里读取所有可获得的数据。 最后的“outportb(0x20,0x20);
”语句告诉可编程中断控制器(Programmable Interrupt Controller
,PIC
)中断已经完成。 void interrupt (* _Cdecl getvect(int interruptno)) (); oldport1isr = getvect(INTVECT); void interrupt (*oldport1isr)(); 我们融合setvect()
函数、中断服务程序和getvect()
函数,给出一个由Craig Peacock
编写的完备例程: /* Name : Sample Comm's Program - 1024 Byte Buffer - buff1024.c */ /* Written By : Craig Peacock <[email]cpeacock@senet.com.au[/email]> */ #define PORT1 0x3F8 /* Port Address Goes Here */ #define INTVECT 0x0C /* Com Port's IRQ here (Must also change PIC setting) */ /* Defines Serial Ports Base Address */ void interrupt(*oldport1isr)(); void interrupt PORT1INT() /* Interrupt Service Routine (ISR) for PORT1 */ buffer[bufferin] = inportb(PORT1); outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */ oldport1isr = getvect(INTVECT); /* Save old Interrupt Vector of later setvect(INTVECT, PORT1INT); /* Set Interrupt Vector Entry */ /* PORT 1 - Communication Settings */ outportb(PORT1 + 3, 0x80); /* SET DLAB ON */ outportb(PORT1 + 0, 0x0C); /* Set Baud rate - Divisor Latch Low Byte */ /* Default 0x03 = 38,400 BPS */ outportb(PORT1 + 1, 0x00); /* Set Baud rate - Divisor Latch High Byte */ outportb(PORT1 + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */ outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */ outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */ outportb(0x21, (inportb(0x21) &0xEF)); /* Set Programmable Interrupt Controller */ outportb(PORT1 + 1, 0x01); /* Interrupt when data received */ printf("\nSample Comm's Program. Press ESC to quit \n"); if (bufferin != bufferout) /* Turn off interrupts - Port1 */ outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */ setvect(INTVECT, oldport1isr); /* Restore old interrupt vector */ 3.硬件查询
通过读取和写入串口UART
对应的硬件端口,我们可以控制串口的收发。请看下面的例子: /* Name : Sample Comm's Program - Polled Version - termpoll.c */ /* Written By : Craig Peacock <[email]cpeacock@senet.com.au[/email]> */ /* Defines Serial Ports Base Address */ outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */ /* PORT 1 - Communication Settings */ outportb(PORT1 + 3, 0x80); /* SET DLAB ON */ outportb(PORT1 + 0, 0x03); /* Set Baud rate - Divisor Latch Low Byte */ /* Default 0x03 = 38,400 BPS */ outportb(PORT1 + 1, 0x00); /* Set Baud rate - Divisor Latch High Byte */ outportb(PORT1 + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */ outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */ outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */ printf("\nSample Comm's Program. Press ESC to quit \n"); c = inportb(PORT1 + 5); /* Check to see if char has been */ ch = inportb(PORT1); /* If so, then get Char */ } /* Print Char to Screen */ ch = getch(); /* If key pressed, get Char */ } /* Send Char to Serial Port */ while (ch != 27); /* Quit when ESC (ASC 27) is pressed */ c = inportb(PORT1 + 5); /* Check to see if char has been */ 检查PORT1 + 5
端口地址,通过c&1
可以判断是否有数据被UART
接收到。关于UART
对应的端口范围,从图2
中也可以直观地看出。 本文转自 21cnbao 51CTO博客,原文链接:http://blog.51cto.com/21cnbao/120291,如需转载请自行联系原作者