Skip to content

Commit 89b17d5

Browse files
MoegoeWetterlsongdev
authored andcommitted
added check for device when closing. in rare instances when doing lots of prints, we get a null device passed into the timeout for close
1 parent c44f3ff commit 89b17d5

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

packages/serialport/index.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
"use strict";
2-
const util = require("util");
3-
const EventEmitter = require("events");
1+
'use strict';
2+
const util = require('util');
3+
const EventEmitter = require('events');
44

55
/**
66
* SerialPort device
77
* @param {[type]} port
88
* @param {[type]} options
99
*/
10-
function Serial(port, options) {
10+
function Serial(port, options){
1111
var self = this;
12-
options = options || {
12+
options = options || {
1313
baudRate: 9600,
14-
autoOpen: false,
14+
autoOpen: false
1515
};
16-
const SerialPort = require("serialport");
16+
const SerialPort = require('serialport');
1717
this.device = new SerialPort(port, options);
18-
this.device.on("close", function () {
19-
self.emit("disconnect", self.device);
18+
this.device.on('close', function() {
19+
self.emit('disconnect', self.device);
2020
self.device = null;
2121
});
2222
EventEmitter.call(this);
2323
return this;
24-
}
24+
};
2525

2626
util.inherits(Serial, EventEmitter);
2727

@@ -30,7 +30,7 @@ util.inherits(Serial, EventEmitter);
3030
* @param {Function} callback
3131
* @return {[type]}
3232
*/
33-
Serial.prototype.open = function (callback) {
33+
Serial.prototype.open = function(callback){
3434
this.device.open(callback);
3535
return this;
3636
};
@@ -41,7 +41,7 @@ Serial.prototype.open = function (callback) {
4141
* @param {Function} callback [description]
4242
* @return {[type]} [description]
4343
*/
44-
Serial.prototype.write = function (data, callback) {
44+
Serial.prototype.write = function(data, callback){
4545
this.device.write(data, callback);
4646
return this;
4747
};
@@ -52,36 +52,38 @@ Serial.prototype.write = function (data, callback) {
5252
* @param {int} timeout [allow manual timeout for emulated COM ports (bluetooth, ...)]
5353
* @return {[type]} [description]
5454
*/
55-
Serial.prototype.close = function (callback, timeout) {
55+
Serial.prototype.close = function(callback, timeout) {
56+
5657
var self = this;
5758

58-
this.device.drain(function () {
59-
self.device.flush(function (err) {
60-
setTimeout(
61-
function () {
62-
err
63-
? callback && callback(err, self.device)
64-
: self.device &&
65-
self.device.close(function (err) {
66-
self.device = null;
67-
return callback && callback(err, self.device);
68-
});
69-
},
70-
"number" === typeof timeout && 0 < timeout ? timeout : 0
71-
);
59+
this.device.drain(function() {
60+
61+
self.device.flush(function(err) {
62+
63+
setTimeout(function() {
64+
65+
err ? callback && callback(err, self.device) : self.device && self.device.close(function(err) {
66+
self.device = null;
67+
return callback && callback(err, self.device);
68+
});
69+
70+
}, "number" === typeof timeout && 0 < timeout ? timeout : 0);
71+
7272
});
73+
7374
});
7475

7576
return this;
77+
7678
};
7779

7880
/**
7981
* read buffer from the printer
8082
* @param {Function} callback
8183
* @return {Serial}
8284
*/
83-
Serial.prototype.read = function (callback) {
84-
this.device.on("data", function (data) {
85+
Serial.prototype.read = function(callback) {
86+
this.device.on('data', function(data) {
8587
callback(data);
8688
});
8789
return this;

0 commit comments

Comments
 (0)