c# - UWP , Device COM port number is NULL in 2nd call SerialDevice.FromIdAsync -
to com port number ( serial, ex : com1,com2,,, ) when clicking button, want show available com port list on ui. first click success com numbers, 2nd click failed. have reason ?
here simple code test 2 click.
public async task<bool> test() { debug.writeline("start"); var aqs = serialdevice.getdeviceselector(); var dis = await deviceinformation.findallasync(aqs); var port = await serialdevice.fromidasync(dis[0].id); debug.writeline("com=" + port?.portname); var aqs2 = serialdevice.getdeviceselector(); var dis2 = await deviceinformation.findallasync(aqs2); var port2 = await serialdevice.fromidasync(dis2[0].id); //port2 null debug.writeline("com=" + port2?.portname); debug.writeline("end"); return true; } public mainpage() { this.initializecomponent(); task.run(() => this.test()).wait(); }
and package.appxmanifext has code.
<capability name="internetclient" /> <devicecapability name="serialcommunication"> <device id="any"> <function type="name:serialport" /> </device> </devicecapability> </capabilities>
and here executed result.
start com=com15 com= end
this post point out same issue. wrote comment @ here. answer, post new question.
q : com port list , update list when device changed, code should write ?
you have:
var aqs = serialdevice.getdeviceselector(); var dis = await deviceinformation.findallasync(aqs); var port = await serialdevice.fromidasync(dis[0].id); debug.writeline("com=" + port?.portname); var aqs2 = serialdevice.getdeviceselector(); var dis2 = await deviceinformation.findallasync(aqs); var port2 = await serialdevice.fromidasync(dis[0].id); debug.writeline("com="+port2?.portname);
i think meant have:
..... var aqs2 = serialdevice.getdeviceselector(); var dis2 = await deviceinformation.findallasync(aqs2); var port2 = await serialdevice.fromidasync(dis2[0].id); debug.writeline("com="+port2?.portname);
Comments
Post a Comment