Google Chrome Web Serial API: How do I address a Modbus device with a hex code?

522 views Asked by At

It is my intention to address a device with Modbus via the Web Serial API built into Google Chrome. I want to address my device with a HEX code. The following screenshot proves that my device can be successfully addressed with a tool like this. screenshot The interface is thus addressed with the following hex value: 01 03 00 01 00 02 95 CB

My question now is. The Tutorial only shows how to address the interface as Uint8Array or Text. How can I address the interface with a HEX code?

Thanks for the help

1

There are 1 answers

0
François Beaufort On

As indicated in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array, the Uint8Array typed array represents an array of 8-bit unsigned integers.

In JavaScript, 0x is used to indicate that all subsequent characters should be interpreted as hexadecimal (base 16 number system).

Therefore, all you need would be something like:

const writer = port.writable.getWriter();

const data = new Uint8Array([0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x95, 0xCB]);
await writer.write(data);