Using Windows UWP Windows.Devices.SerialCommunication.SerialDevice from Universal Class library

﹥>﹥吖頭↗ 提交于 2019-12-06 04:31:11

问题


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. 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>
    
  2. 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.

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.' ?


回答1:


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.

It works.

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


来源:https://stackoverflow.com/questions/34234217/using-windows-uwp-windows-devices-serialcommunication-serialdevice-from-universa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!