Skip to content

Commit 3d488cb

Browse files
committed
API extended with a new function: You can now call the FPS constructor with the desired baud rate you want to establish communications, as additional parameter. Furthermore, comms won't fail while initializing if the FPS baud rate was established to something else than 9600.
1 parent 400ff3b commit 3d488cb

File tree

2 files changed

+134
-10
lines changed

2 files changed

+134
-10
lines changed

src/FPS_GT511C3.cpp

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,15 @@ void Data_Packet::SendToSerial(uint8_t data[], uint16_t length)
316316
#pragma region -= Constructor/Destructor =-
317317
#endif //__GNUC__
318318
// Creates a new object to interface with the fingerprint scanner
319+
// It will establish the communication to the desired baud rate if defined
319320
FPS_GT511C3::FPS_GT511C3(uint8_t rx, uint8_t tx, uint32_t baud)
320321
: _serial(rx,tx)
321322
{
322323
pin_RX = rx;
323324
pin_TX = tx;
324-
_serial.begin(baud);
325325
this->UseSerialDebug = false;
326+
this->Started = false;
327+
desiredBaud = baud;
326328
};
327329

328330
// destructor
@@ -338,8 +340,10 @@ FPS_GT511C3::~FPS_GT511C3()
338340
#pragma region -= Device Commands =-
339341
#endif //__GNUC__
340342
//Initialises the device and gets ready for commands
341-
void FPS_GT511C3::Open()
343+
//Returns true if the communication established
344+
bool FPS_GT511C3::Open()
342345
{
346+
if (!Started) Start();
343347
if (UseSerialDebug) Serial.println("FPS - Open");
344348
Command_Packet* cp = new Command_Packet();
345349
cp->Command = Command_Packet::Commands::Open;
@@ -351,8 +355,11 @@ void FPS_GT511C3::Open()
351355
delete cp;
352356
SendCommand(packetbytes, 12);
353357
Response_Packet* rp = GetResponse();
358+
bool retval = true;
359+
if (rp->ACK == false) retval = false;
354360
delete rp;
355361
delete packetbytes;
362+
return retval;
356363
}
357364

358365
// According to the DataSheet, this does nothing...
@@ -413,8 +420,7 @@ bool FPS_GT511C3::ChangeBaudRate(uint32_t baud)
413420
{
414421
if ((baud == 9600) || (baud == 19200) || (baud == 38400) || (baud == 57600) || (baud == 115200))
415422
{
416-
417-
if (UseSerialDebug) Serial.println("FPS - ChangeBaudRate");
423+
if (UseSerialDebug) Serial.println("FPS - ChangeBaudRate");
418424
Command_Packet* cp = new Command_Packet();
419425
cp->Command = Command_Packet::Commands::ChangeBaudRate;
420426
cp->ParameterFrom(baud);
@@ -847,6 +853,114 @@ bool FPS_GT511C3::GetRawImage()
847853
#ifndef __GNUC__
848854
#pragma region -= Private Methods =-
849855
#endif //__GNUC__
856+
// Configures the device correctly for communications at the desired baud rate
857+
void FPS_GT511C3::Start()
858+
{
859+
Command_Packet* cp = new Command_Packet();
860+
cp->Command = Command_Packet::Commands::Open;
861+
cp->Parameter[0] = 0x00;
862+
cp->Parameter[1] = 0x00;
863+
cp->Parameter[2] = 0x00;
864+
cp->Parameter[3] = 0x00;
865+
uint8_t* packetbytes = cp->GetPacketBytes();
866+
delete cp;
867+
868+
uint32_t baud = desiredBaud;
869+
if (!(baud == 9600) && !(baud == 19200) && !(baud == 38400) && !(baud == 57600) && !(baud == 115200)) baud=9600;
870+
uint32_t actualBaud = 0;
871+
uint32_t BaudRates[5] = {9600, 19200, 38400, 57600, 115200};
872+
for(uint8_t i = 0; i<5; i++) // Trying to find FPS baud rate
873+
{
874+
if(UseSerialDebug)
875+
{
876+
Serial.print("Establishing connection with FPS at baud rate: ");
877+
Serial.print(BaudRates[i]);
878+
Serial.println();
879+
}
880+
_serial.begin(BaudRates[i]);
881+
_serial.listen();
882+
SendCommand(packetbytes, 12);
883+
delay(100);
884+
885+
uint8_t firstbyte = 0;
886+
uint8_t secondbyte = 0;
887+
bool done = false;
888+
uint8_t byteCount = 0;
889+
while (done == false && byteCount<100)
890+
{
891+
byteCount++;
892+
if(_serial.peek() == -1) break;
893+
firstbyte = (uint8_t)_serial.read();
894+
if (firstbyte == Response_Packet::COMMAND_START_CODE_1)
895+
{
896+
if(_serial.peek() == -1) break;
897+
secondbyte = (uint8_t)_serial.read();
898+
if (secondbyte == Response_Packet::COMMAND_START_CODE_2)
899+
{
900+
done = true;
901+
}
902+
}
903+
}
904+
if (!done)
905+
{
906+
while (_serial.available()) _serial.read(); // Clear Serial buffer
907+
} else
908+
{
909+
uint8_t* resp = new uint8_t[12];
910+
resp[0] = firstbyte;
911+
resp[1] = secondbyte;
912+
for (uint8_t i=2; i < 12; i++)
913+
{
914+
while (_serial.available() == false) delay(10);
915+
resp[i]= (uint8_t) _serial.read();
916+
}
917+
if (UseSerialDebug)
918+
{
919+
Response_Packet* rp = new Response_Packet(resp, UseSerialDebug);
920+
Serial.print("FPS - RECV: ");
921+
SendToSerial(rp->RawBytes, 12);
922+
Serial.println();
923+
Serial.println();
924+
delete rp;
925+
}
926+
delete resp;
927+
actualBaud = BaudRates[i];
928+
break;
929+
}
930+
}
931+
932+
if(UseSerialDebug)
933+
{
934+
Serial.print("Connection established succesfully. FPS baud rate was: ");
935+
Serial.print(actualBaud);
936+
Serial.println();
937+
Serial.println();
938+
}
939+
940+
if (actualBaud == 0) while(true)
941+
{
942+
if(UseSerialDebug)
943+
{
944+
Serial.print("EXCEPTION: FPS didn't answer to communications. Code execution stopped.");
945+
Serial.println();
946+
}
947+
delay(1000); // Something went terribly wrong with the FPS, and you aren't allowed to leave
948+
}
949+
950+
if (actualBaud != baud)
951+
{
952+
if(UseSerialDebug)
953+
{
954+
Serial.print("Undesired baud rate. Changing baud rate to: ");
955+
Serial.print(baud);
956+
Serial.println();
957+
Serial.println();
958+
}
959+
ChangeBaudRate(baud);
960+
}
961+
Started = true;
962+
}
963+
850964
// Sends the command to the software serial channel
851965
void FPS_GT511C3::SendCommand(uint8_t cmd[], uint16_t length)
852966
{

src/FPS_GT511C3.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ class FPS_GT511C3
181181
public:
182182
// Enables verbose debug output using hardware Serial
183183
bool UseSerialDebug;
184+
uint32_t desiredBaud;
184185

185186
#ifndef __GNUC__
186187
#pragma region -= Constructor/Destructor =-
187188
#endif //__GNUC__
188189
// Creates a new object to interface with the fingerprint scanner
190+
// It will establish the communication to the desired baud rate if defined
189191
FPS_GT511C3(uint8_t rx, uint8_t tx, uint32_t baud = 9600);
190192

191193
// destructor
@@ -199,7 +201,8 @@ class FPS_GT511C3
199201
#pragma region -= Device Commands =-
200202
#endif //__GNUC__
201203
//Initialises the device and gets ready for commands
202-
void Open();
204+
//Returns true if the communication established
205+
bool Open();
203206

204207
// Does not actually do anything (according to the datasheet)
205208
// I implemented open, so had to do closed too... lol
@@ -361,11 +364,18 @@ class FPS_GT511C3
361364
void SendToSerial(uint8_t data[], uint16_t length);
362365

363366
private:
364-
void SendCommand(uint8_t cmd[], uint16_t length);
365-
Response_Packet* GetResponse();
366-
void GetData(uint16_t length);
367-
uint8_t pin_RX,pin_TX;
368-
SoftwareSerial _serial;
367+
368+
// Indicates if the communication was configured for the first time
369+
bool Started;
370+
371+
//Configures the device correctly for communications at the desired baud rate
372+
void Start();
373+
374+
void SendCommand(uint8_t cmd[], uint16_t length);
375+
Response_Packet* GetResponse();
376+
void GetData(uint16_t length);
377+
uint8_t pin_RX,pin_TX;
378+
SoftwareSerial _serial;
369379
};
370380

371381

0 commit comments

Comments
 (0)