Skip to content

Commit 2cde7ec

Browse files
reconbotThomas
authored andcommitted
feat: upgrade socket-io example to latest serialport and fix bug (serialport#1505)
1 parent a90a99c commit 2cde7ec

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

examples/socketio/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
const app = require('express')();
1313
const http = require('http').Server(app);
1414
const io = require('socket.io')(http);
15-
const port = process.env.PORT || 3000;
15+
const tcpPort = process.env.PORT || 3000;
1616

17-
const serialport = require('serialport');
18-
const SerialPort = serialport.SerialPort;
17+
const SerialPort = require('serialport');
1918

20-
const serial = new SerialPort('/dev/cu.usbmodem1411', {
21-
baudRate: 9600,
22-
parser: SerialPort.parsers.byteLength(1)
19+
const port = new SerialPort('/dev/cu.usbmodem1411', {
20+
baudRate: 9600
2321
});
2422

23+
const byteParser = new SerialPort.parsers.ByteLength({ length: 1 });
24+
port.pipe(byteParser);
25+
2526
// Values to send over to Arduino.
2627
const HIGH = Buffer.from([1]);
2728
const LOW = Buffer.from([0]);
@@ -36,8 +37,8 @@ app.get('/', (req, res) => {
3637
res.sendfile('index.html');
3738
});
3839

39-
http.listen(port, () => {
40-
console.log(`listening on *:${port}`);
40+
http.listen(tcpPort, () => {
41+
console.log(`listening on http://localhost:${tcpPort}`);
4142
});
4243

4344
/* ===========================================
@@ -57,10 +58,10 @@ io.on('connection', (socket) => {
5758
console.log('Message received: ', msg);
5859
switch (msg) {
5960
case 'on':
60-
serial.write(HIGH);
61+
port.write(HIGH);
6162
break;
6263
case 'off':
63-
serial.write(LOW);
64+
port.write(LOW);
6465
break;
6566
default:
6667
break;
@@ -74,15 +75,14 @@ io.on('connection', (socket) => {
7475
*
7576
=========================================== */
7677

77-
serial.on('open', () => {
78+
port.on('open', () => {
7879
console.log('Port is open!');
7980
});
8081

8182
/**
82-
* EventListener to receive data from .ino script uploaded to Arduino.
83-
*
83+
* listen to the bytes as they are parsed from the parser.
8484
*/
85-
serial.on('data', (data) => {
85+
byteParser.on('data', (data) => {
8686
let message;
8787

8888
if (HIGH.compare(data) === 0) {
@@ -96,7 +96,7 @@ serial.on('data', (data) => {
9696
io.sockets.emit('new message', message);
9797
});
9898

99-
serial.on('close', () => {
99+
port.on('close', () => {
100100
console.log('Serial port disconnected.');
101101
io.sockets.emit('close');
102102
});

examples/socketio/ledWrite.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ void loop() {
99
while (Serial.available()) {
1010
int byte = Serial.read();
1111
digitalWrite(LED_BUILTIN, byte);
12-
Serial.print(byte);
12+
Serial.write(byte);
1313
}
14-
}
14+
}

examples/socketio/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
2-
"name": "socketio-example",
3-
"version": "1.0.0",
2+
"name": "serialport-socketio-example",
3+
"version": "1.0.1",
44
"description": "LED switch example written for node-serialport.",
55
"main": "index.js",
66
"dependencies": {
77
"express": "^4.14.1",
8-
"serialport": "^4.0.7",
8+
"serialport": "^6.0.0",
99
"socket.io": "^1.7.2"
1010
},
1111
"devDependencies": {},
1212
"scripts": {
1313
"start": "node index.js"
1414
},
15-
"author": "GitHub",
16-
"license": "CC0-1.0"
15+
"license": "MIT"
1716
}

0 commit comments

Comments
 (0)