文档详情

Windows平台下串行通信的几种实现方法.docx

发布:2021-07-20约8.18千字共8页下载文档
文本预览下载声明
Windows平台下串行通信的几种实现方法 Windows平台下串行通信的几种实现方法 Windows 平台下串行通信的几种实现方法 冯雪飞, 陈文亮 (南京航空航天大学机电学院, 江苏南京210016) 摘 要:本文主要介绍了在Window s (Window s 9x /NT , Window s 2000) 平台上利用串口COM x 遵循RS -232C 标准进行串行通信的实现机制, 论述了采用Visual C ++为工具设计串行通信程序的几种常用方法。最后给出一个采用多线程方法实现的实例。关键词:Window s 异步串行通讯; RS -232C , 数控机床通信 中图分类号:TP316, TP311. 52 文献标识码:A  文章编号:1671-5276(2002) 01-0033-03 The Several Methods for Serial Ports Communication under Windows FENG Xue -fei , CHEN Wen -liang (Nanjing University of Aeronautios , JS Nanjing 210016, China ) A bstract :This article mainly describes the implemented mechanism of serial po rts communication under Win -dow 9x /NT and Window s 2000platform . Microsoft s development platform Visual C ++6. 0is the main tool for im plementing the several common methods w hich are discussed here fo r Serial communication . At last , there is an instance adopted multithread method . Key words :w indow s asynchronous serial communication ; RS -232C ; numerical control machine communica -tio n 串行通信是终端与计算机或计算机之间进行数据通信的一种方式, 现在它的应用仍然很广泛, 利用计算机的串行通信端口COMx 与其他设备如打印机、计算机、单片机、数控机床等提供的遵循RS -232C 标准的串行通信端口进行数据通讯。特别是工业上与数控机床进行通讯, 应用价值更大。 两个通信设备(以1、2表示) 之间通信最简单的连接方法:发送端1—接收端2; 发送端2—接收端1, 地1—地2, 还可以实现更加复杂的连接方式。串行通信方式有同步和异步之分, 根据收发操作的能力又可分为:全双工、半双工和单工通信。在具体通信中, 常常采用的流量控制(Flow Con -trol ) 又称握手(handshaking ) , 可分为硬件握手/硬件流控和软件握手/软件流控。 作系统提供的设备驱动程序来进行数据传递。 在Window s 平台下, 对各种通信资源(串行口、并行口、传真机和MODEM 等) 都作为文件来 处理。利用大量的API 函数如CreateFile 、Close -H andle 、ReadFile (ReadFileEx ) 和WriteFile (Write -FileEx ) 等对通讯资源COMx 端口的创建和关闭、读取和发送数据等操作来实现。此外, 可以参考MSDN 中涉及的通讯结构和其他API 函数。具体实现按下面步骤: a ) 创建通信设备。在应用程序中, 要在通信类中添加创建通信设备的函数, 创建通信资源的函数是: HANDLE CreateFile (LPCTSTR DWO RD Mode , lpFileName , dwShare -lpSecurity -pointer dwDesiredAccess , DWORD LPSECURIT Y ATTRIBUTES Attributes , DWORD dwCreationDisposition , DWORD dwFlagsAnd Attribute s , HANDLE hTem plateFile ) ; 2 利用Windows A PI 实现串行通讯 DOS 平台下串行通信程序特点是直接控制硬件, 而W
显示全部
相似文档