正 文

深入浅出VC++串口编程之DOS的串口编程


www.7dspace.com  更新日期:2006-2-21 4:29:46  七度空间


  3.硬件查询

  通过读取和写入串口UART对应的硬件端口,我们可以控制串口的收发。请看下面的例子:

/* Name : Sample Comm's Program - Polled Version - termpoll.c */
/* Written By : Craig Peacock <cpeacock@senet.com.au> */
#include <dos.h>
#include <stdio.h>
#include <conio.h>

00000000000000000000#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中也可以直观地看出。

3页,页码:[1] [2] [3] 

上一篇:优化关键字 征服Google搜索
下一篇:用Xara3D轻松快速制作三维LOGO
标题:深入浅出VC++串口编程之DOS的串口编程 作者:宋宝华 来源:天极网
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐