1

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

Brits
  • 14,829
  • 2
  • 18
  • 31
5-HT2A
  • 69
  • 1
  • 1
  • 7
  • Not really understanding your issue; a `Uint8Array` is an array of 8 bit unsigned integers; how you set this (using HEX, Decimal, Octal etc) is not relevant to the end result. For example `Uint8Array([0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x95, 0xCB])`. – Brits Nov 25 '21 at 19:06

1 Answers1

0

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);
François Beaufort
  • 4,843
  • 3
  • 29
  • 38