您的位置:首页 > 其它

几个常用IO端口读写函数

2008-07-29 17:17 288 查看
 DOS中,几个常用端口读写函数:

 

int inport(int prot);     //从指定的输入端口读入一个字,并返回这个字

int inportb(int port);  //从指定的输入端口读入一个字节,并返回这个字节

void outport(int port,int word); //将字word写入指定的输出端口port

void outportb(int port,char byte); //将字节byte写入指定的输出端口port

这些功能可以通过宏定义实现:

对于IO空间映射在存储空间的结构,输入输出处理

#define inp(port) (*((volatile byte *)(port)))

#define inpw(port) (*((volatile word *)(port)))

#define inpdw(port) (*((volatile dword *)(port)))

#define outp(port,val) (*((volatile byte *)(port))=((byte)(val)))

#define outpw(port, val) (*((volatile word *)(port))=((word)(val)))

#define outpdw(port, val) (*((volatile dword *)(port))=((dword)(val)))

对于IO空间映射在存储空间的结构,输入输出处理

#define inp(port)  (*((volatile byte *) (port)))

dos下读端口。在dos下就是读内存,读内存就是取某一个地址的内容,比如读n端口就是读取内存n处的内容,

(*((volatile  byte  *)  (port)))

其实就是

*(unsigned char *)port  

   

在BC3.1自带的dos.h中,定义如下

#ifndef _PORT_DEFS

unsigned        _Cdecl inport ( unsigned __portid );

unsigned char   _Cdecl inportb( unsigned __portid );

unsigned        _Cdecl inpw   ( unsigned __portid );

int             _Cdecl inp    ( unsigned __portid );

#endif

在VC的C运行库中,说明如下:

Outputs a byte (_outp), a word (_outpw), or a double word (_outpd) at a port

int _outp(

   unsigned short port,

   int databyte

);

unsigned short _outpw(

   unsigned short port,

   unsigned short dataword

);

unsigned long _outpd(

   unsigned short port,

   unsigned long dataword

);

VC中,outputb和inportb对应的函数为_inp和_outp。使用时需要<conio.h>头文件。建议使用_inp和
_outp,而不是使用inp和outp。因为_inp和_outp在Debug和Release都可以使用,而inp和outp只能用于Release
设置。如果在 Debug设置时使用inp和outp,编译时不会错,但会产生链接错误如下:

     error LINK2001:Unresolved external symbol _inp

     error LINK2001:Unresolved external symbol _outp

     fatal error LNK1120:2 Unresolved externals.

以上方法只适用于Win9x,在Windows NT/2000下任何硬件I/O操作均需要通过设备启动程序。

可以借助DDK(http://www.microsoft.com/ddk)编写设备驱动程序。当然你也可以购买现成的软件,如http:
//china.zealsoft.com/cn/ntport/。Windows的开发同DOS开发有很大的不同。如果你设计的是控制台程序,那么整个
程序运行起来更DOS程序差不多,这时你可以象在
DOS中一样使用标准输出函数。如果你设计的是图形界面的程序,那么所有C和C++的标准输入/输出函数都不再能使用了。可以使用TextOut或
DrawText输出文字。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  io byte dos windows ddk 存储