<commit_before>var serialport = (require("serialport/package").version.split(".")[0] > 3 ? require("serialport") : require("serialport").SerialPort);
var stream     = require("../stream");

function Driver() {

}

Driver.connect = function (device, options) {
	options = options || {};

	var port = new serialport(device || "/dev/ttyS0", {
		baudRate : options.baudRate || 9600,
		dataBits : options.dataBits || 8,
		stopBits : options.stopBits || 1,
		parity   : options.parity   || "none",
		autoOpen : false
	});

	return {
		attach : function (transport, next) {
			if (typeof options == "function") {
				next    = options;
				options = {};
			}

			port.open(function (err) {
				if (err) return next(err);

				return next(null, new stream(transport(port), options));
			});

			return port;
		}
	}
};

module.exports = Driver;<commit_msg>serial: return a proper code when not being able to open port<commit_after>var serialport = (require("serialport/package").version.split(".")[0] > 3 ? require("serialport") : require("serialport").SerialPort);
var stream     = require("../stream");
var pdu        = require("modbus-pdu");

function Driver() {

}

Driver.connect = function (device, options) {
	options = options || {};

	var port = new serialport(device || "/dev/ttyS0", {
		baudRate : options.baudRate || 9600,
		dataBits : options.dataBits || 8,
		stopBits : options.stopBits || 1,
		parity   : options.parity   || "none",
		autoOpen : false
	});

	return {
		attach : function (transport, next) {
			if (typeof options == "function") {
				next    = options;
				options = {};
			}

			port.on("error", function () {});

			port.open(function (err) {
				if (err) return next(pdu.Exception.error("GatewayPathUnavailable"));

				return next(null, new stream(transport(port), options));
			});

			return port;
		}
	}
};

module.exports = Driver;
{'fraction_non_alphanumeric': 0.16809881847475833, 'fraction_numerical': 0.00966702470461869, 'mean_word_length': 3.5108958837772395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3701247', 'n_tokens_mistral': 670, 'n_tokens_neox': 586, 'n_words': 141}