访问Windows 10 IoT核心上的Usb端口

问题描述 投票:0回答:1

我在Minnowboard Turbot上的Windows 10 IoT核心上运行物联网边缘模块。该模块需要从USB端口读取流。我们使用的是System.Io.Ports(与我们的.net核心2.1代码兼容的.net标准版本)。

我在使用Windows 10专业版的笔记本电脑上看到了这项工作,但它不适用于Windows IoT核心版。我可以找到一些声明IoT Core不支持System.Io.Ports的消息来源,因为Usb端口的命名方案(必须将其命名为COM {x}才能使SerialPort正常工作.SerialIOPrts示例附带的自述文件来自Windows IoT Samples(https://go.microsoft.com/fwlink/?linkid=860459)说

“此示例使用标准.NET Core System.IO.Ports API来访问串行设备。这些API仅适用于名为COM {x}的串行设备。因此,此方法与Windows 10 IoT Enterprise相关,但不适用于Windows 10物联网核心。“

有没有人找到解决这个问题的方法?我可以在Windows 10 IoT Enterprise上使用它,但我真的想证明我们可以在最小的硬件/操作系统上运行这个模块。

c# module usb windows-10-iot-core
1个回答
0
投票

在Windows IoT Core上使用System.IO.Ports,您可以参考“SerialWin32 Azure IoT Edge module”。 (注意:这些说明适用于运行Windows 10 IoT Enterprise或Windows IoT Core build 17763的PC。目前仅支持x64架构。未来将推出Arm32。)

(不适用于Azure IoT Edge模块)要在Windows IoT Core上访问不带端口名称(COMx)的串行设备,您可以使用Win32 API CreateFile。有一个用于为Windows IoT Core创建Win32控制台应用程序的项目模板,您可以开始使用它。

enter image description here

在这里,我使用设备ID创建一个示例来打开一个串行设备并进行写入和读取:

#include <windows.h>
#include <iostream>
using namespace std;


int main()
{
    DWORD errCode = 0;
    LPCWSTR pDeviceId = L"\\\\?\\FTDIBUS#VID_0403+PID_6001+A50285BIA#0000#{86e0d1e0-8089-11d0-9ce4-08003e301f73}";

    HANDLE serialDeviceHdl = CreateFile(pDeviceId, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (!serialDeviceHdl)
    {
        errCode = GetLastError();
        cout << "Open device failed. Error code: " << errCode << endl;
        return 0;
    }

    DWORD writenSize = 0;
    BOOL result = WriteFile(serialDeviceHdl, "hello", 5, &writenSize, NULL);
    if (FALSE == result)
    {
        errCode = GetLastError();
        cout << "Write to device failed. Error code: " << errCode << endl;
    }

    CHAR readBuf[5];
    DWORD readSize = 0;
    result = ReadFile(serialDeviceHdl, readBuf, 5, &readSize, NULL);
    if (FALSE == result)
    {
        errCode = GetLastError();
        cout << "Read from device failed. Error code: " << errCode << endl;
    }
}

您可以通过官方样本“SerialUART” - “DeviceInformation.Id”找到设备ID。

如何在Windows IoT Core上部署和调试win32 C ++控制台,您可以参考here

© www.soinside.com 2019 - 2024. All rights reserved.