move path to constructor

This commit is contained in:
liamcottle 2025-04-08 12:18:12 +12:00
parent 7ec82f8554
commit bc32340185
2 changed files with 9 additions and 10 deletions

View file

@ -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();

View file

@ -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<void>}
*/
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,
});