diff --git a/WaveshareUARTFingerprintSensor.Sample/Program.cs b/WaveshareUARTFingerprintSensor.Sample/Program.cs index bdd5f59..3bc31cc 100644 --- a/WaveshareUARTFingerprintSensor.Sample/Program.cs +++ b/WaveshareUARTFingerprintSensor.Sample/Program.cs @@ -19,7 +19,12 @@ namespace WaveshareUARTFingerprintSensor.Sample sensor.Start(); - var count = sensor.GetUserCount(); + if (sensor.TryGetUserCount(out ushort count)) + { + Console.WriteLine(count); + } + + Console.WriteLine("End"); Thread.Sleep(-1); } diff --git a/WaveshareUARTFingerprintSensor/FingerprintSensor.cs b/WaveshareUARTFingerprintSensor/FingerprintSensor.cs index 12920f4..0dae72f 100644 --- a/WaveshareUARTFingerprintSensor/FingerprintSensor.cs +++ b/WaveshareUARTFingerprintSensor/FingerprintSensor.cs @@ -102,6 +102,22 @@ namespace WaveshareUARTFingerprintSensor 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) { @@ -138,11 +154,18 @@ namespace WaveshareUARTFingerprintSensor 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()