Compare commits

...

8 Commits

Author SHA1 Message Date
f93a821a2b Add TryGetComparisonLevel and TrySetComparisonLevel commands 2020-11-30 09:45:30 +01:00
76163715bb Add TryQueryPermission command 2020-11-30 09:39:50 +01:00
713a3a2188 Clean code 2020-11-30 09:36:46 +01:00
7fa2159d05 Add Comparison11 and TryComparison1N commands 2020-11-30 09:31:17 +01:00
bf8ce006e4 Add DeleteAllUsers command 2020-11-30 09:05:16 +01:00
da763d5912 Add DeleteUser command 2020-11-30 09:01:38 +01:00
4287e00665 Add AddFingerprintAndAcquireEigenvalues command 2020-11-30 09:01:33 +01:00
ca04700291 Add ReadData 2020-11-30 08:45:05 +01:00
2 changed files with 218 additions and 13 deletions

View File

@@ -75,6 +75,18 @@ namespace WaveshareUARTFingerprintSensor
return checksum;
}
private byte ComputeChecksumData(byte[] data, int length)
{
byte checksum = 0;
for (int i = 1; i < length + 1; i++)
{
checksum ^= data[i];
}
return checksum;
}
private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
{
if (_sleeping)
@@ -152,6 +164,30 @@ namespace WaveshareUARTFingerprintSensor
return true;
}
private byte[] ReadData(int length)
{
byte first = (byte)_serialPort.ReadByte();
if (first != PacketSeparator)
{
throw new InvalidDataException("Invalid response from the sensor");
}
byte[] data = new byte[length];
_serialPort.Read(data, 0, length);
byte checksum = (byte)_serialPort.ReadByte();
byte separator = (byte)_serialPort.ReadByte();
if (separator != PacketSeparator || checksum != ComputeChecksumData(data, length))
{
throw new InvalidDataException("Invalid checksum");
}
return data;
}
public uint QuerySerialNumber()
{
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
@@ -175,18 +211,6 @@ namespace WaveshareUARTFingerprintSensor
return false;
}
public void Sleep()
{
_sleeping = true;
_rstPin.Write(GpioPinValue.Low);
}
public void Wake()
{
_sleeping = false;
_rstPin.Write(GpioPinValue.High);
}
public ResponseType AddFingerprint(ushort userID, UserPermission userPermission)
{
if (userID > MaxUserID)
@@ -217,6 +241,187 @@ namespace WaveshareUARTFingerprintSensor
return ResponseType.Success;
}
public (ResponseType responseType, byte[] eigenvalues) AddFingerprintAndAcquireEigenvalues(ushort userID, UserPermission userPermission)
{
if (userID > MaxUserID)
{
return (ResponseType.Full, null);
}
CommandType[] commands = { CommandType.AddFingerprint1, CommandType.AddFingerprint2 };
(byte idHigh, byte idLow) = Utils.Split(userID);
foreach (var command in commands)
{
if (TrySendAndReceive(command, idHigh, idLow, (byte)userPermission, out var loopResponse))
{
if (loopResponse.responseType != ResponseType.Success)
{
return (loopResponse.responseType, null);
}
}
else
{
return (ResponseType.Timeout, null);
}
Thread.Sleep(50);
}
if (TrySendAndReceive(CommandType.AddAndAcquireFingerprint, 0, 0, 0, out var response))
{
if (response.responseType != ResponseType.Success)
{
return (response.responseType, null);
}
ushort length = Utils.Merge(response.first, response.second);
var data = ReadData(length);
var eigenvalues = data.Skip(3).ToArray();
return (ResponseType.Success, eigenvalues);
}
else
{
return (ResponseType.Timeout, null);
}
}
public bool DeleteUser(ushort userID)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.DeleteUser, high, low, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
public bool DeleteAllUsers()
{
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
public bool DeleteAllUsersWithPermission(UserPermission userPermission)
{
if (TrySendAndReceive(CommandType.DeleteAllUsers, 0, 0, (byte)userPermission, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Read a fingerprint and check if it matches with the specified user
/// </summary>
/// <param name="userID">A registered user ID</param>
/// <returns></returns>
public bool Comparison11(ushort userID)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.Comparison11, high, low, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
/// <summary>
/// Read a fingerprint and check if it match with any registered user
/// </summary>
/// <param name="userInfo">The matched user info</param>
/// <returns></returns>
public bool TryComparison1N(out (ushort userID, UserPermission permission) userInfo)
{
if (TrySendAndReceive(CommandType.Comparison11, 0, 0, 0, out var response, 1000))
{
if (response.responseType != ResponseType.NoUser && response.responseType != ResponseType.Timeout)
{
userInfo = (Utils.Merge(response.first, response.second), (UserPermission)response.responseType);
return true;
}
}
userInfo = default;
return false;
}
public bool TryQueryPermission(ushort userID, out UserPermission userPermission)
{
(byte high, byte low) = Utils.Split(userID);
if (TrySendAndReceive(CommandType.QueryPermission, high, low, 0, out var reponse, 1000))
{
if (reponse.responseType != ResponseType.NoUser)
{
userPermission = (UserPermission)reponse.responseType;
return true;
}
}
userPermission = default;
return true;
}
public bool TryGetComparisonLevel(out byte comparisonLevel)
{
if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, 0, 1, out var response, 1000))
{
if (response.responseType == ResponseType.Success)
{
comparisonLevel = response.second;
return true;
}
}
comparisonLevel = default;
return false;
}
public bool TrySetComparisonLevel(byte comparisonLevel)
{
if (comparisonLevel < 0 || comparisonLevel > 9)
{
return false;
}
if (TrySendAndReceive(CommandType.ManageComparisonLevel, 0, comparisonLevel, 0, out var response, 1000))
{
return response.responseType == ResponseType.Success;
}
return false;
}
public void Sleep()
{
_sleeping = true;
_rstPin.Write(GpioPinValue.Low);
}
public void Wake()
{
_sleeping = false;
_rstPin.Write(GpioPinValue.High);
}
private void OnWake()
{
if (_wakePin.Read())

View File

@@ -1,6 +1,6 @@
namespace WaveshareUARTFingerprintSensor
{
public enum UserPermission
public enum UserPermission : byte
{
Level1 = 1,
Level2 = 2,