在Windows 10下使用C ++访问端口的最佳方法? [英] Best way to access ports using C++ under windows 10 ?

查看:61
本文介绍了在Windows 10下使用C ++访问端口的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请分享有关如何使用C ++(使用g ++编译)在Windows 10中功能性访问端口的信息。



请包括测试所需的所有#include文件和代码I / O并行端口引脚编程。



谢谢



我尝试过:



Please share info on how to functionally access ports in Windows 10 using C++ (compiled with g++).

Please include all #include files and code necessary to test I/O Parallel Port Pin Programming.

Thank you

What I have tried:

/*** Following Code Compiles but DOES NOT modify ports ***/

#include <stdio.h>
#include <conio.h>
#include <windows.h>


/* Definitions in the build of inpout32.dll are:            */
/*   short _stdcall Inp32(short PortAddress);               */
/*   void _stdcall Out32(short PortAddress, short data);    */
/* prototype (function typedef) for DLL function Inp32: */

     typedef short _stdcall (*inpfuncPtr)(short portaddr);
     typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);

int main(void)
{
     HINSTANCE hLib;
     inpfuncPtr inp32;
     oupfuncPtr oup32;

     short x;
     int i;

     /* Load the library */
     hLib = LoadLibrary("inpout32.dll");

     if (hLib == NULL) {
          printf("LoadLibrary Failed.\n");
          return -1;
     }

     /* get the address of the function */

     inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");

     if (inp32 == NULL) {
          printf("GetProcAddress for Inp32 Failed.\n");
          return -1;
     }

     oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");

     if (oup32 == NULL) {
          printf("GetProcAddress for Oup32 Failed.\n");
          return -1;
     }

/***************************************************************/
/* now test the functions */

     /* Try to read 0x378..0x37F, LPT1:  */

     for (i=0x378; (i<0x380); i++) {

          x = (inp32)(i);

          printf("port read (%04X)= %04X\n",i,x);
     }

     /*****  Write the data register */

     i=0x378;
     x=0x77;

     (oup32)(i,x);

     printf("port write to 0x%X, datum=0x%2X\n" ,i ,x);

     /***** And read back to verify  */
     x = (inp32)(i);
     printf("port read (%04X)= %04X\n",i,x);

     /*****  One more time, different value */

     i=0x378;
     x=0xAA;

     (oup32)(i,x);

     printf("port write to 0x%X, datum=0x%2X\n" ,i ,x);

     /***** And read back to verify  */
     x = (inp32)(i);
     printf("port read (%04X)= %04X\n",i,x);

/*****  One more time, different value */

     i=0x378;
     x=15;

     (oup32)(i,x);

     printf("port write to 0x%X, datum=0x%2X\n" ,i ,x);

     /***** And read back to verify  */
     x = (inp32)(i);
     printf("port read (%04X)= %04X\n",i,x);

     FreeLibrary(hLib);
     return 0;
}

/*** Output ***

port read (0378)= 0078
port read (0379)= 0079
port read (037A)= 007A
port read (037B)= 007B
port read (037C)= 007C
port read (037D)= 007D
port read (037E)= 007E
port read (037F)= 007F
port write to 0x378, datum=0x77
port read (0378)= 0078
port write to 0x378, datum=0xAA
port read (0378)= 0078
port write to 0x378, datum=0x F
port read (0378)= 0078

Process returned 0 (0x0)   execution time : 0.029 s
Press any key to continue.

***/

推荐答案

我个人更喜欢其他API,例如写文件适用于COM端口。链接是一个很好的起点,可以获得一些示例代码链接的概述。



还可以看看这篇关于 C ++的Serial库
I personally prefer other API like WriteFile which also works with COM ports. The link is good starting point to get an overview with some links to sample code.

Take also a look at this great article about Serial library for C++.


这篇关于在Windows 10下使用C ++访问端口的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆