Compare commits

..

10 Commits

Author SHA1 Message Date
67fb46174a Fix Checksum 2020-11-24 17:07:13 +01:00
d601150e4d Update Waked delegate 2020-11-24 16:33:21 +01:00
2162ebad89 Add Waked event and Wake command 2020-11-24 16:30:45 +01:00
10e15f3d5c Add QuerySerialNumber command 2020-11-24 16:29:46 +01:00
cc370ad56b Update Utils 2020-11-24 16:28:54 +01:00
8b95d4b89d Add SendAndReceiveRaw 2020-11-24 15:53:29 +01:00
93dacbbaa6 Update TryGetUserCount
Change default timeout in TryGetUserCount
2020-11-24 15:42:11 +01:00
92a3a312ad Remove unused methods 2020-11-24 15:36:05 +01:00
1eefc8993c Add TrySendAndReceive 2020-11-24 15:34:50 +01:00
9b6e8799b0 Add timeout 2020-11-24 15:34:20 +01:00
3 changed files with 68 additions and 31 deletions

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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));
}
}