简体   繁体   English

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

[英]No overload for method 'GetDeviceSelector' takes 2 arguments

I'm a complete beginner at c# and keep getting this error "CS1501 No overload for method 'GetDeviceSelector' takes 2 arguments" 我是c#的一个完整的初学者,并不断收到此错误“ CS1501方法'GetDeviceSelector'的重载没有2个参数”

Visual studio suggest to change SerialDevice.GetDeviceSelector(vid, pid) to SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid) . Visual Studio建议将SerialDevice.GetDeviceSelector(vid, pid)更改为SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid)

This works but when I run the debug on my Raspberry pi 3 on Windows 10 IoT Core it returns: Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index 这可以工作,但是当我在Windows 10 IoT核心版上的Raspberry pi 3上运行调试时,它返回: Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index

My goal is to connect my Arduino to my Raspberry pi and communicate with it through serial. 我的目标是将Arduino连接到Raspberry pi并通过串行与之通信。 (I don't want to use the Microsoft maker stuff) (我不想使用微软制造商的东西)

Here's the full code: 这是完整的代码:

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();
    }*/
  }
}

edit: I changed some lines so that it works 编辑:我更改了一些行,以便工作

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;
        }

But I found a new problem... This serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id); 但是我发现了一个新问题...这个serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id); returns null 返回null

The capabilities are correct (when I compare it to Microsoft's sample) 功能是正确的(当我将其与Microsoft的示例进行比较时)

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

I changed the ushort to UInt16 which solved the System.ArgumentOutOfRangeException error. 我将ushort更改为UInt16,从而解决了System.ArgumentOutOfRangeException错误。

The solution for that 解决方案

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

returned null 返回null

is changing the variable serialDevice to serialPort for some annoying reason... 由于某些令人讨厌的原因将变量serialDevice更改为serialPort ...

Edit: It worked once, it's broken again.... any ideas? 编辑:它曾经工作过,又被打破了...。有什么想法吗?

Edit 2: Someone gave me the solution 编辑2:有人给了我解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM