【发布时间】:2016-09-09 21:27:36
【问题描述】:
我是为 Windows IOT 开发软件的新手。我有关于 .Net 3.5 和 4 的信息。当我开始为 Win IOT 开发时,我发现很多事情都发生了变化。生词async、await、Task等很多。
现在我想从蓝牙读写数据。我可以做到,但如果我尝试在无限循环中写入和读取数据,则会引发异常。
我有两个功能
阅读:
private async Task ReadAsync(CancellationToken cancellationToken)
{
uint ReadBufferLength = 1024;
Task<UInt32> loadAsyncTask;
// If task cancellation was requested, comply
cancellationToken.ThrowIfCancellationRequested();
// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
// Create a task object to wait for data on the serialPort.InputStream
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
// Launch the task and wait
UInt32 bytesRead = await loadAsyncTask;
if (bytesRead > 0)
{
receivedData = dataReaderObject.ReadString(bytesRead);
}
else
{
receivedData = "";
}
}
写:
private async Task SendData(string data)
{
if (deviceService != null)
{
//send data
if (string.IsNullOrEmpty(data))
{
uiTxtError.Text = "Please specify the string you are going to send";
}
else
{
//DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
UInt32 len = dwriter.MeasureString(data);
dwriter.WriteUInt32(len);
dwriter.WriteString(data);
await dwriter.StoreAsync();
await dwriter.FlushAsync();
}
}
else
{
uiTxtError.Text = "Bluetooth is not connected correctly!";
}
}
使用读写的函数。
await SendData("010C" + Environment.NewLine);
await ReadAsync(ReadCancellationTokenSource.Token);
if (!string.IsNullOrEmpty(receivedData))
{
string[] splitted = receivedData.Replace("\r", "").Replace(">", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int a = 0;
int b = 0;
if (int.TryParse(splitted[splitted.Length - 1], System.Globalization.NumberStyles.HexNumber, null as IFormatProvider, out b))
{
if(int.TryParse(splitted[splitted.Length - 2], System.Globalization.NumberStyles.HexNumber, null as IFormatProvider, out a))
{
uiGaugeRpm.Value = ((a * 256) + b) / 4;
}
}
receivedData = "";
}
我在具有 200 毫秒延迟的无限循环中调用上述函数 (Task.Delay(200))。
ReadCancellationTokenSource = new CancellationTokenSource();
if (streamSocket.InputStream != null)
{
dataReaderObject = new DataReader(streamSocket.InputStream);
try
{
while (true)
{
await GetRPM();
await Task.Delay(200);
}
}
catch (Exception excp)
{
MessageDialog dialog = new MessageDialog(excp.Message);
await dialog.ShowAsync();
}
}
循环开始后,它得到的数据是正确的,但在几个循环之后它会抛出异常。
如果我在写入函数中创建数据写入器对象,它会引发未处理的异常并停止应用程序。我通过在连接后仅创建一次此对象来解决此问题。现在我得到了新的例外。
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
在这一行我得到了 ObjectDisposedException,我观察了对象,但看不到任何已处理的东西。
我正在使用 rfcomm 协议连接到 ELM327 蓝牙设备。我是为 Win IOT 开发的新手。
我也不能使用带有异步函数的绑定。
拜托,你能帮帮我吗?
【问题讨论】:
-
这个例外对我来说似乎很清楚。您正在尝试使用已处置的对象。如果您需要这里的帮助,您需要提供一个好的minimal reproducible example 以可靠地重现问题。另请参阅How to Ask,了解如何以清晰、可回答的方式提出您的问题。
标签: c# raspberry-pi3 windows-10-iot-core rfcomm .net-4.5.2