使用通用类库中的 Windows UWP Windows.Devices.SerialCommunication.SerialDevice [英] Using Windows UWP Windows.Devices.SerialCommunication.SerialDevice from Universal Class library

查看:56
本文介绍了使用通用类库中的 Windows UWP Windows.Devices.SerialCommunication.SerialDevice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Modbus 库(再次...).这次它打算在 Windows 10 IoT Core 上运行.

I'm making a Modbus library (again...). This time it is meant to run on Windows 10 IoT Core.

我遇到了一个有趣的问题.这段代码:

I've encountered an interesting problem. This chunk of code:

string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);

var port = await SerialDevice.FromIdAsync(dis[0].Id);

if (port != null) {
    port.BaudRate = 9600;
    port.DataBits = 8;
    port.StopBits = SerialStopBitCount.One;
    port.Parity = SerialParity.None;
    port.Handshake = SerialHandshake.None;
    port.ReadTimeout = TimeSpan.FromMilliseconds(1000);
    port.WriteTimeout = TimeSpan.FromMilliseconds(1000);
}

  1. 在通用应用程序中使用效果很好.当然,如果你在 Package.appxmanifest 中添加以下代码:

  1. Used from within a Universal App works perfectly. Of course if you add following code to Package.appxmanifest:

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

  • Used from within Universal Class Library (File -> New Project -> ... -> Windows -> Universal -> Class Library (Universal Windows) in VS2015) 从 mscorlib.dll 中创建空引用异常,其中与从通用应用的 Package.appxmanifest 中删除 DeviceCapability 相同.

  • Used from within Universal Class Library (File -> New Project -> ... -> Windows -> Universal -> Class Library (Universal Windows) in VS2015) creates Null Reference Exception from mscorlib.dll, which is same as if you remove DeviceCapability from Universal app's Package.appxmanifest.

    我怀疑这种行为与类库没有清单文件并因此没有适当的权限有关.

    I suspect that this behaviour has something to do with Class Library not having manifest file and therefore not having appropriate permissions.

    我可以在类库中使用 Windows.Devices.SerialCommunication 吗?

    Can I use Windows.Devices.SerialCommunication from within class libraries?

    是微软让我告诉用户'嘿!我为你做了一个无用的库,你必须在你的任何应用程序中自己实现传输层.?

    Is Microsoft making me tell the users 'Hey! I made for you a useless library for which you have to implement transport layer by yourself in any of your applications separately.' ?

    推荐答案

    我在 Windows 10 10586 上使用 Visual Studio 2015 Update 1 创建了一个通用应用项目和一个通用类库.我设置了应用清单,将 SerialDevice 代码放入图书馆并从应用中调用它.

    I created a Universal app project and a Universal Class Library using Visual Studio 2015 Update 1 on Windows 10 10586. I setup the app manifest, put the SerialDevice code in the librairie and call it from the app.

    它有效.

    请注意,如果您的代码被调用两次,您可能会收到空引用异常,因为 port2 将为空.

    Be careful if your code is called twice, you might get a null reference exception as port2 will be null.

    var aqs = SerialDevice.GetDeviceSelector();
    var dis = await DeviceInformation.FindAllAsync(aqs);
    var port = await SerialDevice.FromIdAsync(dis[0].Id);
    Debug.WriteLIne(port?.PortName);
    var aqs2 = SerialDevice.GetDeviceSelector();
    var dis2 = await DeviceInformation.FindAllAsync(aqs);
    var port2 = await SerialDevice.FromIdAsync(dis[0].Id);
    //port2 will be null
    Debug.WriteLine(port2?.PortName);
    

    这篇关于使用通用类库中的 Windows UWP Windows.Devices.SerialCommunication.SerialDevice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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