DuinoCom
is a simple JavaScript class designed to facilitate communication with Arduino or similar microcontrollers via a serial port in a web browser environment. This package utilizes the Web Serial API to establish a connection and exchange data with the serial port.
This npm package is intended for use in web browsers. Please refer to Can I use Web Serial API for information on browser compatibility.
You can install DuinoCom
via npm:
npm install duinocom
import DuinoCom from 'duinocom';
To create an instance of DuinoCom
, you need to specify the baud rate:
const duino = new DuinoCom(9600);
Before sending or receiving data, you need to establish a connection to the serial port:
await duino.connect();
You can send data to the connected serial port using the send()
method. By default, the data is expected to be a string:
await duino.send("Hello Arduino!");
If you need to send a byte array, you can specify the isByteArray
parameter as true
:
const byteArray = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in ASCII
await duino.send(byteArray, true);
To listen for incoming data from the serial port, you can use the listen()
method. You can provide a callback function to process the received data:
duino.listen((data) => {
console.log("Received data:", data);
});
The listen()
method expects the incoming data to be terminated by a newline (\n
). Once a newline is detected, the received data is passed to the provided callback function.
The connection to the serial port is automatically closed when the port is disconnected. However, you can also manually close the connection:
duino.serialPort.close();
The package includes basic error handling for connection, sending, and receiving data from the serial port. Errors are logged to the console with detailed error messages.
Here's a complete example demonstrating the usage of DuinoCom
:
import DuinoCom from 'duinocom';
const duino = new DuinoCom(9600);
async function main() {
try {
await duino.connect();
// Send data to Arduino
await duino.send("Hello Arduino!");
// Listen for incoming data
duino.listen((data) => {
console.log("Received data:", data);
});
} catch (error) {
console.error("An error occurred:", error);
}
}
// Trigger main function on button click
document.getElementById("startButton").addEventListener("click", main);
Note: To run the example, add a button with the id "startButton" to your HTML and execute the script in a web browser environment.
Please note that this package is still experimental and should be used with caution.
This package is licensed under the ISC License. See the LICENSE file for more details.
The source code for DuinoCom
is hosted on GitHub. Feel free to open issues or contribute to the project!