Using Windows UWP Windows.Devices.SerialCommunication.SerialDevice from the Universal Class Library

I am making a Modbus library (again ...). This time it is designed to work in Windows 10 IoT Core.

I had an interesting problem. This piece 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);
}
  • Used in a universal application. Of course, if you add the following code to Package.appxmanifest:

    <Capabilities>
         <DeviceCapability Name="serialcommunication">
             <Device Id="any">
                 <Function Type="name:serialPort" />
             </Device>
         </DeviceCapability>
     </Capabilities>
    
  • Used in the universal class library (File → New Project → ... → Windows → Universal → Class Library (Universal Windows) in VS2015) creates a Null Reference Exception from mscorlib.dll, which is the same as if you removed DeviceCapability from the universal application Package.appxmanifest.

I suspect this behavior is because the class library does not have a manifest file and therefore does not have the appropriate permissions.

Windows.Devices.SerialCommunication ?

Microsoft "! , ".

+4
1

Visual Studio 2015 Update 1 Windows 10 10586. , SerialDevice librairie .

.

, , , port2 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);
+5

Source: https://habr.com/ru/post/1619579/


All Articles