I can address a device via Modbus with my web application. Addressing means in this case sending a Modbus command and receiving a response.
However, I cannot repeat the process without closing the browser and restarting the page.
My specific question is now how do I adjust the code to permanently resend the same modbus command? So you would press the button and instead of a single response appearing in the console, after the response appears another response and another response would appear....
Thanks for your help.
<!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: 9600, dataBits: 8, stopBits: 2, ParityType: "none"});
//Ü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([0x01, 0x03, 0x00, 0x37, 0x00, 0x02, 0x75, 0xC5]);
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>
Aucun commentaire:
Enregistrer un commentaire