Reputation: 105
I was able to prove that the following JavaScript code does not generate the desired Modbus signal.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>examplepage</title>
<script>
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: 57600, dataBits: 8, stopBits: 1, ParityType: "even"});
//Übermittle Anfrage um Inhalt von Register anzufordern
const writer = port.writable.getWriter();
//const data = new Uint8Array([0x01, 0x03, 0x00, 0x01, 0x00, 0x02, 0x95, 0xCB]); // request for register
const data = new Uint8Array([0x11, 0x01, 0x00, 0x13, 0x00, 0x25, 0x0E, 0x84]);
await writer.write(data);
// Allow the serial port to be closed later.
writer.releaseLock();
// empfangen
const reader = port.readable.getReader();
// Listen to data coming from the serial device.
while (true) {
const { value, done } = await reader.read();
if (done) {
// Allow the serial port to be closed later.
reader.releaseLock();
break;
}
// value is a Uint8Array.
console.log(value);
}
}
if ("serial" in navigator) {
alert("Your browser supports Web Serial API!");
}
else {alert("Your browser does not support Web Serial API, the latest version of Google Chrome is recommended!");};
</script>
</head>
<body>
<button onclick="start()">Click me</button>
</body>
</html>
I have connected the corresponding COM port to another COM port of the computer. On the other side HTerm is listening. HTerm receives 11 40 13 25 88 instead of 11 01 00 13 00 25 0E 84. If I send the desired signal with HTerm I get the correct signal. Consequently, the hardware is working properly.
receiving the wrong command
send the desired modbus command by HTerm
receiving the desired commad sended by HTerm
What's going wrong here? How do I need to adjust my code? Or does the Google Chrome have a bug?
Upvotes: 0
Views: 994
Reputation: 105
François Beaufort is right. Replace ParityType with Parity and the code sends the correct signal. Thanks a lot!
Upvotes: 1