Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js and Serialport on Windows 7 to connect your Ardunio or other cool devices

Node.js and serialport with Windows 7

This is a short example how to get the Node.js package serialport up and running with Windows 7 (this should also work for higher versions of Windows, but I didn't test that).

By using this package, you will be able to send and receive data to and from the serial port of your Arduino or any other device, using this port.

Installation

As of September 16, 2015, serialport does not compile using Node.js version: v4.0.0

First of all, you need to get Node.js.

To install the serialport package on Windows, is a bit more time consuming, because you have to compile the package on your machine.

To be able to do that, you need to install a version of Visual Studio.

After you installed it you can now install and compile serialport (the msvs_version depends on your version of Visual Studio):

npm install serialport -msvs_version=2013

Usage

Here is a little example on how to use serialport to connect to your device (e.g. Arduino):

/**
 * Create HTTP server.
 */

var http = require('http');
var port = process.env.PORT || 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);

/**
 * Create IO connection
 */

var io = require('socket.io').listen(server);


/**
 * Create SerialPort connection
 */

var SerialPort = require("serialport").SerialPort

// creates a new serial port connection, in this case to port COM5
// Windows users: to find out which port is used, go to: Device Manager -> Ports (COM & LPT)
var serialPort = new SerialPort("COM5", {
	baudrate: 9600
}, false); // this is the openImmediately flag [default is true]


serialPort.open(function (error) {
	if ( error ) {
		console.log('failed to open: ' + error);
	} else {
		console.log('serial port opened');
		serialport_opened = true;

		// get data from connected device via serial port
		serialPort.on('data', function(data) {
			// get buffered data and parse it to an utf-8 string
			data = data.toString('utf-8');
			// you could for example, send this data now to the the client via socket.io
			// io.emit('emit_data', data);
		});

		serialPort.on('error', function(data) {
			console.log('Error: ' + data);
		})
	}
});

// sends data to the connected device via serial port
function writeAndDrain (data, callback) {
	// flush data received but not read
	serialPort.flush();

	// write/send data to serial port
	serialPort.write(data, function (error) {
		if(error){console.log(error);}
		else{
			// waits until all output data has been transmitted to the serial port.
			serialPort.drain(callback);      
		}
	});
}

io.on('connection', function (socket) {
	console.log('user connected');
	socket.on('disconnect', function(){
		console.log('user disconnected');
	});
	socket.on('get_data', function(data) {
		// send data to the connected device via serial port
		writeAndDrain(data, null);
	})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment