我在C#中的初學者和不斷收到此錯誤「CS1501沒有重載方法‘GetDeviceSelector’需要兩個參數」無重載方法「GetDeviceSelector」需要兩個參數
Visual Studio中建議修改SerialDevice.GetDeviceSelector(vid, pid)
到SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid)
。
這個工作,但是當我在Windows 10上我的樹莓派3運行調試物聯網核心,它返回: Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index
我的目標是我的Arduino連接到我的覆盆子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;
}
但我發現了一個新問題... This serialDevice = await SerialDevice.FromIdAsync(myDevices[0].Id);
returns null
Capabi lities是正確的(當我把它比作微軟的樣品)
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort"/>
</Device>
</DeviceCapability>
你在得到線異常'serialDevice =等待SerialDevice.FromIdAsync(myDevices [0] .ID);'?看起來你正在得到一個空的設備陣列。 –
是的,我剛剛發現... 我不得不將UInt32更改爲UInt16,雖然它是微軟示例中的UInt32 ... https://docs.microsoft.com/en-us/uwp/api/windows.devices .serialcommunication.serialdevice#Windows_Devices_SerialCommunication_SerialDevice_FromIdAsync_System_String_ – Principis
問題已修復,但我發現了一個新問題...此行爲: serialDevice = await SerialDevice.FromIdAsync(myDevices [0] .Id); 返回始終爲空我多次檢查了Package.appxmanifest,它與serialUART示例的工作方式相同。 – Principis