深入浅出串口编程(2)――基于DOS的串口编程

简介:


深入浅出串口编程(2

――基于 DOS 的串口编程
宋宝华  [email]21cnbao@21cn.com[/email]
DOS平台下,操作串口主要有下列方式:通过BIOS调用、通过串口的硬件中断或通过对串口硬件进行轮询,本章将对以上三种方式进行具体的介绍并给出例子。

1.BIOS中断

DOS操作系统下,IBM PC及其兼容机提供了一种灵活的串口I/O访问方法,即通过INT 14H调用ROM BIOS串行通讯例行程序。当设置AH为不同的值时,产生不同的功能:
AH  初始化端口
AH  向串口写字符
AH  从串口读字符
AH  取通讯口状态
初始化端口时(即当AH0时),需要在AL寄存器中赋一字节初始化参数,其各项意义如图1
调用INT 14HAL寄存器设置
       当向串口写字符时(即当AH1时),AL寄存器中的字符是需要写入的字符;
       当向串口写字符时(即当AH2时),AL寄存器中的字符是需要读取的字符。
看看下面的例程:
#include <stdio.h>
#include <dos.h>
#include <bios.h>
 
#define STR "author:sbh"
union REGS inregs,outregs;
 
main()
{    
       //设置串口参数
       init_rs232();
       //写串口的例子
       write_rs232(STR,strlen(STR));
       //读串口的例子
       read_rs232();
 
       return(0);
}
 
init_rs232()
{
       do{
              inregs.h.ah=0;  //AH=0表示初始化端口
              inregs.h.al=0xe7;
              inregs.x.dx=0;  //COM1
              int86(0x14, &inregs, &outregs);
       }while(outregs.h.ah>=0x80);
 
       return(0);
}
 
write_rs232(char *string, int len)
{
       int i;
       do{
              inregs.h.ah=1;//发送AL寄存器的字符
              inregs.h.al= *string;
              inregs.x.dx=0;
              int86(0x14, &inregs, &outregs);
       }while(outregs.h.al>=0x80);
 
       for(i=1;i<len;i++)
       {
              inregs.h.ah=1;
              inregs.h.al=*(string+i);
              inregs.x.dx=0;
              int86(0x14, &inregs, &outregs);
       }
}
 
read_rs232()
{
       do{
              inregs.h.ah=2; //读取AL寄存器中的字符
              inregs.x.dx=0;
              int86(0x14, &inregs, &outregs);
       }while(outregs.h.al!=3||outregs.h.ah>=0x80);
 
       return(0);
}
其中使用的int86函数的原型为:
int   _Cdecl int86(int intno, union REGS *inregs, union REGS *outregs);
int86()函数可以调用BIOS功能,现在的程序员们已经很少接触这个函数,80%的程序员甚至都未曾见过这个函数。其实,在茹毛饮血的DOS时代,int86()函数几乎是最常用和最核心的函数之一。几乎可以说,在那个时代,不会int86()就等于不会编程。而与int86配合使用的,就是REGS这样一个联合体,定义为:
union      REGS     {
       struct      WORDREGS x;
       struct      BYTEREGS h;
};
其中的WORDREGS定义为:
struct WORDREGS {
       unsigned int    ax, bx, cx, dx, si, di,
    cflag /*进位标志*/,
       flags /*标志寄存器*/;
};
BYTEREGS则定义为:
struct BYTEREGS {
       unsigned char al, ah, bl, bh, cl, ch, dl, dh;
};
原来WORDREGSBYTEREGS16位的8086处理器内部的寄存器啊!因此,当CPU发展到286386以后,再安装DOS也是建立在利用CPU实模式的基础上的!
另外一个函数与int86()的功能是类似的:
Int _Cdecl int86x(int intno, union REGS inregs, union REGS outregs, struct SREGS segregs)
其中的SREGS为段寄存器结构体,定义为:
struct      SREGS  
{
       unsigned int    es;
       unsigned int    cs;
       unsigned int    ss;
       unsigned int    ds;
};
int86int86x这两个函数的功能都是执行一个由参数intno指定的8086软中断。在执行软中断之前,两个函数都把inregs中的内容放置到各寄存器中(int86x还把segregs.x.essegregs.x.ds的值存到相应的段寄存器中),软中断返回后,这两个函数都把当前寄存器的值存到outregs,并把系统进位标志拷贝到outregs.s.cflag中,把8086标志寄存器值存到outregs.x.flag中(int86x还恢复DS,并设置Segregs.esSegregs.ds的值为对应段寄存器的值)。
查阅BIOS中断调用手册,发现绝大多数调用都未用到ESDS段寄存器,故在程序设计中经常只利用了int86函数。

2.硬件中断

为了给读者一个直观的印象,我们通过在Windows操作系统中查看COM的资源属性获得某COM对应的中断号,如图2(该对话框中设备管理器中开启)。
 COM中断号
实际上COM的确直接对应于一个中断,而系统也按照一定的规律为各类硬件分配了一个较固定的中断号,如表1
中断向量表
INT (Hex)
IRQ
Common Uses
08
0
System Timer
09
1
Keyboard
0A
2
Redirected
0B
3
Serial Comms. COM2/COM4
0C
4
Serial Comms. COM1/COM3
0D
5
Reserved/Sound Card
0E
6
Floppy Disk Controller
0F
7
Parallel Comms.
70
8
Real Time Clock
71
9
Reserved
72
10
Reserved
73
11
Reserved
74
12
PS/2 Mouse
75
13
Maths Co-Processor
76
14
Hard Disk Drive
77
15
Reserved
通过编写COM对应的中断服务程序,我们也可以操作串口,涉及到的相关函数有:
1)设置中断向量表
/*dos.h*/
void _Cdecl setvect      (int interruptno, void interrupt (*isr) ());
例如,COM3对应的中断号是4,那么对应中断向量表中的地址是0x0C,设置0x0C对应中断程序的函数为:
       setvect(0x0C, PORT1INT);
其中的中断服务程序PORT1INT为:
void interrupt PORT1INT()
{
  int c;
  do
  {
    c = inportb(PORT1 + 5);
    if (c &1)
    {
      buffer[bufferin] = inportb(PORT1);
      bufferin++;
      if (bufferin == 1024)
        bufferin = 0;
    }
  }
  while (c &1);
  outportb(0x20, 0x20);
}
上述中断服务程序检查是否有字符可接收,其后将其通过inportb(PORT1)语句将其从UART中读出并放入输入buffer。持续的检查UART,以便能在一次中断里读取所有可获得的数据。
最后的“outportb(0x20,0x20);”语句告诉可编程中断控制器(Programmable Interrupt ControllerPIC)中断已经完成。
2)读取中断向量表
/*dos.h*/
void interrupt        (* _Cdecl getvect(int interruptno)) ();
例如:
oldport1isr = getvect(INTVECT);
其中的oldport1isr定义为:
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]>                */
#include <dos.h>
#include <stdio.h>
#include <conio.h>
 
#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 */
/* COM1 0x3F8                  */
/* COM2 0x2F8                          */
/* COM3 0x3E8                          */
/* COM4 0x2E8                          */
 
int bufferin = 0;
int bufferout = 0;
char ch;
char buffer[1025];
 
void interrupt(*oldport1isr)();
 
void interrupt PORT1INT() /* Interrupt Service Routine (ISR) for PORT1 */
{
  int c;
  do
  {
    c = inportb(PORT1 + 5);
    if (c &1)
    {
      buffer[bufferin] = inportb(PORT1);
      bufferin++;
      if (bufferin == 1024)
      {
        bufferin = 0;
      }
    }
  }
  while (c &1);
  outportb(0x20, 0x20);
}
 
void main(void)
{
  int c;
  outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */
 
  oldport1isr = getvect(INTVECT); /* Save old Interrupt Vector of later
  recovery */
 
  setvect(INTVECT, PORT1INT); /* Set Interrupt Vector Entry */
  /* COM1 - 0x0C */
  /* COM2 - 0x0B */
  /* COM3 - 0x0C */
  /* COM4 - 0x0B */
 
  /*         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 */
  /*         0x01 = 115,200 BPS */
  /*         0x02 =  57,600 BPS */
  /*         0x06 =  19,200 BPS */
  /*         0x0C =   9,600 BPS */
  /*         0x18 =   4,800 BPS */
  /*         0x30 =   2,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 */
  /* COM1 (IRQ4) - 0xEF  */
  /* COM2 (IRQ3) - 0xF7  */
  /* COM3 (IRQ4) - 0xEF  */
  /* COM4 (IRQ3) - 0xF7  */
 
  outportb(PORT1 + 1, 0x01); /* Interrupt when data received */
 
  printf("\nSample Comm's Program. Press ESC to quit \n");
 
  do
  {
    if (bufferin != bufferout)
    {
      ch = buffer[bufferout];
      bufferout++;
      if (bufferout == 1024)
      {
        bufferout = 0;
      }
      printf("%c", ch);
    }
 
    if (kbhit())
    {
      c = getch();
      outportb(PORT1, c);
    }
  }
  while (c != 27);
 
  outportb(PORT1 + 1, 0);
   /* Turn off interrupts - Port1 */
  outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */
  /* COM1 (IRQ4) - 0x10  */
  /* COM2 (IRQ3) - 0x08  */
  /* COM3 (IRQ4) - 0x10  */
  /* COM4 (IRQ3) - 0x08  */
  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]>                   */
#include <dos.h>
#include <stdio.h>
#include <conio.h>
 
#define PORT1 0x3F8
 
/* Defines Serial Ports Base Address */
/* COM1 0x3F8                        */
/* COM2 0x2F8                          */
/* COM3 0x3E8                          */
/* COM4 0x2E8                          */
 
void main(void)
{
  int c;
  int ch;
  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 */
  /*         0x01 = 115,200 BPS */
  /*         0x02 =  57,600 BPS */
  /*         0x06 =  19,200 BPS */
  /*         0x0C =   9,600 BPS */
  /*         0x18 =   4,800 BPS */
  /*         0x30 =   2,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");
 
  do
  {
    c = inportb(PORT1 + 5); /* Check to see if char has been */
    /* received.                     */
    if (c &1)
    {
      ch = inportb(PORT1); /* If so, then get Char          */
      printf("%c", ch);
    } /* Print Char to Screen          */
 
    if (kbhit())
    {
      ch = getch(); /* If key pressed, get Char */
      outportb(PORT1, ch);
    } /* 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 */
    /* received.                     */
    if (c &1)
检查PORT1 + 5端口地址,通过c&1可以判断是否有数据被UART接收到。关于UART对应的端口范围,从图2中也可以直观地看出。



 本文转自 21cnbao 51CTO博客,原文链接:http://blog.51cto.com/21cnbao/120291,如需转载请自行联系原作者



相关文章
|
30天前
|
存储 网络协议 C语言
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
【C/C++ 串口编程 】深入探讨C/C++与Qt串口编程中的粘包现象及其解决策略
79 0
|
22天前
|
传感器 Linux API
嵌入式Linux串口编程简介
嵌入式Linux串口编程简介
19 1
|
2月前
|
监控 Linux 定位技术
Linux应用开发基础知识——串口应用编程(十一)
Linux应用开发基础知识——串口应用编程(十一)
46 0
Linux应用开发基础知识——串口应用编程(十一)
|
9月前
|
监控 网络协议
2013-12-17 与外网串口服务器的delphi程序设计 随笔说说
2013-12-17 与外网串口服务器的delphi程序设计 随笔说说
|
负载均衡 安全 网络协议
网络编程懒人入门(十五):外行也能读懂的网络硬件设备功能原理速成
本文是《网络编程懒人入门》系列文章的第15篇,本篇将继续以通俗易懂的文字,帮你无脑理解各种基础网络硬件设备的功能原理。
94 0
|
负载均衡 网络协议 安全
不为人知的网络编程(十五):深入操作系统,一文搞懂Socket到底是什么
这篇文章我打算从初学者的角度,用通俗易懂的文字,跟大家分享下我所理解的socket是什么,并由浅入深从操作系统内核实现来透视socket的原理。
317 0
不为人知的网络编程(十五):深入操作系统,一文搞懂Socket到底是什么
|
存储 Java 数据安全/隐私保护
嵌入式开发学习之--串口通讯(下)
嵌入式开发学习之--串口通讯(下)
嵌入式开发学习之--串口通讯(下)
|
芯片 开发者
嵌入式开发学习之--串口通讯(上)
嵌入式开发学习之--串口通讯(上)
嵌入式开发学习之--串口通讯(上)
|
网络协议 算法 Linux
Linux网络原理及编程(8)——第十八节 数据链路层
在网卡出厂时就确定了, 不能修改. mac地址通常是唯一的(虚拟机中的mac地址不是真实的mac地址, 可能会冲突; 也有些网卡支持用户配置mac地址)
195 0
Linux网络原理及编程(8)——第十八节 数据链路层