深入浅出VC++串口编程之DOS的串口编程
发布时间:2006-02-20 10:42:19 来源:天极开发 网友评论 0 条 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中也可以直观地看出。
- 推荐阅讯
- 解读VC++编程中的文件操作API和CFile类
- VC++编程实现广告窗口自动关闭
- 用VC实现对超长数据库字段的操作
- Visual C++ 2005图像编程之属性设置栏
- VC++.NET中事件编程剖析之什么是事件
- 利用VC打造自己的资源浏览器
- 在VC中添加响应自定义的消息的代码步骤
- 在VC中调用DirectShow全屏播放视频
- Visual C# 2005快速入门之运用作用域
- VC和Delphi程序只运行一个实例的方法
- 阅读排行
- 1.VC++编程实现广告窗口自动关闭
- 2.深入浅出VC++串口编程之基于控件
- 3.解读VC++编程中的文件操作API和CFile类
- 4.利用Visual C#实现ICMP网络协议
- 5.深入浅出VC++串口编程之第三方类
- 6.掀起你的盖头来——谈VC++对象模型
- 7.Visual C#中用WMI控制远程计算机
- 8.深入浅出VC++串口编程之基于Win32 API
- 9.Visual C++2005中开发自定义绘图控件
- 10.深入浅出VC++串口编程之基本概念
- 专题教程
- Windows Server-Windows Server文档-Windows Server新闻-Windows Ser PostgreSQL-PostgreSQL文档-PostgreSQL新闻-PostgreSQL专家
- WebLogic-WebLogic文档-WebLogic新闻-WebLogic专家 FreeBSD-FreeBSD文档-FreeBSD新闻-FreeBSD专家
- Linux-内核 GUI KDE Gnome DNS FTP 安全 安装-Linux专区 Windows-AD IIS ServerCore 虚拟化 安全 HPC-Windows专区
- 大话G游 专题:手机病毒揭密
- ARP攻击防范与解决方案 路由故障处理手册
