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

前端 未结 2 1370
误落风尘
误落风尘 2021-01-03 06:33

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.

相关标签:
2条回答
  • 2021-01-03 07:09

    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);
    
    0 讨论(0)
  • 2021-01-03 07:10

    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.

    0 讨论(0)
提交回复
热议问题