Compare commits
3 Commits
67fb46174a
...
933aac042f
| Author | SHA1 | Date | |
|---|---|---|---|
| 933aac042f | |||
| 5af87ed327 | |||
| b2b275d638 |
@@ -19,9 +19,9 @@ namespace WaveshareUARTFingerprintSensor.Sample
|
|||||||
|
|
||||||
sensor.Start();
|
sensor.Start();
|
||||||
|
|
||||||
sensor.Waked += s => sensor.Wake();
|
Console.WriteLine("Adding fingerprint");
|
||||||
|
|
||||||
sensor.Sleep();
|
var response = sensor.AddFingerprint(40, UserPermission.Level3);
|
||||||
|
|
||||||
Console.WriteLine("End");
|
Console.WriteLine("End");
|
||||||
|
|
||||||
|
|||||||
@@ -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) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ using System.Threading.Tasks;
|
|||||||
using Unosquare.RaspberryIO;
|
using Unosquare.RaspberryIO;
|
||||||
using Unosquare.RaspberryIO.Abstractions;
|
using Unosquare.RaspberryIO.Abstractions;
|
||||||
using Unosquare.WiringPi;
|
using Unosquare.WiringPi;
|
||||||
|
using WaveshareUARTFingerprintSensor.Exceptions;
|
||||||
|
|
||||||
namespace WaveshareUARTFingerprintSensor
|
namespace WaveshareUARTFingerprintSensor
|
||||||
{
|
{
|
||||||
@@ -18,6 +19,7 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
public const string PrimarySerialPort = "/dev/ttyAMA0";
|
public const string PrimarySerialPort = "/dev/ttyAMA0";
|
||||||
public const string SecondarySerialPort = "/dev/ttyS0";
|
public const string SecondarySerialPort = "/dev/ttyS0";
|
||||||
public const int DefaultTimeout = 10_000;
|
public const int DefaultTimeout = 10_000;
|
||||||
|
public const int MaxUserID = 0xFFF;
|
||||||
|
|
||||||
public event WakedEventHandler Waked;
|
public event WakedEventHandler Waked;
|
||||||
public delegate void WakedEventHandler(FingerprintSensor sender);
|
public delegate void WakedEventHandler(FingerprintSensor sender);
|
||||||
@@ -27,11 +29,12 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
private const byte PacketSeparator = 0xF5;
|
private const byte PacketSeparator = 0xF5;
|
||||||
|
|
||||||
private SerialPort _serialPort;
|
private SerialPort _serialPort;
|
||||||
private int _wakePinNumber;
|
private readonly int _wakePinNumber;
|
||||||
private int _rstPinNumber;
|
private readonly int _rstPinNumber;
|
||||||
private IGpioPin _wakePin;
|
private IGpioPin _wakePin;
|
||||||
private IGpioPin _rstPin;
|
private IGpioPin _rstPin;
|
||||||
private object _lock = new object();
|
private bool _sleeping = false;
|
||||||
|
private readonly object _lock = new object();
|
||||||
|
|
||||||
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
|
public FingerprintSensor(string portName, int wakePin = 23, int rstPin = 24)
|
||||||
{
|
{
|
||||||
@@ -74,6 +77,11 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
|
|
||||||
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)
|
||||||
|
{
|
||||||
|
throw new SensorStateException(SensorStateException.SensorSleepingMessage);
|
||||||
|
}
|
||||||
|
|
||||||
// Command packet
|
// Command packet
|
||||||
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
||||||
|
|
||||||
@@ -169,14 +177,46 @@ namespace WaveshareUARTFingerprintSensor
|
|||||||
|
|
||||||
public void Sleep()
|
public void Sleep()
|
||||||
{
|
{
|
||||||
|
_sleeping = true;
|
||||||
_rstPin.Write(GpioPinValue.Low);
|
_rstPin.Write(GpioPinValue.Low);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Wake()
|
public void Wake()
|
||||||
{
|
{
|
||||||
|
_sleeping = false;
|
||||||
_rstPin.Write(GpioPinValue.High);
|
_rstPin.Write(GpioPinValue.High);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResponseType AddFingerprint(ushort userID, UserPermission userPermission)
|
||||||
|
{
|
||||||
|
if (userID > MaxUserID)
|
||||||
|
{
|
||||||
|
return ResponseType.Full;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandType[] commands = { CommandType.AddFingerprint1, CommandType.AddFingerprint2, CommandType.AddFingerprint3 };
|
||||||
|
(byte idHigh, byte idLow) = Utils.Split(userID);
|
||||||
|
|
||||||
|
foreach (var command in commands)
|
||||||
|
{
|
||||||
|
if (TrySendAndReceive(command, idHigh, idLow, (byte)userPermission, out var response))
|
||||||
|
{
|
||||||
|
if (response.responseType != ResponseType.Success)
|
||||||
|
{
|
||||||
|
return response.responseType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ResponseType.Timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseType.Success;
|
||||||
|
}
|
||||||
|
|
||||||
private void OnWake()
|
private void OnWake()
|
||||||
{
|
{
|
||||||
if (_wakePin.Read())
|
if (_wakePin.Read())
|
||||||
|
|||||||
9
WaveshareUARTFingerprintSensor/UserPermission.cs
Normal file
9
WaveshareUARTFingerprintSensor/UserPermission.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace WaveshareUARTFingerprintSensor
|
||||||
|
{
|
||||||
|
public enum UserPermission
|
||||||
|
{
|
||||||
|
Level1 = 1,
|
||||||
|
Level2 = 2,
|
||||||
|
Level3 = 3
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,6 +65,7 @@
|
|||||||
<Compile Include="FingerprintSensor.cs" />
|
<Compile Include="FingerprintSensor.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ResponseType.cs" />
|
<Compile Include="ResponseType.cs" />
|
||||||
|
<Compile Include="UserPermission.cs" />
|
||||||
<Compile Include="Utils.cs" />
|
<Compile Include="Utils.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user