Skip to content

Commit f8c0697

Browse files
Durand NetoDurand Neto
Durand Neto
authored and
Durand Neto
committed
Adding example how to watch a serial port using Event Emiiters and SocketIO
1 parent 310f855 commit f8c0697

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

examples/serial-port/client.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { io } from "socket.io-client";
2+
3+
const socket = io("ws://localhost:8080/", {});
4+
5+
socket.on("connect", () => {
6+
console.log(`connect ${socket.id}`);
7+
});
8+
9+
socket.on("disconnect", () => {
10+
console.log(`disconnect`);
11+
});
12+
13+
socket.on("onDeviceDisconnected", () => {
14+
console.log(`onDeviceDisconnected`);
15+
});
16+
socket.on("onDeviceConnected", () => {
17+
console.log(`onDeviceConnected`);
18+
});

examples/serial-port/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "typescript-example",
3+
"version": "1.0.0",
4+
"description": "An example with TypeScript",
5+
"private": true,
6+
"scripts": {
7+
"build": "tsc",
8+
"start:server": "ts-node server.ts",
9+
"start:client": "ts-node client.ts"
10+
},
11+
"author": "Damien Arrachequesne",
12+
"license": "MIT",
13+
"dependencies": {
14+
"serialport": "^9.2.0",
15+
"socket.io": "^4.0.0",
16+
"socket.io-client": "^4.0.0",
17+
"ts-node": "^9.0.0",
18+
"typescript": "^4.0.5"
19+
}
20+
}

examples/serial-port/server.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Server } from "socket.io";
2+
const SerialPort = require("serialport");
3+
const Readline = require("@serialport/parser-readline");
4+
const emitter = require("events").EventEmitter;
5+
6+
const em = new emitter();
7+
const deviceName = "/dev/tty.usbserial-0001";
8+
9+
const io = new Server(8080);
10+
let found = false;
11+
12+
function findDevice() {
13+
console.log("findDevice");
14+
SerialPort.list()
15+
.then(function (ports) {
16+
// // iterate through ports
17+
for (var i = 0; i < ports.length; i += 1) {
18+
console.log("list", ports[i].path);
19+
if (ports[i].path === deviceName) {
20+
console.log("device foundded");
21+
found = true;
22+
em.emit("connected");
23+
startDevice();
24+
return;
25+
}
26+
}
27+
if (!found) {
28+
setTimeout(findDevice, 1000);
29+
}
30+
})
31+
.catch(function (error) {
32+
console.log("error on read port", error);
33+
});
34+
}
35+
36+
function startDevice() {
37+
try {
38+
const port = new SerialPort(deviceName, { baudRate: 9600 });
39+
const parser = port.pipe(new Readline({ delimiter: "\n" })); // Read the port data
40+
41+
port.on("open", () => {
42+
console.log("serial port open");
43+
});
44+
45+
parser.on("data", (data) => {
46+
console.log("got word from arduino:", data);
47+
});
48+
49+
port.on("close", function (err) {
50+
console.log("Port closed."), err;
51+
found = false;
52+
em.emit("disconnect");
53+
setTimeout(findDevice, 1000);
54+
});
55+
} catch (e) {
56+
console.log("device not found ");
57+
}
58+
}
59+
60+
io.on("connect", (socket) => {
61+
console.log(`connect ${socket.id}`);
62+
63+
if (found) {
64+
socket.emit("onDeviceConnected");
65+
} else {
66+
socket.emit("onDeviceDisconnected");
67+
}
68+
69+
em.on("disconnect", function (data) {
70+
socket.emit("onDeviceDisconnected");
71+
});
72+
em.on("connected", function (data) {
73+
socket.emit("onDeviceConnected");
74+
});
75+
76+
socket.on("disconnect", () => {
77+
console.log(`disconnect ${socket.id}`);
78+
});
79+
});
80+
81+
console.log("start program");
82+
findDevice();

examples/serial-port/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5"
5+
},
6+
"exclude": [
7+
"node_modules"
8+
]
9+
}

0 commit comments

Comments
 (0)