方法 'GetDeviceSelector' 没有重载需要 2 个参数

No overload for method 'GetDeviceSelector' takes 2 arguments

我是 c# 的完全初学者,并且不断收到此错误 "CS1501 No overload for method 'GetDeviceSelector' takes 2 arguments"

Visual studio建议将SerialDevice.GetDeviceSelector(vid, pid)改为SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid)

这有效,但是当我 运行 在 Windows 10 IoT Core 上对 Raspberry pi 3 进行调试时 returns: Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index

我的目标是将我的 Arduino 连接到我的 Raspberry pi 并通过串口与其通信。 (我不想使用微软制造商的东西)

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration;
using System.Diagnostics;

// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409

namespace BackgroundApplication1
{
    public sealed class StartupTask : IBackgroundTask
    {
        //private String debugtest;
        private SerialDevice serialDevice = null;



    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // 
        // TODO: Insert code to perform background work
        //
        // If you start any asynchronous methods here, prevent the task
        // from closing prematurely by using BackgroundTaskDeferral as
        // described in http://aka.ms/backgroundtaskdeferral
        //
        FindDevice();
        Debug.WriteLine("test1");
    }

    /*private async void ListAvailablePorts()
    {
        UInt32 vid = 0x045E;
        UInt32 pid = 0x0611;
        try
        {
            string aqs = SerialDevice.GetDeviceSelector(vid, pid);
            var dis = await DeviceInformation.FindAllAsync(aqs);

            //status.Text = "Select a device and connect";

            for (int i = 0; i < dis.Count; i++)
            {
                listOfDevices.Add(dis[i]);
            }

            DeviceListSource.Source = listOfDevices;
            comPortInput.IsEnabled = true;
            ConnectDevices.SelectedIndex = -1;
        }
        catch (Exception ex)
        {
            status.Text = ex.Message;
        }
    }*/

    private async void FindDevice()
    {
        Debug.WriteLine("begintest");
        ushort vid = 2341;
        ushort pid = 0001;

        string aqs = SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);

        var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs);

        try
        {
            serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id);

            Debug.WriteLine("ok");

            /*serialDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            serialDevice.BaudRate = 9600;
            serialDevice.Parity = SerialParity.None;
            serialDevice.StopBits = SerialStopBitCount.One;
            serialDevice.DataBits = 8;
            serialDevice.Handshake = SerialHandshake.None;

            String debugtest = "Serial port configured successfully: ";
            debugtest += serialDevice.BaudRate + "-";
            debugtest += serialDevice.DataBits + "-";
            debugtest += serialDevice.Parity.ToString() + "-";
            debugtest += serialDevice.StopBits;
            //debugtest += (DeviceInformation)myDevices[0];

            Debug.WriteLine(debugtest);*/
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception.Message.ToString());
            Debug.WriteLine("error");
        }
        finally        
        {
            Debug.WriteLine("Opened device for communication.");             
            Debug.WriteLine("test");
        }
        Debug.WriteLine("test2");
    }

    /*private void ShowStatus(string v)
    {
        throw new NotImplementedException();
    }*/
  }
}

编辑:我更改了一些行以使其有效

UInt16 vid = 0x2341;
        UInt16 pid = 0x0001;

        string aqs = SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);

        var myDevices = await DeviceInformation.FindAllAsync(aqs);

        if (myDevices.Count == 0)
        {
            Debug.WriteLine("Device not found!");
            return;
        }

但是我发现了一个新问题... 这个serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id); returns空

功能正确(当我将它与 Microsoft 的示例进行比较时)

<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort"/>
  </Device>
</DeviceCapability>

我将 ushort 更改为 UInt16,这解决了 System.ArgumentOutOfRangeException 错误。

解决方案

serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id); 

返回空值

出于某些烦人的原因正在将变量 serialDevice 更改为 serialPort...

编辑:它曾经工作过,但又坏了....有什么想法吗?

编辑 2: 有人给了我