第 27 卷第 5 期 唐山师范学院学报 2005 年 9 月 Vol 27 No 5 Journal of Tangshan Teachers College Sep 2005 收稿日期 2005 04 02 作者简介 李金钟 1964 男 河北唐山人 唐山师范学院物理系讲师 93 运用 C 语言对串行口进行操作 李金钟 1 赵 欣1 李敬民2 1 唐山师范学院 物理系 河北 唐山 063000 2 唐山一中 河北 唐山 063000 摘 要 通过运用 C 语言对串行口进行操作的应用与研究 现给出 C 语言对串行口的一系列操作方法 对于 用 C 语言或其他语言对串行口进行操作的编程者 有重要的参考价值 关键词 串行口 C 语言 指令 中图分类号 TP311 文献标识码 A 文章编号 1009 9115 2005 05 0093 03 C 语言是当前最流行的程序设计语言 它像其它高级语 言一样 面向用户 面向解题的过程 编程者不必熟悉具体 的计算机内部结构和指令 C 语言又像汇编语言一样 可以 对机器硬件进行操作 如进行端口 I O 操作 位操作 地址 操作 并可内嵌汇编指令 将汇编指令当作它的语句一样 我们知道 汇编语言将涉及计算机硬件 所以 C 语言又像低 级语言一样 可以对计算机硬件进行控制 因此人们把它称 为介于高级语言与低级语言之间的一种中级语言 正是因为 C 语言具有这样的特性 所以很适合编写要对硬件进行操作 的软件程序 1 1 对串行口的初始化 通讯数据是从 RS232 串行接口读入的 从串口读数 初始化是个关键 下面把串行口通讯初始化程序语句做一详 细分析 1 1 数据接收采用查询方式 define BASE1 0 x2f8 设置默认的端口值 0 x2f8 为串口 2 地址 void initiateCOM void 串行口初始化子程序 outportb BASE1 3 0 x80 寻址波特率寄存器 outportb BASE1 0 x0c 波 特 率 设为 9600 outportb BASE1 1 0 outportb BASE1 3 0 x1B 8 个数据位 1 个停 止位 偶校验 outportb BASE1 1 0 非中断模式 outportb BASE1 4 0 x03 哑传所需的 DTR 和 RTS 有效信号 1 2 数据接收采用中断方式 define BASE1 0 x3f8 设置默认的端口值 0 x3f8 为串口 1 地址 void interrupt oldisr void interrupt far newisr void initiateCOM void 串行口初始化子程 序 disable 关 中 断 指令 outportb BASE1 4 0 outportb BASE1 1 0 outportb BASE1 3 0 x80 寻址波特率寄存器 outportb BASE1 0 x04 波特率设为 28 8K outportb BASE1 1 0 x00 inportb BASE1 2 inportb BASE1 oldisr getvect intp1 获得原来中断矢量 并保存 setvect intp1 newisr 设置新矢量 outportb BASE1 3 0 x3B 8 个数据位 1 个停 止位 偶校验 奇偶校验位恒为 0 outportb BASE1 1 0 x01 中断模式 outportb BASE1 4 0 x08 哑传所需的 DTR 和 RTS 有效信号 outportb 0 x21 inportb 0 x21 2 No 1 Middle School of Tangshan Hebei Tangshan 063000 China Abstract By studying the method of operating COM port using language C this paper lists a series of such methods which has important reference values to the programmers Key words COM port language C command 责任编辑 校对 田敬军 上接第 82 页 Relying on the Athletics Education Characteristic Put the New Concept in Practice in the P E Course of Higher Academy YAO Chang qing MA Chun lan TangShan Vocational Technical College Hebei Tangshan 063004 China Abstract Physical education has its own unique practical and visible features Teachers should pay more attention to the individual diversity among students exerting the pluralism of physical education cultivating the interest and habit of student and arousing their enthusiasm and initiative Key words high academy physical education class characteristic of physical education new conception 责任编辑 校对 孙海祥
展开阅读全文
本文介绍了使用C语言对串行口进行操作的方法,包括串行口的初始化及数据接收的查询与中断方式。这些方法对于C语言或其他语言的编程者具有重要的参考价值。
1535

被折叠的 条评论
为什么被折叠?



