2017-03-08 10 views
0

내가 C#을에서 완전한 초보자있어이 오류가 계속지지 않습니다는방법 'GetDeviceSelector'에 대한 과부하가 2 개 인자

비주얼 스튜디오에 SerialDevice.GetDeviceSelector(vid, pid)을 변경하는 것이 좋습니다 "CS1501는 방법에 대한 과부하는 'GetDeviceSelector은'이 개 인수를 사용하지 않습니다" SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid).

이 작동하지만 윈도우 10에서 내 라즈베리 파이 3 디버그를 실행할 때의 IoT 핵심은 반환 Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.ni.dll Specified argument was out of the range of valid values. Parameter name: index

내 목표는 내 라즈베리 파이에 내 아두 이노를 연결하고 시리얼을 통해 통신하는 것입니다.

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); 을 반환합니다.

capabi (I 마이크로 소프트의 샘플과 비교시) lities 내가 System.ArgumentOutOfRangeException이 오류를 해결 UINT16에 USHORT을 변경

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

+0

당신이 줄에서 예외가 점점 나에게 준'serialDevice =이 SerialDevice.FromIdAsync (myDevices [0] .ID) 기다리고,'? 장치 배열이 비어있는 것처럼 보입니다. –

+0

그래, 방금 그걸 발견 했어. UInt32는 Microsoft의 샘플에서 UInt32이지만 UInt32로 변경해야했다. https://docs.microsoft.com/en-us/uwp/api/windows.devices .serialcommunication.serialdevice # Windows_Devices_SerialCommunication_SerialDevice_FromIdAsync_System_String_ – Principis

+0

문제가 해결되었지만 새로운 문제가 발견되었습니다 ...이 줄 : serialDevice = 기다리고 SerialDevice.FromIdAsync (myDevices [0] .Id); 항상 null입니다. Package.appxmanifest를 여러 번 확인했으며 작동하는 serialUART 샘플과 동일합니다 ... – Principis

답변

0

정확합니다. 그것은 한 번 일 :

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

에 대한 해결책은

편집 ...

몇 가지 짜증나는 이유하여 SerialPort에를 변수 serialDevice을 변화 null을 반환 , 다시 깨 졌어 .... 어떤 생각이야?

편집 2 : 누군가가 solution