mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
This has always been the dream. Use `fs.read` and `fs.write` to do reading and writing. To make that happen I've created a cross platform `Poller` object that notifies us once on `readable` and `writable` states. - Reduce large write cpu usage from 100% to 1-2% - Replace ReadPoller with a multi purpose poller because you can only have a single uv_poll_t on a fd at a time because reasons - Experimented with doing this all in c++, maybe there are gains to doing that but I'd never finish. - Some unrelated eslint changes - Arduino echo should just echo, writing random bytes was messing it up - use a file to disable tests in envs - read and write errors can trigger disconnects - disconnect event only happens if there are listeners, otherwise error events will be triggered - a disconnect during write will always have a write error too - win32 bindings seem to only trigger disconnects on writes
102 lines
2.2 KiB
JavaScript
102 lines
2.2 KiB
JavaScript
/* eslint-disable node/no-missing-require */
|
|
|
|
/**
|
|
* This is a small example app to turn off and on
|
|
* the built-in LED of an arduino by data sent
|
|
* from the browser with socket.io.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
// Initialize application constants
|
|
const app = require('express')();
|
|
const http = require('http').Server(app);
|
|
const io = require('socket.io')(http);
|
|
const port = process.env.PORT || 3000;
|
|
|
|
const serialport = require('serialport');
|
|
const SerialPort = serialport.SerialPort;
|
|
|
|
const serial = new SerialPort('/dev/cu.usbmodem1411', {
|
|
baudrate: 9600,
|
|
parser: SerialPort.parsers.byteLength(1)
|
|
});
|
|
|
|
// Values to send over to Arduino.
|
|
const HIGH = Buffer.from([1]);
|
|
const LOW = Buffer.from([0]);
|
|
|
|
/* ===========================================
|
|
*
|
|
* Setup a simple server.
|
|
*
|
|
=========================================== */
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendfile('index.html');
|
|
});
|
|
|
|
http.listen(port, () => {
|
|
console.log(`listening on *:${port}`);
|
|
});
|
|
|
|
/* ===========================================
|
|
*
|
|
* Socket.io stuff
|
|
*
|
|
=========================================== */
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('a user connected');
|
|
|
|
/**
|
|
* Socket listener to determine whether or not to send HIGH / LOW
|
|
* values to Arduino.
|
|
*/
|
|
socket.on('message', (msg) => {
|
|
console.log('Message received: ', msg);
|
|
switch (msg) {
|
|
case 'on':
|
|
serial.write(HIGH);
|
|
break;
|
|
case 'off':
|
|
serial.write(LOW);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
});
|
|
|
|
/* ===========================================
|
|
*
|
|
* Serialport stuff
|
|
*
|
|
=========================================== */
|
|
|
|
serial.on('open', () => {
|
|
console.log('Port is open!');
|
|
});
|
|
|
|
/**
|
|
* EventListener to receive data from .ino script uploaded to Arduino.
|
|
*
|
|
*/
|
|
serial.on('data', (data) => {
|
|
let message;
|
|
|
|
if (HIGH.compare(data) === 0) {
|
|
message = 'LED successfully turned on.';
|
|
} else if (LOW.compare(data) === 0) {
|
|
message = 'LED successfully turned off.';
|
|
} else {
|
|
message = 'LED did not behave as expected.';
|
|
}
|
|
|
|
io.sockets.emit('new message', message);
|
|
});
|
|
|
|
serial.on('close', () => {
|
|
console.log('Serial port disconnected.');
|
|
io.sockets.emit('close');
|
|
});
|