0

It is my intention to use the Web Serial API in Google Chrome to address a device with Modbus RTU.

The baud rate must be specified to start the setup - consequently this has already been done.

The following link leads to the part of a documentation which describes how to set up the parameters: Goog https://wicg.github.io/serial/#serialoptions-dictionary

I do not understand the syntax explanation. Javascript does not know a "dictionary".

Thanks for your help

5-HT2A
  • 69
  • 1
  • 1
  • 7
  • A dictionary is an object (see the [example here](https://wicg.github.io/serial/#open-method) or the [Web IDL Standard](https://webidl.spec.whatwg.org/#es-dictionary) if you want full details of the Dictionary->Object mapping). – Brits Nov 20 '21 at 00:57

2 Answers2

0

As explained at https://web.dev/serial/#open-port, once you have a SerialPort object, calling port.open() with the desired baud rate will open the serial port. The baudRate dictionary member specifies how fast data is sent over a serial line. It is expressed in units of bits-per-second (bps).

Check your device's documentation for the correct value as all the data you send and receive will be gibberish if this is specified incorrectly. For some USB and Bluetooth devices that emulate a serial port this value may be safely set to any value as it is ignored by the emulation.

// Prompt user to select any serial port.
const port = await navigator.serial.requestPort();

// Wait for the serial port to open.
await port.open({ baudRate: 9600 });

You can also specify other options when opening a serial port. These options are optional and have convenient default values.

  • dataBits: The number of data bits per frame (either 7 or 8).
  • stopBits: The number of stop bits at the end of a frame (either 1 or 2).
  • parity: The parity mode (either "none", "even" or "odd").
  • bufferSize: The size of the read and write buffers that should be created (must be less than 16MB).
  • flowControl: The flow control mode (either "none" or "hardware").
// Wait for the serial port to open with more options.
await port.open({
  baudRate: 9600,
  dataBits: 8,
  stopBits: 1,
  parity: "none",
  bufferSize: 255,
  flowControl: "none",
});

François Beaufort
  • 4,843
  • 3
  • 29
  • 38
  • My problem was that it was not clear to me how to specify other parameters besides the baud rate. So it was a trivial syntax problem. I found a way which is not underlined red in Visual Studio and does not generate an error message in the browser console. – 5-HT2A Nov 25 '21 at 07:33
  • I've edited my answer to include more options. – François Beaufort Nov 25 '21 at 08:47
-1
async function start() 
{
        // Prompt user to select any serial port.
        const port = await navigator.serial.requestPort();

        // Wait for the serial port to open.
        await port.open({ baudRate: 9600, dataBits: 8,  stopBits: 2, ParityType: "none"});
}
5-HT2A
  • 69
  • 1
  • 1
  • 7
  • You basically just took some code from the accepted answer – klutt Nov 25 '21 at 09:05
  • I was wrong there. Sorry about the unfair accusation. I saw that the accepted answer was edited to include YOUR suggestions. However, this is still not a good answer. Mostly because it's almost a code only answer without explanations. – klutt Nov 25 '21 at 11:04