mirror of
https://github.com/tenrok/Rfid-Credential-Provider.git
synced 2026-06-11 18:12:24 +03:00
Z-2 USB Reader
This commit is contained in:
@@ -4,11 +4,12 @@
|
||||
|
||||
Serial::Serial()
|
||||
{
|
||||
//We're not yet connected
|
||||
// We're not yet connected
|
||||
this->connected = false;
|
||||
|
||||
HKEY hKey;
|
||||
if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Tyler Menezes\\Rfid Login", 0, KEY_READ, &hKey) != ERROR_SUCCESS){
|
||||
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\KORNET\\WinLogin", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
|
||||
{
|
||||
delete hKey;
|
||||
return; // Token not recognized.
|
||||
}
|
||||
@@ -16,24 +17,23 @@ Serial::Serial()
|
||||
std::wstring port;
|
||||
GetStringRegKey(hKey, L"Port", port, L"COM3");
|
||||
|
||||
//Try to connect to the given port throuh CreateFile
|
||||
// Try to connect to the given port throuh CreateFile
|
||||
this->hSerial = CreateFile(port.c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL);
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL);
|
||||
|
||||
//Check if the connection was successfull
|
||||
if(this->hSerial==INVALID_HANDLE_VALUE)
|
||||
// Check if the connection was successfull
|
||||
if (this->hSerial == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
//If not success full display an Error
|
||||
if(GetLastError()==ERROR_FILE_NOT_FOUND){
|
||||
|
||||
//Print Error if neccessary
|
||||
// If not success full display an Error
|
||||
if (GetLastError() == ERROR_FILE_NOT_FOUND)
|
||||
{
|
||||
// Print Error if neccessary
|
||||
printf("ERROR: Handle was not attached. Reason: %s not available.\n");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -42,69 +42,68 @@ Serial::Serial()
|
||||
}
|
||||
else
|
||||
{
|
||||
//If connected we try to set the comm parameters
|
||||
DCB dcbSerialParams = {0};
|
||||
// If connected we try to set the comm parameters
|
||||
DCB dcbSerialParams = { 0 };
|
||||
|
||||
//Try to get the current
|
||||
// Try to get the current
|
||||
if (!GetCommState(this->hSerial, &dcbSerialParams))
|
||||
{
|
||||
//If impossible, show an error
|
||||
// If impossible, show an error
|
||||
printf("failed to get current serial parameters!");
|
||||
}
|
||||
else
|
||||
{
|
||||
//Define serial connection parameters for the arduino board
|
||||
dcbSerialParams.BaudRate=CBR_9600;
|
||||
dcbSerialParams.ByteSize=8;
|
||||
dcbSerialParams.StopBits=ONESTOPBIT;
|
||||
dcbSerialParams.Parity=NOPARITY;
|
||||
// Define serial connection parameters for the arduino board
|
||||
dcbSerialParams.BaudRate = CBR_9600;
|
||||
dcbSerialParams.ByteSize = 8;
|
||||
dcbSerialParams.StopBits = ONESTOPBIT;
|
||||
dcbSerialParams.Parity = NOPARITY;
|
||||
|
||||
//Set the parameters and check for their proper application
|
||||
if(!SetCommState(hSerial, &dcbSerialParams))
|
||||
{
|
||||
// Set the parameters and check for their proper application
|
||||
if (!SetCommState(hSerial, &dcbSerialParams))
|
||||
{
|
||||
printf("ALERT: Could not set Serial Port parameters");
|
||||
}
|
||||
else
|
||||
{
|
||||
//If everything went fine we're connected
|
||||
this->connected = true;
|
||||
//We wait 2s as the arduino board will be reseting
|
||||
Sleep(ARDUINO_WAIT_TIME);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If everything went fine we're connected
|
||||
this->connected = true;
|
||||
// We wait 2s as the arduino board will be reseting
|
||||
Sleep(ARDUINO_WAIT_TIME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Serial::~Serial()
|
||||
{
|
||||
//Check if we are connected before trying to disconnect
|
||||
if(this->connected)
|
||||
// Check if we are connected before trying to disconnect
|
||||
if (this->connected)
|
||||
{
|
||||
//We're no longer connected
|
||||
// We're no longer connected
|
||||
this->connected = false;
|
||||
//Close the serial handler
|
||||
// Close the serial handler
|
||||
CloseHandle(this->hSerial);
|
||||
}
|
||||
}
|
||||
|
||||
int Serial::ReadData(char *buffer, unsigned int nbChar)
|
||||
int Serial::ReadData(char* buffer, unsigned int nbChar)
|
||||
{
|
||||
//Number of bytes we'll have read
|
||||
// Number of bytes we'll have read
|
||||
DWORD bytesRead;
|
||||
//Number of bytes we'll really ask to read
|
||||
// Number of bytes we'll really ask to read
|
||||
unsigned int toRead;
|
||||
|
||||
//Use the ClearCommError function to get status info on the Serial port
|
||||
// Use the ClearCommError function to get status info on the Serial port
|
||||
ClearCommError(this->hSerial, &this->errors, &this->status);
|
||||
|
||||
//Check if there is something to read
|
||||
if(this->status.cbInQue>0)
|
||||
// Check if there is something to read
|
||||
if (this->status.cbInQue > 0)
|
||||
{
|
||||
//If there is we check if there is enough data to read the required number
|
||||
//of characters, if not we'll read only the available characters to prevent
|
||||
//locking of the application.
|
||||
if(this->status.cbInQue>nbChar)
|
||||
// If there is we check if there is enough data to read the required number
|
||||
// of characters, if not we'll read only the available characters to prevent
|
||||
// locking of the application.
|
||||
if (this->status.cbInQue > nbChar)
|
||||
{
|
||||
toRead = nbChar;
|
||||
}
|
||||
@@ -113,30 +112,27 @@ int Serial::ReadData(char *buffer, unsigned int nbChar)
|
||||
toRead = this->status.cbInQue;
|
||||
}
|
||||
|
||||
//Try to read the require number of chars, and return the number of read bytes on success
|
||||
if(ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
|
||||
// Try to read the require number of chars, and return the number of read bytes on success
|
||||
if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, nullptr) && bytesRead != 0)
|
||||
{
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//If nothing has been read, or that an error was detected return -1
|
||||
// If nothing has been read, or that an error was detected return -1
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool Serial::WriteData(char *buffer, unsigned int nbChar)
|
||||
bool Serial::WriteData(char* buffer, unsigned int nbChar)
|
||||
{
|
||||
DWORD bytesSend;
|
||||
|
||||
//Try to write the buffer on the Serial port
|
||||
if(!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
|
||||
// Try to write the buffer on the Serial port
|
||||
if (!WriteFile(this->hSerial, (void*)buffer, nbChar, &bytesSend, nullptr))
|
||||
{
|
||||
//In case it don't work get comm error and return false
|
||||
// In case it don't work get comm error and return false
|
||||
ClearCommError(this->hSerial, &this->errors, &this->status);
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
@@ -145,6 +141,6 @@ bool Serial::WriteData(char *buffer, unsigned int nbChar)
|
||||
|
||||
bool Serial::IsConnected()
|
||||
{
|
||||
//Simply return the connection status
|
||||
// Simply return the connection status
|
||||
return this->connected;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user