TWebUSBSerial

Hello,

Do you have any exemple code showing how to use the TWebUSBSerial component ? Specialy how to select the right port and open it.
Thanks

Hi,

All of our samples are hardware specific, but generally you can do something like:

procedure TForm2.requestBtnClick(Sender: TObject);
begin
  WebUSBSerial1.RequestPorts;
end;

procedure TForm2.WebUSBSerial1OpenPort(Sender: TObject; APort: TSerialPort);
begin
  WebMemo1.Lines.Add('Connected');
end;

procedure TForm2.WebUSBSerial1PortsInitialized(Sender: TObject);
var
  I: Integer;
begin
  //Ports initialized (triggered after RequestPorts)
  requestBtn.Enabled := False;

  //Log all the ports
  for I := 0 to WebUSBSerial1.Ports.Count - 1 do
    WebMemo1.Lines.Add('ProductId: ' + IntToStr(WebUSBSerial1.Ports.Items[I].ProductId) + ' - VendorId: ' + IntToStr(WebUSBSerial1.Ports.Items[I].VendorId));

  //Select the first one, but different logic can be used
  //For example: select based on ProductId or VendorId
  if WebUSBSerial1.Ports.Count > 0 then
    WebUSBSerial1.SelectedPort := WebUSBSerial1.Ports.Items[0];

  //WebUSBSerial1.AutoOpenSelection is True by default so it will
  //automatically open the port.
end;

In the Ports collection you only have ports that the user has granted access to (after they see the popup). You can filter the list beforehand based on product and vendor IDs.

If you set the AutoInitializePorts property to True it will trigger the OnPortsInitialized event if the website has access to ports already. This allows to to automatically connect to ports that you've connected to before (= end-user has granted access to them).

1 Like

Thank you.