Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kingkupps committed Dec 13, 2024
0 parents commit 6fc4d09
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
14 changes: 14 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isMainThread, Worker } from "node:worker_threads";

if (isMainThread) {
const worker = new Worker("./worker.js");
worker.on("exit", (code) => {
console.log(`Worker excited with code ${code}`);
});

setTimeout(() => {
worker.postMessage({
type: "stop",
});
}, 5000);
}
289 changes: 289 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "serial-port-1938",
"version": "1.0.0",
"description": "Repro of https://github.com/serialport/node-serialport/issues/1938",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
"dependencies": {
"serialport": "^12.0.0"
}
}
28 changes: 28 additions & 0 deletions worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { isMainThread, parentPort } from "node:worker_threads";
import { SerialPort, ReadlineParser } from "serialport";

async function main() {
await new Promise((resolve) => {
const port = new SerialPort({
path: "/dev/tty.usbmodem101", // Replace with valid path
baudRate: 115200,
parser: new ReadlineParser({
delimiter: "\r\n",
}),
});

port.on("data", (data) => {
console.log(data);
});

parentPort.on("message", (message) => {
if (message.type === "stop") {
resolve();
}
});
});
}

if (!isMainThread) {
main();
}

0 comments on commit 6fc4d09

Please sign in to comment.