我是c#的完全初学者,并且不断收到此错误" CS1501方法' GetDeviceSelector'需要2个参数"
Visual Studio建议将SerialDevice.GetDeviceSelector(vid, pid)
更改为SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid)
。
这有效,但是当我在Windows 10 IoT Core上的Raspberry pi 3上运行调试时,它返回:
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);
返回null
功能正确(当我将其与Microsoft的样本进行比较时)
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
答案 0 :(得分:0)
我将ushort更改为UInt16,解决了System.ArgumentOutOfRangeException错误。
该解决方案
serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id);
返回null
正在将变量 serialDevice 更改为 serialPort ,这是出于某种恼人的原因......
编辑:它曾经工作过一次,它再次被打破......任何想法?
编辑2:有人给了我solution