0

I'm doing a project in Angular and I need to make the connection to a scale through the serial input (data input and output). I'm using the serialPort web API.

I have already enabled the experimental web platform features of my browser. Chrome version is 86.0.4240.198.

My system is showing the available entries. However, when I do an .open, it cannot make the connection.

This is my code:

...

reader: ReadableStreamDefaultReader;
writer: WritableStreamDefaultWriter;
encoder = new TextEncoder();
decoder = new TextDecoder();

...

async init() {
    if ('serial' in navigator) {
      try {
        const port = await (navigator as any).serial.requestPort();
        await console.log(port)
        await port.open({ baudrate: 4800 });
        this.reader = port.readable.getReader();
        this.writer = port.writable.getWriter();

        this.verificacao = true;
      } catch(err) {
        console.error('There was an error opening the serial port:', err);
        this.verificacao = false;
      }
    } else {
      console.error('Web serial doesn\'t seem to be enabled in your browser. Try enabling it by visiting:')
      console.error('chrome://flags/#enable-experimental-web-platform-features');
      console.error('opera://flags/#enable-experimental-web-platform-features');
      console.error('edge://flags/#enable-experimental-web-platform-features');
    }
  }

...

I can identify the serial input:

listing of serial ports

But this error happens:

Failed to execute 'open' on 'SerialPort': required member baudRate is undefined.

When selecting the serial input, the port variable returns:

variable port

1 Answers1

0

I'm still looking for a clean solution myself. The issue @Otavia Koike was having setting baud rate is because the variable's been renamed from baudrate to baudRate. So, await port.open({ baudRate: 4800 }); would have gotten them over this particular hurdle.

And even though I'm not sure I can use it in my environment yet, here's a link to serialport.io's example electron-serialport project on GitHub. Hope this helps someone, anyone out there!

Sashu
  • 21
  • 5