I am new at developing softwares for Windows IOT. I have information about .Net 3.5 and 4. When I start to develop for Win IOT, I saw that lots of things have changed. There are a lot of new words async, await, Task etc.
Now I want to read and write data from bluetooth. I can do it but if I try to write and read data in a infinite loop it throws exceptions.
I have 2 functions
Read :
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 = "";
}
}
Write :
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!";
}
}
Function that use read and write.
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 = "";
}
I call function above in a infinite loop with 200 ms delays (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();
}
}
After loop starts, it gets data true and fine, but a few loop later it throws exceptions.
If I create datawriter object in write function it throws unhandled exception and stops application. I solve this by creating this object only one time after connection. Now I get new exception.
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
At this line I get ObjectDisposedException, I observed the objects but I could not see anything disposed.
I am connecting to ELM327 bluetooth device with rfcomm protocol. I am newbie at developing for Win IOT.
Also I could not use bindings with async functions.
Please, could you help me?