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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -