mic*_*son 3 c# serial-port cancellationtokensource cancellation-token
我尝试以异步方式从串行端口读取数据,请记住操作所花费的时间不得超过指定的时间段。我使用的代码:
private async Task<int> 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;
}
Run Code Online (Sandbox Code Playgroud)
以下代码将这些消息返回到控制台:
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
Run Code Online (Sandbox Code Playgroud)
最后一条消息后它挂起。当然,应该使用比 333ms 更长的超时时间。对我来说,令牌似乎永远不会被取消,或者至少永远不会捕获这种取消。我 99% 确定设备不会发送更多数据,因此这就是我指望超时触发的原因。我是 C# 新手,所以也许有些东西我不太明白。您能看出任务没有被取消的原因吗?
我尝试复制和调整本文中提出的方法。
我最近遇到了同样的问题,试图通过串行线与一些旧的嵌入式仪器进行通信。我的解决方案是一组如下所示的扩展方法,我不处理整个端口,关闭基本流似乎就足够了,然后如果在取消时它是打开的,我会立即重新打开它。
public static class SerialAsyncExtensions
{
public static async Task<int> ReadSerialAsync(this SerialPort f_Serial, byte[] f_Buffer, int f_Offset, int f_Count, int f_Delay)
{
if (f_Delay > 0)
await Task.Delay(f_Delay);
return await f_Serial.BaseStream.ReadAsync(f_Buffer, f_Offset, f_Count);
}
public static async Task<int> ReadSerialAsync(this SerialPort f_Serial, byte[] f_Buffer, int f_Offset, int f_Count, int f_Delay, CancellationToken f_Token)
{
CancellationTokenRegistration l_Token_Registration = f_Token.Register((param) =>
{
SerialPort l_Serial = (SerialPort)param;
l_Serial.DiscardInBuffer();
}, f_Serial);
if (f_Delay > 0)
await Task.Delay(f_Delay, f_Token);
try
{
int l_Result = await f_Serial.BaseStream.ReadAsync(f_Buffer, f_Offset, f_Count, f_Token);
return l_Result;
}
catch(System.IO.IOException Ex)
{
throw new OperationCanceledException("ReadSerialAsync operation Cancelled.", Ex);
}
finally
{
l_Token_Registration.Dispose();
}
}
public static async Task WriteSerialAsync(this SerialPort f_Serial, byte[] f_Buffer, int f_Offset, int f_Count)
{
await f_Serial.BaseStream.WriteAsync(f_Buffer, f_Offset, f_Count);
}
public static async Task WriteSerialAsync(this SerialPort f_Serial, byte[] f_Buffer, int f_Offset, int f_Count, CancellationToken f_Token)
{
CancellationTokenRegistration l_Token_Registration = f_Token.Register((param) =>
{
SerialPort l_Serial = (SerialPort)param;
l_Serial.DiscardOutBuffer();
}, f_Serial);
try
{
await f_Serial.BaseStream.WriteAsync(f_Buffer, f_Offset, f_Count, f_Token);
}
catch(System.IO.IOException Ex)
{
throw new OperationCanceledException("WriteSerialAsync operation Cancelled.", Ex);
}
finally
{
l_Token_Registration.Dispose();
}
}
}
Run Code Online (Sandbox Code Playgroud)