并行编程卡在串行设备的 Concurrency::create_task 调用的第四个实例上

2023-12-29

我目前正在开发一个程序,其中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(使用前将#替换为@)

并行编程卡在串行设备的 Concurrency::create_task 调用的第四个实例上 的相关文章

  • 如何释放 C++ WinRT 值结构的内存

    我是否必须以及如何从已返回到托管 C 项目的 Windows 运行时组件中创建的值结构中释放内存 我声明了该结构 Custom struct public value struct PlayerData Platform String Na
  • 如何忽略异常并完成尝试

    所以我一直在奋斗这个问题 https stackoverflow com questions 14885288 io exception error when using serialport open大约一周了 我想我知道这个问题 但我不
  • 推荐一款便宜的串口设备

    我需要将我编写的 Java 应用程序与使用串行端口进行通信的医疗设备集成 该设备相当昂贵 而且也不那么便携 我希望能够随时随地测试代码 但在需要插入串行设备并开始测试之前 您只能做这么多 因此 我正在寻找一种使用串行端口进行通信的廉价便携式
  • 终止导致设备或资源繁忙的进程:“/dev/ttyUSB0”?

    我使用以下 Python 代码连接到我的 Arduino 板 device glob glob dev ttyUSB 0 time sleep 1 arduino serial Serial device 115200 timeout 5
  • Linux下与CashCode纸币接受器通信

    我已经实现了 CCNET 协议 以便通过 Linux 上的软件与 CashCode 纸币接受器进行通信 最初 我花了很多时间试图弄清楚为什么设备不响应 我发送的命令 使用反复试验的方法 我找到了为串行端口设置以下选项的解决方案 stty F
  • 以串行对象作为参数的多进程

    我在使用 Python 并将串行对象作为参数传递给单独的进程时遇到问题 该程序在 Windows 8 中运行 因此不能选择使用全局变量 from multiprocessing import Queue from multiprocessi
  • C#读取Arduino

    我正在尝试制作一个从 Arduino 读取传出信号的应用程序 但我无法使其在 C 中工作Windows 窗体 http en wikipedia org wiki Windows Forms 仅在控制台中 我的 C Windows 窗体代码
  • 使用 awk 读取串行输入,插入日期

    我正在尝试重新格式化串行输入 该输入由两个用逗号分隔的整数组成 从 Arduino 发送 1 2 3 4 0 0 0 1 我想在每行后面附加日期 用制表符分隔所有内容 到目前为止 这是我的代码 cat dev cu usbmodem3d11
  • 读取串行数据而不需要高 CPU 使用率

    我想在 Linux 下用简单的 C 或 C 程序读取通过 FTDI 串行 接口从 Arduino 发送的消息 Arduino 发送两个字符的 标头 一个命令字节 后跟几个字节的数据 具体取决于命令 我的第一次尝试是简单地使用 open 和
  • Linux环境下串口数据转换为TCP/IP

    我需要从Linux系统的串口获取数据并将其转换为TCP IP发送到服务器 这很难做到吗 我有一些基本的编程经验 但对 Linux 的经验不多 有没有开源应用程序可以做到这一点 在 Linux 中您不需要编写程序来执行此操作 只是pipe h
  • 如何使 Raspberry Pi 3 可被 iOS 和 CoreBluetooth 发现?

    我有一个 Raspberry Pi 3 我正在尝试使其可供 iOS 设备发现 我跑了sudo hciconfig hci0 piscan让它被发现 我可以用我的 iMac 看到它 在 iOS 上 我使用 CoreBluetooth 并扫描附
  • 在命令行上编译 C++/CX

    我收到链接器错误fatal error C1107 could not find assembly platform winmd please specify the assembly search path using AI or by
  • C# SerialPort BaseStream ReadAsync - CancellationToken 从未取消?

    我尝试以异步方式从串行端口读取数据 请记住操作所花费的时间不得超过指定的时间段 我使用的代码 private async Task
  • Node exec 无权执行脚本

    直到最近 它都运行良好 但是当我今天尝试使用它时 它无法正常运行 它返回以下错误 错误 命令失败 bin sh c home pi RPi Computer Power RPi Server routes scripts hash js 1
  • 如何在 Mac OS X 10.9.5 上以编程方式读取低功耗蓝牙传输的数据?

    我正在尝试阅读蓝牙低功耗 http www bluetooth com Pages low energy tech info aspx使用 Ruby 以编程方式传输数据 低功耗蓝牙技术不支持标准规范 v4 0 中的串行端口配置文件 SPP
  • 通过串行端口通过诺基亚手机发送短信

    我正在尝试通过诺基亚手机通过串口发送短信 这通过腻子很容易 命令来自诺基亚文档 http wiki forum nokia com index php Using AT commands to send and read SMS工作正常 然
  • 如何在 Arduino 中将 char 变量作为数字打印到串行端口?

    我正在使用 Arduino Uno 我正在从 EEPROM 读取一个字节值并将该值存储在类型变量中char 1 字节 我想将变量的值作为数字 而不是相应的 ASCII 代码 打印到串行监视器 例如考虑char val 5 我想查看串行监视器
  • GSM 调制解调器 USSD 检查余额时出现 CME 错误 100

    我正在尝试使用华为 E1550 3G 调制解调器通过 USSD 检查 SIM 卡余额 但当我总是发送 AT CUSD 1 100 15 I Get CME Error 100 是的 我的调制解调器支持 USSD 因为我有一个调制解调器的仪表
  • 使用 python 进行串行数据记录

    Intro 我需要编写一个小程序来实时读取串行数据并将其写入文本文件 我在读取数据方面取得了一些进展 但尚未成功地将这些信息存储在新文件中 这是我的代码 from future import print function import se
  • 在 C++ linux 中将 STRINGS 写入串口

    我知道这个问题遍布互联网 但仍然没有任何东西能让我完全解决这个问题 我想用 C linux 将数据写入 Propeller 板的串行端口 从控制台获取输入时程序运行良好 但是当我向它写入字符串时总是返回 ERROR Invalid comm

随机推荐