I recently started experimenting with sending G-code to my printer via USB cable. I downloaded Printrun and it works great! However, I want to be able to do this from my web browser because the app im developing is web based.
I found this Chrome app called chrome-gcode-sender that does this but it unfortunately no longer works because Google sadly ended support for all Chrome apps in June 2022.
The maker of this app suggested on GitHub that the same thing should be achievable using Web Serial API. I have been trying to get this to work for almost two days now but with little success. I found several tutorials online about how to use Web Serial API but nothing for this specific use case. Here is a sample of the code I have developed so far, just a simple HTML document with some vanilla JavaScript:
<html>
<head>
<title>Web Serial API Demo</title>
</head>
<body>
<button id="connect">Connect</button>
<script>
var device
let button = document.getElementById("connect")
document.addEventListener("DOMContentLoaded", event => {
button.addEventListener("click", async() => {
try {
device = await navigator.serial.requestPort()
await device.open({baudRate: 9600})
console.log("Opened: ", device)
console.log("Info: ", device.getInfo())
const encoder = new TextEncoder()
const writer = device.writable.getWriter()
await writer.write(encoder.encode("G28"))
writer.releaseLock()
await device.close()
} catch (error) {
console.log(error)
}
})
})
</script>
</body>
</html>
When I click the 'Connect' button and select the device I want it appears to connect fine. However, when I try to send a simple G-code like G28 (autohome) the printer does nothing. If anyone has an idea how I could get this working please let me know! Im also 100 % open to an alternative approach! The main idea is just to be able to send G-code from a browser to a printer.
Notes:
- I've also tried experimenting with Web USB API but no luck here as well.