2017-03-14 44 views
0

當我嘗試通過串行(usb)將我的覆盆子pi連接到我的Arduino Uno時,遇到了一個非常奇怪的問題。SerialDevice.FromIdAsync()返回null

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

總是返回null。 我嘗試了很多東西,直到我把它放在一個循環中,然後第二次運行。所以我刪除了循環並使其運行了兩次。

這是我的輸出

begintest 
testrange 
\\?\USB#VID_2341&PID_0001#55639313633351210252#{86e0d1e0-8089-11d0-9ce4-08003e301f73} 
test1 
null 
begintest 
ok 
ok2 
debugtest2 
gelukt 
Opened device for communication. 
test 
test2 

這裏是我的代碼

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

namespace BackgroundApplication2 
{ 
    public sealed class StartupTask : IBackgroundTask 
    { 
     private SerialDevice serialPort = null; 
     public void Run(IBackgroundTaskInstance taskInstance) 
     { 
      FindDevice(); 
      Debug.WriteLine("test1"); 

      if (serialPort == null) 
      { 
       Debug.WriteLine("null"); 
      } 
      FindDevice(); 
     } 


     private async void FindDevice() 
     { 
      Debug.WriteLine("begintest"); 
      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; 
      } 

      try 
      { 
       Debug.WriteLine("testrange"); 
       Debug.WriteLine(myDevices[0].Id); 
       serialPort = await SerialDevice.FromIdAsync(myDevices[0].Id); 
       if (serialPort == null) 
       { 
        Debug.WriteLine("null2"); 
        return; 
       } 
       Debug.WriteLine("ok"); 
       serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); 
       serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); 
       serialPort.BaudRate = 9600; 
       serialPort.Parity = SerialParity.None; 
       serialPort.StopBits = SerialStopBitCount.One; 
       serialPort.DataBits = 8; 
       serialPort.Handshake = SerialHandshake.None; 
       Debug.WriteLine("ok2"); 
       /*String debugtest = "Serial port configured successfully: "; 
       debugtest += serialPort.BaudRate + "-"; 
       debugtest += serialPort.DataBits + "-"; 
       debugtest += serialPort.Parity.ToString() + "-"; 
       debugtest += serialPort.StopBits; 
       debugtest += (DeviceInformation)myDevices[0]; 

       Debug.WriteLine("debugtest1"); 
       Debug.WriteLine(debugtest);*/ 
       Debug.WriteLine("debugtest2"); 
       Listen(); 
      } 
      catch (Exception exception) 
      { 
       Debug.WriteLine(exception.Message.ToString()); 
       Debug.WriteLine("error"); 
      } 
      finally 
      { 
       Debug.WriteLine("Opened device for communication."); 
      } 
      Debug.WriteLine("test2"); 
     } 
     private async void Listen() 
     { 
      Debug.WriteLine("gelukt"); 
     } 
    } 
} 

出於某種原因,這部分使得它卡住了。它只是停在那裏......

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

Debug.WriteLine("debugtest1"); 
Debug.WriteLine(debugtest); 

這是輸出:

begintest 
testrange 
\\?\USB#VID_2341&PID_0001#55639313633351210252#{86e0d1e0-8089-11d0-9ce4-08003e301f73} 
test1 
null 
begintest 
ok 
ok2 
The thread 0x508 has exited with code 0 (0x0). 
The program '[2308] serialsample.exe' has exited with code -1 (0xffffffff). 

我的最後一個問題,爲什麼它會自動停止運行?我總是調試退出與此(或代碼-1如上所示):

The program '[240] backgroundTaskHost.exe' has exited with code 1 (0x1). 

很抱歉,如果有在這裏和那裏荷蘭詞在我的代碼。

+0

@ScottChamberlain這是我第一次編碼這樣的事情所以大部分是複製過去所做的。我真的不知道爲什麼這是錯的,但我會谷歌,謝謝! :) – Principis

+0

把我的評論轉換爲完整的答案,顯示你在哪裏做錯了。 –

回答

0

您錯誤地使用了IBackgroundTask,您必須在完成後註冊您的知識和通知。這是通過改變你的async void功能async task,使Run一個async void

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

namespace BackgroundApplication2 
{ 
    public sealed class StartupTask : IBackgroundTask 
    { 
     private SerialDevice serialPort = null; 
     public void Run(IBackgroundTaskInstance taskInstance) 
     { 
      //This tells IBackgroundTask you will be doing some extra work in the background and it should not shut down. 
      var deferral = taskInstance.GetDeferral(); 
      try 
      { 
       await FindDevice(); 
       Debug.WriteLine("test1"); 

       if (serialPort == null) 
       { 
        Debug.WriteLine("null"); 
       } 
       await FindDevice(); 
      } 
      finally 
      { 
       //This tells IBackgroundTask that you are done with the last await. 
       deferral.Complete(); 
      } 
     } 


     private async Task FindDevice() 
     { 
      Debug.WriteLine("begintest"); 
      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; 
      } 

      try 
      { 
       Debug.WriteLine("testrange"); 
       Debug.WriteLine(myDevices[0].Id); 
       serialPort = await SerialDevice.FromIdAsync(myDevices[0].Id); 
       if (serialPort == null) 
       { 
        Debug.WriteLine("null2"); 
        return; 
       } 
       Debug.WriteLine("ok"); 
       serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); 
       serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); 
       serialPort.BaudRate = 9600; 
       serialPort.Parity = SerialParity.None; 
       serialPort.StopBits = SerialStopBitCount.One; 
       serialPort.DataBits = 8; 
       serialPort.Handshake = SerialHandshake.None; 
       Debug.WriteLine("ok2"); 
       /*String debugtest = "Serial port configured successfully: "; 
       debugtest += serialPort.BaudRate + "-"; 
       debugtest += serialPort.DataBits + "-"; 
       debugtest += serialPort.Parity.ToString() + "-"; 
       debugtest += serialPort.StopBits; 
       debugtest += (DeviceInformation)myDevices[0]; 

       Debug.WriteLine("debugtest1"); 
       Debug.WriteLine(debugtest);*/ 
       Debug.WriteLine("debugtest2"); 
       await Listen(); 
      } 
      catch (Exception exception) 
      { 
       Debug.WriteLine(exception.Message.ToString()); 
       Debug.WriteLine("error"); 
      } 
      finally 
      { 
       Debug.WriteLine("Opened device for communication."); 
      } 
      Debug.WriteLine("test2"); 
     } 
     private async Task Listen() 
     { 
      Debug.WriteLine("gelukt"); 
     } 
    } 
} 
+0

謝謝!我剛剛讀到:「有一個關鍵點需要注意:默認情況下,應用程序將在運行方法完成時關閉。」感謝您的回答! – Principis