Код: Выделить всё
private async Task Read(byte[] buffer, int offset, int length)
{
Console.WriteLine("Reading response to buffer of size {0}, offset {1}, max to cnt {2}", buffer.Length, offset, length);
int totalBytesRead = 0;
Console.WriteLine("Begin reading...");
while (true)
{
Console.WriteLine("Re-entering read");
int bytesRead = 0;
using (CancellationTokenSource cts = new CancellationTokenSource(new TimeSpan(0, 0, 0, 0, 333))) // 333 ms
{
Console.WriteLine("Waiting for read to complete...");
int tmpOffset = offset + totalBytesRead;
int tmpLength = length - totalBytesRead;
Console.WriteLine("tmpOffset: {0}, tmpLength: {1}", tmpOffset, tmpLength);
try
{
bytesRead = await mPort.BaseStream.ReadAsync(buffer, tmpOffset, tmpLength, cts.Token);
}
catch (Exception e)
{
Console.WriteLine("Exception during read: {0}, stack trace: {1}", e.Message, e.StackTrace);
break;
}
Console.WriteLine("Read completed");
}
totalBytesRead += bytesRead;
Console.WriteLine("Bytes read: {0}, totalBytesRead: {1}, buffer: {2}", bytesRead, totalBytesRead, BitConverter.ToString(buffer, 0, buffer.Length));
if (bytesRead == 0)
{
break;
}
}
Console.WriteLine("Read finished");
return totalBytesRead;
}
Код: Выделить всё
Reading response to buffer of size 256, offset 0, max to cnt 256
Begin reading...
Re-entering read
Waiting for read to complete...
tmpOffset: 0, tmpLength: 256
Read completed
Bytes read: 1, totalBytesRead: 1, buffer: 52-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
Re-entering read
Waiting for read to complete...
tmpOffset: 1, tmpLength: 255
Read completed
Bytes read: 8, totalBytesRead: 9, buffer: 52-45-4A-30-30-36-4A-44-52-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
Re-entering read
Waiting for read to complete...
tmpOffset: 9, tmpLength: 247
Я новичок в C#, поэтому, возможно, я чего-то не совсем понимаю. Видите ли вы причину, по которой задача здесь не отменяется?
Я попытался воспроизвести и скорректировать подход, представленный в этой статье.