Skip to content

Commit d2d3428

Browse files
Durand Netodurandneto
Durand Neto
authored andcommitted
Adding an xample how to watch a serial port using Event Emiiters and SocketIO
1 parent 310f855 commit d2d3428

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-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

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