Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an example of how to watch a serial port using Event Emitters and So… #4044

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/serial-port/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { io } from "socket.io-client";

const socket = io("ws://localhost:8080/", {});

socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});

socket.on("disconnect", () => {
console.log(`disconnect`);
});

socket.on("onDeviceDisconnected", () => {
console.log(`onDeviceDisconnected`);
});
socket.on("onDeviceConnected", () => {
console.log(`onDeviceConnected`);
});
20 changes: 20 additions & 0 deletions examples/serial-port/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "typescript-example",
"version": "1.0.0",
"description": "An example with TypeScript",
"private": true,
"scripts": {
"build": "tsc",
"start:server": "ts-node server.ts",
"start:client": "ts-node client.ts"
},
"author": "Damien Arrachequesne",
"license": "MIT",
"dependencies": {
"serialport": "^9.2.0",
"socket.io": "^4.0.0",
"socket.io-client": "^4.0.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
}
}
87 changes: 87 additions & 0 deletions examples/serial-port/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Server } from "socket.io";
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
const emitter = require("events").EventEmitter;

const em = new emitter();
const deviceName = "/dev/tty.usbserial-0001";

const io = new Server(8080);

let found = false;

function findDevice() {
console.log("findDevice");
SerialPort.list()
.then(function (ports) {
// // iterate through ports
for (var i = 0; i < ports.length; i += 1) {
console.log("list", ports[i]);
if (ports[i].path === deviceName) {
console.log("device founded");
found = true;
em.emit("connected");
startDevice();
return;
}
}
if (!found) {
setTimeout(findDevice, 1000);
}
})
.catch(function (error) {
console.log("error on read port", error);
});
}

function startDevice() {
try {
const port = new SerialPort(deviceName, { baudRate: 9600 });
const parser = port.pipe(new Readline({ delimiter: "\n" })); // Read the port data

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

parser.on("data", (data) => {
console.log("got word from arduino:", data);
em.emit("onDataReceived", data);
});

port.on("close", function (err) {
console.log("Port closed."), err;
found = false;
em.emit("disconnect");
setTimeout(findDevice, 1000);
});
} catch (e) {
console.log("device not found ");
}
}

io.on("connect", (socket) => {
console.log(`connect ${socket.id}`);

if (found) {
socket.emit("onDeviceConnected");
} else {
socket.emit("onDeviceDisconnected");
}

em.on("disconnect", function (data) {
socket.emit("onDeviceDisconnected");
});
em.on("connected", function (data) {
socket.emit("onDeviceConnected");
});
em.on("onDataReceived", function (data) {
socket.emit("onDataReceived", data);
});

socket.on("disconnect", () => {
console.log(`disconnect ${socket.id}`);
});
});

console.log("start program");
findDevice();
9 changes: 9 additions & 0 deletions examples/serial-port/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
},
"exclude": [
"node_modules"
]
}