Home > Enterprise >  Google Chrome Web Serial API: How do I configure the parameters for Modbus RTU?
Google Chrome Web Serial API: How do I configure the parameters for Modbus RTU?

Time:11-25

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

CodePudding user response:

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",
});

CodePudding user response:

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"});
}
  • Related