简体   繁体   English

如何从C ++设置和读取并行端口上的引脚?

[英]How to set and read pins on the parallel port from C++?

I am helping a friend to finish a final year project in which he has this circuit that we want to switch on and off using a C++ program. 我正在帮助一个朋友完成最后一个项目,在这个项目中他拥有我们想要使用C ++程序打开和关闭的电路。

我想要的图像

I initially thought it would be easy, but I have failed to implement this program. 我最初以为这很容易,但是我没有实现该程序。 The main problem is that 主要的问题是

  • Windows XP and above don't allow direct access to hardware so some websites are suggesting that I need to write a driver or find a driver. Windows XP及更高版本不允许直接访问硬件,因此某些网站建议我需要编写驱动程序或查找驱动程序。
  • I have also looked at some projects online but they seem to work for Windows XP but fail to work for Windows 7. 我也在线查看了一些项目,但它们似乎适用于Windows XP,但不适用于Windows 7。
  • Also, most projects were written in VB or C# which I am not familiar with. 另外,大多数项目都是用VB或C#编写的,我对此并不熟悉。

Question: 题:

  • Is there a suitable driver that works for Windows XP and Windows 7, and if yes how can I use it in my code? 是否有适用于Windows XP和Windows 7的驱动程序?如果是,如何在我的代码中使用它? (code snippets would be appreciated) (代码段不胜感激)
  • Is there a cross platform way of dealing communicating with parallel ports? 是否有跨平台的方式来处理与并行端口的通信?

You shouldn't need to write a driver or anything -- you just call CreateFile with a filename like "LPT1" to open up a handle to the parallel port, and then you can use WriteFile to write data to it. 您不需要编写驱动程序或任何东西-您只需使用诸如"LPT1"类的文件名调用CreateFile即可打开并行端口的句柄,然后可以使用WriteFile向其中写入数据。 For example: 例如:

HANDLE parallelPort = CreateFile("LPT1", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(parallelPort == INVALID_HANDLE_VALUE)
{
    // handle error
}
...
// Write the string "foobar" (and its null terminator) to the parallel port.
// Error checking omitted for expository purposes.
const char *data = "foobar";
WriteFile(parallelPort, data, strlen(data)+1, NULL, NULL);
...
CloseHandle(parallelPort);

Have a look at codeproject: here , here and here . 看看codeproject: 这里这里这里 You'll find treasures. 您会发现宝藏。

The 1st link works for Windows 7 - both 32 bit and 64 bit. 第一个链接适用于Windows 7-32位和64位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM