VB编程如何控制I/O口
【打印文章】
VB没有提供直接读写I/O口的方法。所以只能借助其他语言来编写DLL,然后在VB中调用。
如果你在Windows下使用过C/C++,那么编写这样的DLL可能没有什么困难。在C语言里都包括inp和outp函数。可以把下面这段C语言代码(32位)编译生成DLL,然后在VB中调用。
代码如下:
#include
#include
/*作用:从指定端口读入一个字节
参数:portid端口号
返回值:读入的字节*/
int _stdcall Inport(short portid)
{
return inp(portid);}
/*作用:向指定端口写入一个字节
参数:portid端口号*/
void _stdcall output(short portid,short byte)
{
outp(portid,byte);
}
/*作用:从指定端口读入一个字节
参数:portid端口号
返回值:读入的字节*/
int _stdcall Inportw(short portid)
{
return inpw(portid);}
/*作用:向指定端口写入一个字节
参数:portid端口号*/
void _stdcall Outportw(short portid,short word)
{
outpw(portid,(unsigned short) word);
}
注意:这种方法只能用于Windows 95,不能用于Windows NT
如果你在Windows下使用过C/C++,那么编写这样的DLL可能没有什么困难。在C语言里都包括inp和outp函数。可以把下面这段C语言代码(32位)编译生成DLL,然后在VB中调用。
代码如下:
#include
#include
/*作用:从指定端口读入一个字节
参数:portid端口号
返回值:读入的字节*/
int _stdcall Inport(short portid)
{
return inp(portid);}
/*作用:向指定端口写入一个字节
参数:portid端口号*/
void _stdcall output(short portid,short byte)
{
outp(portid,byte);
}
/*作用:从指定端口读入一个字节
参数:portid端口号
返回值:读入的字节*/
int _stdcall Inportw(short portid)
{
return inpw(portid);}
/*作用:向指定端口写入一个字节
参数:portid端口号*/
void _stdcall Outportw(short portid,short word)
{
outpw(portid,(unsigned short) word);
}
注意:这种方法只能用于Windows 95,不能用于Windows NT
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】