I have connected a photographic flash unit to my computer using a relay switch connected to the serial port. The following code causes the strobe to flash at 4Hz for 10 flashes:
#include <windows.h>
//Initialise Windows module
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
//Define the serial port precedure
HANDLE hSerial;
int freq = 4;
int iterations = 10;
int x;
for ( x = 0; x < iterations; x++)
{
//Fire the flash (open the serial port, and immediately close it)
hSerial = CreateFile("COM1",GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
CloseHandle(hSerial);
//Sleep in between flashes for specified duration
Sleep (1000/freq);
}
return 0;
}
How do I implement dialog boxes at the beginning of the program so that the user can input the values of 'freq' and 'iterations'?
Open Visual Studio, New, Project, Visual C++, Windows Forms Application. This will give you a GUI where you can drag and drop what you need. If you don't have Visual Studio then perhaps your IDE has something similar?
Make this a console app that accepts the data in the command line. Call the app with an appropriate command line from a GUI created in any other programming language/ framework.
Make the GUI in C# and use P/Invoke to call CreateFile; it's not that hard.
Btw, does the CreateFile/CloseHandle approach really work? I find it to be a bit "hacky" and I'm not sure it's the best approach. Perhaps another answer or a comment will touch this aspect as well.