google-chrome

How to read COM ports? navigator.serial.getPorts returns empty list


I have a small number of USB serial interfaces connected (a HF transceiver, CW keyer based on Arduino) and two bluetooth ports, that I never use and am not sure where do they come from (probably some application). In my SPA I need to read list of ports to be able to select which one to connect as which function.

However, when I call navigator.serial.getPorts() I always get an empty list. BTW, same for navigator.usb.getDevices() It runs on Chrome version 87.0.4280.141 (Official Build) (64-bit). Serial API is enabled.

How to solve this?

UPDATE: it is invoked from script in HTML file in local filesystem (not served over HTTP)


Solution

  • You need to request the user's permission to read the Serial's ports by first calling navigator.serial.requestPort():

    onclick = async () => {
    
      await navigator.serial.requestPort();
      console.log( await navigator.serial.getPorts() );
    
    };
    

    Live example