Compare commits
10 Commits
65faa1b8e8
...
67fb46174a
| Author | SHA1 | Date | |
|---|---|---|---|
| 67fb46174a | |||
| d601150e4d | |||
| 2162ebad89 | |||
| 10e15f3d5c | |||
| cc370ad56b | |||
| 8b95d4b89d | |||
| 93dacbbaa6 | |||
| 92a3a312ad | |||
| 1eefc8993c | |||
| 9b6e8799b0 |
@@ -19,7 +19,11 @@ namespace WaveshareUARTFingerprintSensor.Sample
|
||||
|
||||
sensor.Start();
|
||||
|
||||
var count = sensor.GetUserCount();
|
||||
sensor.Waked += s => sensor.Wake();
|
||||
|
||||
sensor.Sleep();
|
||||
|
||||
Console.WriteLine("End");
|
||||
|
||||
Thread.Sleep(-1);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ namespace WaveshareUARTFingerprintSensor
|
||||
{
|
||||
public const string PrimarySerialPort = "/dev/ttyAMA0";
|
||||
public const string SecondarySerialPort = "/dev/ttyS0";
|
||||
public const int DefaultTimeout = 10_000;
|
||||
|
||||
public event WakedEventHandler Waked;
|
||||
public delegate void WakedEventHandler(FingerprintSensor sender);
|
||||
|
||||
public string PortName { get; }
|
||||
|
||||
@@ -62,19 +66,22 @@ namespace WaveshareUARTFingerprintSensor
|
||||
|
||||
for (int i = 1; i < 6; i++)
|
||||
{
|
||||
checksum += data[i];
|
||||
checksum ^= data[i];
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third)
|
||||
private (byte first, byte second, ResponseType responseType) SendAndReceive(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
|
||||
{
|
||||
// Command packet
|
||||
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
// Set timeout
|
||||
_serialPort.WriteTimeout = timeout;
|
||||
_serialPort.ReadTimeout = timeout;
|
||||
|
||||
// Checksum
|
||||
buffer[6] = ComputeChecksum(buffer);
|
||||
@@ -98,47 +105,66 @@ namespace WaveshareUARTFingerprintSensor
|
||||
return (buffer[2], buffer[3], (ResponseType)buffer[4]);
|
||||
}
|
||||
|
||||
/*
|
||||
private void Send(CommandType commandType, byte first, byte second, byte third)
|
||||
private (byte first, byte second, byte third) SendAndReceiveRaw(CommandType commandType, byte first, byte second, byte third, int timeout = DefaultTimeout)
|
||||
{
|
||||
// Command packet
|
||||
byte[] buffer = { PacketSeparator, (byte)commandType, first, second, third, 0, 0, PacketSeparator };
|
||||
(byte f, byte s, ResponseType response) = SendAndReceive(commandType, first, second, third, timeout);
|
||||
|
||||
// Checksum
|
||||
buffer[6] = ComputeChecksum(buffer);
|
||||
|
||||
_serialPort.Write(buffer, 0, buffer.Length);
|
||||
return (f, s, (byte)response);
|
||||
}
|
||||
|
||||
private (byte first, byte second, byte third) Receive(CommandType commandType)
|
||||
private bool TrySendAndReceive(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, ResponseType responseType) response, int timeout = DefaultTimeout)
|
||||
{
|
||||
// Response buffer
|
||||
var buffer = new byte[8];
|
||||
|
||||
// Response
|
||||
_serialPort.Read(buffer, 0, buffer.Length);
|
||||
|
||||
if (buffer[0] != PacketSeparator || buffer[7] != PacketSeparator || buffer[1] != (byte)commandType)
|
||||
try
|
||||
{
|
||||
throw new InvalidDataException("Invalid response from the sensor");
|
||||
response = SendAndReceive(commandType, first, second, third, timeout);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (buffer[6] != ComputeChecksum(buffer))
|
||||
{
|
||||
throw new InvalidDataException("Invalid checksum");
|
||||
}
|
||||
|
||||
return (buffer[2], buffer[3], buffer[4]);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TrySendAndReceiveRaw(CommandType commandType, byte first, byte second, byte third, out (byte first, byte second, byte third) response, int timeout = DefaultTimeout)
|
||||
{
|
||||
try
|
||||
{
|
||||
response = SendAndReceiveRaw(commandType, first, second, third, timeout);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public uint QuerySerialNumber()
|
||||
{
|
||||
(byte first, byte second, byte third) = SendAndReceiveRaw(CommandType.QuerySerialNumber, 0, 0, 0);
|
||||
|
||||
return Utils.Merge(first, second, third);
|
||||
}
|
||||
*/
|
||||
|
||||
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, 1000))
|
||||
{
|
||||
(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()
|
||||
@@ -146,11 +172,16 @@ namespace WaveshareUARTFingerprintSensor
|
||||
_rstPin.Write(GpioPinValue.Low);
|
||||
}
|
||||
|
||||
public void Wake()
|
||||
{
|
||||
_rstPin.Write(GpioPinValue.High);
|
||||
}
|
||||
|
||||
private void OnWake()
|
||||
{
|
||||
if (_wakePin.Read())
|
||||
{
|
||||
Console.WriteLine("Sensor WAKE signal received");
|
||||
Waked?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ namespace WaveshareUARTFingerprintSensor
|
||||
public static class Utils
|
||||
{
|
||||
public static ushort Merge(byte high, byte low) => (ushort)(high << 8 | low);
|
||||
public static uint Merge(byte first, byte second, byte third) => (uint)(first << 16 | second << 8 | third);
|
||||
|
||||
public static (byte high, byte low) Split(ushort value) => ((byte)(value >> 8), (byte)(value & 0xFF));
|
||||
public static (byte first, byte second, byte third) Split(uint value) => ((byte)(value >> 16 & 0xFF), (byte)(value >> 8 & 0xFF), (byte)(value & 0xFF));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user