diff --git a/examples/nodejs_serial_connection.js b/examples/nodejs_serial_connection.js index 2ecb353..0ea6c2b 100644 --- a/examples/nodejs_serial_connection.js +++ b/examples/nodejs_serial_connection.js @@ -1,7 +1,7 @@ import NodeJSSerialConnection from "../src/connection/nodejs_serial_connection.js"; // create tcp connection -const connection = new NodeJSSerialConnection(); +const connection = new NodeJSSerialConnection("/dev/cu.usbmodem14401"); // wait until connected connection.on("connected", async () => { @@ -24,4 +24,4 @@ connection.on("connected", async () => { }); // connect to meshcore device -await connection.connect("/dev/cu.usbmodem14401"); +await connection.connect(); diff --git a/src/connection/nodejs_serial_connection.js b/src/connection/nodejs_serial_connection.js index 438ed52..b15d9a6 100644 --- a/src/connection/nodejs_serial_connection.js +++ b/src/connection/nodejs_serial_connection.js @@ -2,16 +2,15 @@ import SerialConnection from "./serial_connection.js"; class NodeJSSerialConnection extends SerialConnection { - constructor() { + /** + * @param path serial port to connect to, e.g: "/dev/ttyACM0" or "/dev/cu.usbmodem14401" + */ + constructor(path) { super(); + this.serialPortPath = path; } - /** - * Connect to Serial Port at the provided path - * @param path serial port to connect to, e.g: "/dev/ttyACM0" or "/dev/cu.usbmodem14401" - * @returns {Promise} - */ - async connect(path) { + async connect() { // note: serialport module is only available in NodeJS, you shouldn't use NodeJSSerialConnection from a web browser const { SerialPort } = await import('serialport'); @@ -19,7 +18,7 @@ class NodeJSSerialConnection extends SerialConnection { // create new serial port this.serialPort = new SerialPort({ autoOpen: false, // don't auto open, we want to control this manually - path: path, // e.g: "/dev/ttyACM0" or "/dev/cu.usbmodem14401" + path: this.serialPortPath, // e.g: "/dev/ttyACM0" or "/dev/cu.usbmodem14401" baudRate: 115200, });