SerialDevice.FromIdAsync()返回null [英] SerialDevice.FromIdAsync() returns null

查看:159
本文介绍了SerialDevice.FromIdAsync()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试通过串行(usb)将树莓派连接到Arduino Uno时,遇到了一个非常奇怪的问题.

I encountered a really strange problem when trying to connect my raspberry pi to my Arduino Uno through serial (usb).

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

始终返回null. 我尝试了很多事情,直到我将其放入循环然后第二次起作用时,它才起作用.所以我删除了循环,使其运行了2次.

Always returns null. I tried many things and it won't until I put it in a loop and then it works the second time. So I removed the loop and made it run 2 times.

这是我的输出

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");
        }
    }
}

由于某种原因,这部分使其卡住.它就在那...

For some reason this part makes it stuck. It just stops there...

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,如上所示):

And my final question why does it automatically stop running? My debugging always exit's with this (or code -1 as seen above):

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

抱歉,我的代码中到处都是荷兰语.

Sorry if there's a dutch word here and there in my code.

推荐答案

您错误地使用了IBackgroundTask,您必须先注册并在完成后通知.这可以通过将async void函数更改为async task并将Run设置为async void

You are using IBackgroundTask incorrectly, you must register your deferal and notifiy when it is complete. This is done by changing your async void functions to async task and making Run a 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");
        }
    }
}

这篇关于SerialDevice.FromIdAsync()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆