1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;
string GetLastWinError ()
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dwLastError = GetLastError ();
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf,
0, NULL);
std::ostringstream oss;
oss << "Windows Error ";
oss << dwLastError << " :" << (LPCSTR)lpMsgBuf;
LocalFree (lpMsgBuf);
return oss.str ();
}
void main ()
{
char buffer[] = "00000000 00000000";
bool breakloop = false;
HANDLE SerialRead;
printf ("Setup serial handle \n");
SerialRead = CreateFile ("COM3", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL);
if (SerialRead == INVALID_HANDLE_VALUE)
{
cerr << "\a" << GetLastWinError () << "\n";
system ("pause");
exit (EXIT_FAILURE);
}
while (breakloop == false)
{
if (GetAsyncKeyState (0x54))
{
cout << "T Is pressed" << endl;
breakloop = true;
}
if (GetAsyncKeyState (0x46))
{
if (!ReadFile (SerialRead, buffer, 17, 0, 0))
{
cerr << "\a" << GetLastWinError () << "\n";
system ("pause");
exit (EXIT_FAILURE);
}
cout << buffer << endl;
//cout << "Passed Varialbe" << endl;
char buffer[] = "00000000 00000000";
}
}
CloseHandle (SerialRead);
SerialRead = INVALID_HANDLE_VALUE;
system ("pause");
}
|