I am writing a program to control 开发者_JS百科a flashbulb. The flash fires in response to a key press by the user. I am trying to limit the occurence regularity of the flash to prevent the bulb burning out. I have already received some help from this forum, but am unable to implement the code with my own. A user suggested using a class, as follows:
class bulb
{
__int64 clocks;
__int64 frequency;
public:
bulb()
{
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
frequency = li.QuadPart;
clocks = 0;
}
void WINAPI flash (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
// If this is the first occurence, set the 'clocks' to system time (+10000 to allow flash to occur)
if (clocks == 0) clocks = li.QuadPart + 10000;
__int64 timepassed = clocks - li.QuadPart;
if (timepassed >= (((double)frequency) / 10000))
{
//Set the clock
clocks = li.QuadPart;
//Define the serial port procedure
HANDLE hSerial;
//Open the serial port (fire the flash)
hSerial = CreateFile("COM1", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
//Close the serial port
CloseHandle(hSerial);
}
}
};
I receive a few syntax errors that I can't seem to shift, all of which are at either the first or last bracket of the class - "syntax error : identifier 'bulb'", "syntax error : ';'", "syntax error : '}'" and "syntax error : '}'". I have never worked with classes before though, so expect this is something to do with that. Where am I going wrong?
Please note '10000' is the minimum delay between flashes.
There are a few major issues with your code:
- Missing a ';' at the end of the class definition.
- Missing a definition
HANDLE hSerial
before it is used. You are comparing the time and frequency incorrectly on the line
if (timepassed >= (((double)frequency) / 10000))
. If you wish to convert the counter fromQueryPerformanceCounter
into a real time use something like:double RealTime = (double) clocks / (double) frequency;
If you are getting other error messages they are related to the code before or after the snippet you posted. A few more minor issues and comments:
- Both
QueryPerformanceFrequency
andQueryPerformanceCounter
can fail. Unless getting invalid values doesn't matter you should be checking the return values from these. - You open a COM port but don't write anything to it or confirm that the open succeeded or not.
- To avoid future issues the if statement
if (clocks == 0)
should all be on one line or include brackets, i.e., one of:
:
if (clocks == 0) clocks = li.QuadPart + 10000;
if (clocks == 0) {
clocks = li.QuadPart + 10000;
}
Edit: Example for converting QueryPerformanceCounter into real times (error checking not included):
LARGE_INTEGER Frequency;
LARGE_INTEGER Counter;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&Counter);
//Time in seconds
double RealTime = (double) Counter.QuadPart / (double)Frequency.QuadPart;
LARGE_INTEGER Counter1;
QueryPerformanceCounter(&Counter1);
//Elapsed time in seconds
double DeltaTime = (double) (Counter1.QuadPart - Counter.QuadPart) / (double)Frequency.QuadPart;
See Also: How to use QueryPerformanceCounter?
add ;
after last }
in your code
class bulb
{
...
};
精彩评论