シリアル(usb)経由で私のラズベリーpiを私のArduino Unoに接続しようとすると、本当に奇妙な問題が発生しました。SerialDevice.FromIdAsync()がnullを返します
serialPort = await SerialDevice.FromIdAsync(myDevices[0].Id);
常にnullを返します。 私は多くのことを試しましたが、それをループに入れてから2回目になるまでそれはできません。だから私はループを削除し、2回実行させた。
これは私の出力
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");
}
}
}
です。
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).
そして、私の最後の質問なぜそれが自動的に実行を停止ん:それはちょうどこれが出力されている...そこ
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);
を停止しますか?私のデバッグいつもこれで終了の(またはコード-1以上見られるように):
The program '[240] backgroundTaskHost.exe' has exited with code 1 (0x1).
申し訳ありませんがあちこちオランダ語は私のコードであるかどう。
を行うことによって行われます。なぜそれが間違っているのか分かりませんが、私はGoogleに感謝します! :) – Principis
は私のコメントを完全な答えに変えて、どこで間違っているのかを示しています。 –