我目前正在开发一个程序,其中Raspberry Pi 3将每隔100ms读取4个Arduino(USB)设备通过串行通信发送的内容。 UI 在 Concurrency::create_task 的第四次调用 (serialDeviceIndex = 3) 上卡住,但如果只有 3 个 Arduino 设备,则问题不会发生,线程执行继续到 .then lambda 主体。你能帮我找出问题所在吗?这是当connect
按下 xaml 设备中的按钮。
void MainPage::comPortInput_Click(Object^ sender, RoutedEventArgs^ e)
{
m_timer->Start();
auto selectionIndex = ConnectDevices->SelectedIndex;
if (selectionIndex < 0)
{
status->Text = "Select a device and connect";
return;
}
auto selectedItems = ConnectDevices->SelectedItems;
for (unsigned serialDeviceIndex = 0; serialDeviceIndex < selectedItems->Size; serialDeviceIndex++)
{
Device ^device = static_cast<Device ^>(selectedItems->GetAt(serialDeviceIndex));
textBox->Text = textBox->Text + "\n<START>:" + device->Id->ToString() + "<END>";
Windows::Devices::SerialCommunication::SerialDevice ^serial_device;
Windows::Devices::Enumeration::DeviceInformation ^entry = device->DeviceInfo;
auto childTokenSource = Concurrency::cancellation_token_source::create_linked_source(cancellationTokenSource->get_token());
auto childToken = childTokenSource.get_token();
Concurrency::create_task(
Windows::Devices::SerialCommunication::SerialDevice::FromIdAsync(entry->Id), childToken)
.then([this, serialDeviceIndex](Windows::Devices::SerialCommunication::SerialDevice ^serial_device)
{
_serialPort = serial_device;
Platform::String ^deviceString1 = _serialPort->ToString();
// Disable the 'Connect' button
//comPortInput->IsEnabled = false;
Windows::Foundation::TimeSpan _timeOut;
_timeOut.Duration = 100000L; //100000 works with delay(200)
// Configure serial settings
_serialPort->WriteTimeout = _timeOut;
_serialPort->ReadTimeout = _timeOut;
_serialPort->BaudRate = 9600;
_serialPort->Parity = Windows::Devices::SerialCommunication::SerialParity::None;
_serialPort->StopBits = Windows::Devices::SerialCommunication::SerialStopBitCount::One;
_serialPort->DataBits = 8;
_serialPort->Handshake = Windows::Devices::SerialCommunication::SerialHandshake::None;
_dataReaderObject = ref new Windows::Storage::Streams::DataReader(_serialPort->InputStream);
_dataReaderObject->InputStreamOptions = Windows::Storage::Streams::InputStreamOptions::Partial;
_dataReaderObjects.Append(_dataReaderObject);
_serialPorts.Append(_serialPort);
int serialPortsSize = _serialPorts.Size;
Listen(_serialPorts.GetAt(serialDeviceIndex), _dataReaderObject, serialDeviceIndex);
});
Sleep(5);
}
}
EDIT:
好的,我现在将代码简化为这个
for (unsigned serialDeviceIndex = 0; serialDeviceIndex < selectedItems->Size; serialDeviceIndex++){
Device ^device = static_cast<Device ^>(selectedItems->GetAt(serialDeviceIndex));
textBox->Text = textBox->Text + "\n<START>:" + device->Id->ToString() + "<END>";
Windows::Devices::Enumeration::DeviceInformation ^entry = device->DeviceInfo;
auto it = Concurrency::create_task(Windows::Devices::SerialCommunication::SerialDevice::FromIdAsync(entry->Id));
while (!it.is_done())
textBoxStatus->Text = "processing" + deviceIndex.ToString();
it.get();
}
仅供参考,这段代码在我的电脑上运行正常。没有错误。当我尝试在 Windows IoT 下的 RaspberryPi3 中运行此程序时,我第四次尝试使用该设备时程序挂起it.get()
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)