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

自闭症网瘾萝莉.ら 提交于 2019-12-12 07:16:16

问题


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.

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.
  • I have also looked at some projects online but they seem to work for Windows XP but fail to work for Windows 7.
  • Also, most projects were written in VB or C# which I am not familiar with.

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? (code snippets would be appreciated)
  • Is there a cross platform way of dealing communicating with parallel ports?

回答1:


Have a look at codeproject: here, here and here. You'll find treasures.

The 1st link works for Windows 7 - both 32 bit and 64 bit.




回答2:


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. 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);


来源:https://stackoverflow.com/questions/4985909/how-to-set-and-read-pins-on-the-parallel-port-from-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!