Add TrySendAndReceive

This commit is contained in:
2020-11-24 15:34:50 +01:00
parent 9b6e8799b0
commit 1eefc8993c
2 changed files with 32 additions and 4 deletions

View File

@@ -19,7 +19,12 @@ namespace WaveshareUARTFingerprintSensor.Sample
sensor.Start(); sensor.Start();
var count = sensor.GetUserCount(); if (sensor.TryGetUserCount(out ushort count))
{
Console.WriteLine(count);
}
Console.WriteLine("End");
Thread.Sleep(-1); Thread.Sleep(-1);
} }

View File

@@ -102,6 +102,22 @@ namespace WaveshareUARTFingerprintSensor
return (buffer[2], buffer[3], (ResponseType)buffer[4]); return (buffer[2], buffer[3], (ResponseType)buffer[4]);
} }
private bool TrySendAndReceive(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, ResponseType responseType) response, int timeout = DefaultTimeout)
{
try
{
response = SendAndReceive(commandType, first, second, third, timeout);
}
catch (Exception)
{
response = default;
return false;
}
return true;
}
/* /*
private void Send(CommandType commandType, byte first, byte second, byte third) private void Send(CommandType commandType, byte first, byte second, byte third)
{ {
@@ -138,11 +154,18 @@ namespace WaveshareUARTFingerprintSensor
public bool TryGetUserCount(out ushort count) public bool TryGetUserCount(out ushort count)
{ {
(byte countHigh, byte countLow, ResponseType response) = SendAndReceive(CommandType.QueryUserCount, 0, 0, 0); if (TrySendAndReceive(CommandType.QueryUserCount, 0, 0, 0, out var response))
{
(byte countHigh, byte countLow, ResponseType responseType) = response;
count = Utils.Merge(countHigh, countLow); count = Utils.Merge(countHigh, countLow);
return response == ResponseType.Success; return responseType == ResponseType.Success;
}
count = default;
return false;
} }
public void Sleep() public void Sleep()