Skip to content

Commit

Permalink
Add FlushSocket before reading input
Browse files Browse the repository at this point in the history
  • Loading branch information
Eline Jorritsma committed Sep 11, 2024
1 parent bc52549 commit 88b1269
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions SickRfid/ConnectedSickRfidController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;

namespace SickRfid;

Expand Down Expand Up @@ -31,6 +32,7 @@ internal ConnectedSickRfidController SetSocket(Socket socket)
{
throw new ConstraintException("Cannot assign a socket twice");
}

_socket = socket;
return this;
}
Expand Down Expand Up @@ -61,11 +63,12 @@ public async Task CloseAsync(CancellationToken cancellationToken = default)
/// Thrown when the socket is not connected.
/// </exception>
public async Task StartAsync(CancellationToken cancellationToken = default)
{
{
if (_socket is null) throw new ConstraintException("Socket is not connected");
await _socket.SendAsync(Commands.START_REQUEST_DATA, SocketFlags.None, cancellationToken).ConfigureAwait(true);
}



/// <summary>
/// Listens for a message from the Sick RFID reader.
Expand All @@ -85,31 +88,47 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
public async Task<string> ReadAsync(CancellationToken cancellationToken = default)
{
if (_socket is null) throw new ConstraintException("Socket is not connected");
var buffer = new byte[1024];

var bufferSize = 48;

FlushSocket(_socket, bufferSize);

var buffer = new byte[bufferSize];
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException("No message received within timeout");
}
}

while (_socket.Available > 0)
{
var result = await _socket.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
var result = await _socket.ReceiveAsync(buffer, SocketFlags.None, cancellationToken)
.ConfigureAwait(false);
if (result <= 0) continue;
var message = Encoding.ASCII.GetString(buffer, 0, result);


if (string.IsNullOrEmpty(message) ||
message.Equals(Acknowledgements.ACK_START, StringComparison.Ordinal) ||
if (string.IsNullOrEmpty(message) ||
message.Contains(Acknowledgements.ACK_START, StringComparison.Ordinal) ||
message.Contains(Acknowledgements.ACK_STOP, StringComparison.Ordinal)) continue;

return message;
}
}
}


private void FlushSocket(Socket socket, int bufferSize)
{
// Flush the socket before reading incoming data.
var buffer = new byte[bufferSize];
// As long as data is available, read and discard it
while (socket.Available > 0)
{
socket.Receive(buffer, SocketFlags.None);
// Discard the buffer; we don't care about its contents here
}
}


/// <summary>
/// Scans a single RFID tag, by combining the start, listen, and stop methods.
/// </summary>
Expand Down

0 comments on commit 88b1269

Please sign in to comment.