diff --git a/WaveshareUARTFingerprintSensor/Exceptions/SensorStateException.cs b/WaveshareUARTFingerprintSensor/Exceptions/SensorStateException.cs new file mode 100644 index 0000000..aa0ccb5 --- /dev/null +++ b/WaveshareUARTFingerprintSensor/Exceptions/SensorStateException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WaveshareUARTFingerprintSensor.Exceptions +{ + [Serializable] + public class SensorStateException : Exception + { + public const string SensorSleepingMessage = "Sensor is sleeping, can't send commands"; + + public SensorStateException() { } + public SensorStateException(string message) : base(message) { } + public SensorStateException(string message, Exception inner) : base(message, inner) { } + protected SensorStateException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/WaveshareUARTFingerprintSensor/FingerprintSensor.cs b/WaveshareUARTFingerprintSensor/FingerprintSensor.cs index 38dce9f..e87f1ff 100644 --- a/WaveshareUARTFingerprintSensor/FingerprintSensor.cs +++ b/WaveshareUARTFingerprintSensor/FingerprintSensor.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Unosquare.RaspberryIO; using Unosquare.RaspberryIO.Abstractions; using Unosquare.WiringPi; +using WaveshareUARTFingerprintSensor.Exceptions; namespace WaveshareUARTFingerprintSensor { @@ -32,6 +33,7 @@ namespace WaveshareUARTFingerprintSensor private readonly int _rstPinNumber; private IGpioPin _wakePin; private IGpioPin _rstPin; + private bool _sleeping = false; private readonly object _lock = new object(); public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24) @@ -75,6 +77,11 @@ namespace WaveshareUARTFingerprintSensor private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout) { + if (_sleeping) + { + throw new SensorStateException(SensorStateException.SensorSleepingMessage); + } + // Command packet byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator }; @@ -170,11 +177,13 @@ namespace WaveshareUARTFingerprintSensor public void Sleep() { + _sleeping = true; _rstPin.Write(GpioPinValue.Low); } public void Wake() { + _sleeping = false; _rstPin.Write(GpioPinValue.High); }