Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlushSocket before reading input #36

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Comment on lines +91 to +95
Copy link
Collaborator

@tluijken tluijken Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat als we de buffer definieren vlak voor gebruik. Zeg, op regel 105? Krijg je dan niet hetzelfde effect? Dan kan de hele flush methode eruit. Voelt nu alsof we 2 keer hetzelfde doen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goed punt, ik heb het verwerkt en opnieuw getest. Het lijkt erop dat het toch niet helemaal hetzelfde is...

+> info: [PDMS.Dispatcher.Tests.Mocks.SickRfidScannerMock] Sending response :\x02sAN MIStartIn\x03
+> info: [PDMS.Dispatcher.Tests.Mocks.SickRfidScannerMock] Sending response :\x02sAN MIStopIn\x03
+> info: [PDMS.Dispatcher.Tests.Mocks.SickRfidScannerMock] Sending RFID 91456789415679
+> warn: [PDMS.Dispatcher.Core.Implementation.BinService] Could not find bin in repository for bin tag 914567894156789145678941567891456789415678914567

Ik heb ook de code met de flush methode nog even gerund als sanity check, en die werkt wel. (Met deze test "Dispatch VS22-0009 plates are all in 3 different trays, but one tray has all the plates" )

🤔

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
Loading