-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an xample how to watch a serial port using Event Emiiters and …
…SocketIO
- Loading branch information
1 parent
310f855
commit d2d3428
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5" | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |