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' ) ;
4
4
5
5
/**
6
6
* SerialPort device
7
7
* @param {[type] } port
8
8
* @param {[type] } options
9
9
*/
10
- function Serial ( port , options ) {
10
+ function Serial ( port , options ) {
11
11
var self = this ;
12
- options = options || {
12
+ options = options || {
13
13
baudRate : 9600 ,
14
- autoOpen : false ,
14
+ autoOpen : false
15
15
} ;
16
- const SerialPort = require ( " serialport" ) ;
16
+ const SerialPort = require ( ' serialport' ) ;
17
17
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 ) ;
20
20
self . device = null ;
21
21
} ) ;
22
22
EventEmitter . call ( this ) ;
23
23
return this ;
24
- }
24
+ } ;
25
25
26
26
util . inherits ( Serial , EventEmitter ) ;
27
27
@@ -30,7 +30,7 @@ util.inherits(Serial, EventEmitter);
30
30
* @param {Function } callback
31
31
* @return {[type] }
32
32
*/
33
- Serial . prototype . open = function ( callback ) {
33
+ Serial . prototype . open = function ( callback ) {
34
34
this . device . open ( callback ) ;
35
35
return this ;
36
36
} ;
@@ -41,7 +41,7 @@ Serial.prototype.open = function (callback) {
41
41
* @param {Function } callback [description]
42
42
* @return {[type] } [description]
43
43
*/
44
- Serial . prototype . write = function ( data , callback ) {
44
+ Serial . prototype . write = function ( data , callback ) {
45
45
this . device . write ( data , callback ) ;
46
46
return this ;
47
47
} ;
@@ -52,36 +52,38 @@ Serial.prototype.write = function (data, callback) {
52
52
* @param {int } timeout [allow manual timeout for emulated COM ports (bluetooth, ...)]
53
53
* @return {[type] } [description]
54
54
*/
55
- Serial . prototype . close = function ( callback , timeout ) {
55
+ Serial . prototype . close = function ( callback , timeout ) {
56
+
56
57
var self = this ;
57
58
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
+
72
72
} ) ;
73
+
73
74
} ) ;
74
75
75
76
return this ;
77
+
76
78
} ;
77
79
78
80
/**
79
81
* read buffer from the printer
80
82
* @param {Function } callback
81
83
* @return {Serial }
82
84
*/
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 ) {
85
87
callback ( data ) ;
86
88
} ) ;
87
89
return this ;
0 commit comments