How do you get the serial port of a printer device?

449 Views Asked by At

I'm using WebSerial API to use thermal-printer in chrome browser, and want to access only 'COM1' port without requestPort.

Can I access to specific port and using that port?

1

There are 1 best solutions below

0
Shankaja Aroshana On

For security purposes user consent is needed for the WebSerial API.

document.querySelector('button').addEventListener('click', async () => {
  // Prompt user to select any serial port.
  const port = await navigator.serial.requestPort();
});

After the first connection you could use this code to access previously consented ports by user

// Get all serial ports the user has previously granted the website access to.
const ports = await navigator.serial.getPorts();