Compare commits
8 Commits
6ea11cb368
...
f93a821a2b
| Author | SHA1 | Date | |
|---|---|---|---|
| f93a821a2b | |||
| 76163715bb | |||
| 713a3a2188 | |||
| 7fa2159d05 | |||
| bf8ce006e4 | |||
| da763d5912 | |||
| 4287e00665 | |||
| ca04700291 |
@@ -75,6 +75,18 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
return checksum;
|
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)
|
private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
|
||||||
{
|
{
|
||||||
if (_sleeping)
|
if (_sleeping)
|
||||||
@@ -152,6 +164,30 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
return true;
|
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()
|
public uint QuerySerialNumber()
|
||||||
{
|
{
|
||||||
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
|
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
|
||||||
@@ -175,18 +211,6 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
return false;
|
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)
|
public ResponseType AddFingerprint(ushort userID, UserPermission userPermission)
|
||||||
{
|
{
|
||||||
if (userID > MaxUserID)
|
if (userID > MaxUserID)
|
||||||
@@ -217,6 +241,187 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
return ResponseType.Success;
|
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()
|
private void OnWake()
|
||||||
{
|
{
|
||||||
if (_wakePin.Read())
|
if (_wakePin.Read())
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace WaveshareUARTFingerprintSensor
|
namespace WaveshareUARTFingerprintSensor
|
||||||
{
|
{
|
||||||
public enum UserPermission
|
public enum UserPermission : byte
|
||||||
{
|
{
|
||||||
Level1 = 1,
|
Level1 = 1,
|
||||||
Level2 = 2,
|
Level2 = 2,
|
||||||
|
|||||||
Reference in New Issue
Block a user