turn on LED using C

598 Views Asked by At

I want to turn on a LED using C, meaning that I want to write on parallel port.

but the code doesn't work.

I use char ledStatus instead of BYTE ledStatus. is there any difference??

what is the problem in this code?

#include <windows.h>
#include <conio.h>
#include <staio.h>
#define LED_ON  1

int main()
{
   HANDLE h;
   unsigned long  dwSize=1;
   int success;

   h = CreateFile(
      L"LPT1",
      GENERIC_WRITE, // access (read-write) mode
      0, // share mode
      NULL, // pointer to security attributes
      OPEN_EXISTING, // how to create
      FILE_ATTRIBUTE_NORMAL, // file attributes
      NULL // handle to file with attributes to copy
   );

   if (INVALID_HANDLE_VALUE == h)
   {
      //Handle Error
      printf("CreateFile failed with error %d\n", GetLastError());
      exit(1);
   }
   else
   {
      printf("CreateFile1 Successful\n");
   }

   char   ledStatus;
   // turn on LED
   ledStatus = LED_ON;
   success = WriteFile(h, &ledStatus, 1, &dwSize, NULL);
   if (success)
   {
      printf("File Write Successful - %i bytes\n", dwSize);
   }
   else
   {
      printf("File Write Failed\n");
   }

   // close port
   CloseHandle(h);
   return 0;
}
1

There are 1 best solutions below

0
On

Your question is very poorly documented, you didn't describe what signal you used or how you wired the LED. Lots of ways to get that wrong. But you have no hope of making it work with the standard Windows parallel driver. It was written to interface parallel devices like printers. Which requires handshaking to clock a byte to the device. The driver turns on the STROBE signal, the device must turn on the ACK signal to acknowledge it copied the byte. That of course doesn't happen, the WriteFile() calls only fill a buffer in the driver.

You'll need another driver to directly control the output lines, Inpout32 is a common choice. Find essential advice in Jan Axelson's book, also includes a link to Inpout32.