2
2
// Licensed under the MIT license.
3
3
4
4
import * as os from "os" ;
5
+ import { SerialPort } from "serialport" ;
6
+ import * as strftime from "strftime" ;
5
7
import { BufferedOutputChannel } from "./outputBuffer" ;
6
- import { SerialPort } from 'serialport' ;
7
- import * as strftime from 'strftime' ;
8
8
9
9
interface ISerialPortDetail {
10
10
port : string ;
@@ -22,12 +22,12 @@ export class SerialPortCtrl {
22
22
*
23
23
*/
24
24
public static async list ( ) : Promise < ISerialPortDetail [ ] > {
25
- return ( await SerialPort . list ( ) ) . map ( port => {
25
+ return ( await SerialPort . list ( ) ) . map ( ( port ) => {
26
26
return {
27
27
port : port . path ,
28
28
desc : ( port as any ) . friendlyName ?? port . manufacturer ,
29
29
vendorId : port . vendorId ,
30
- productId : port . productId
30
+ productId : port . productId ,
31
31
} ;
32
32
} ) ;
33
33
}
@@ -60,30 +60,33 @@ export class SerialPortCtrl {
60
60
this . _bufferedOutputChannel . appendLine ( `[Starting] Opening the serial port - ${ this . _currentPort } ` ) ;
61
61
this . showOutputChannel ( ) ;
62
62
63
- if ( this . isActive ) await this . close ( ) ;
63
+ if ( this . isActive ) { await this . close ( ) ; }
64
64
65
65
await new Promise < void > ( ( resolve , reject ) => {
66
66
this . _port = new SerialPort (
67
67
{ autoOpen : false , path : this . _currentPort , baudRate : this . _currentBaudRate } ,
68
68
( err ) => {
69
- if ( err ) reject ( err ) ;
69
+ if ( err ) { reject ( err ) ; }
70
70
} ) ;
71
71
this . _port . open ( ( err ) => {
72
- if ( err ) reject ( err ) ;
73
- else resolve ( ) ;
72
+ if ( err ) {
73
+ reject ( err ) ;
74
+ } else {
75
+ resolve ( ) ;
76
+ }
74
77
} ) ;
75
78
} ) ;
76
79
77
80
let lastDataEndedWithNewline = true ;
78
81
this . _port . on ( "data" , ( data ) => {
79
- const text : string = data . toString ( ' utf8' ) ;
82
+ const text : string = data . toString ( " utf8" ) ;
80
83
if ( this . _currentTimestampFormat ) {
81
84
const timestamp = strftime ( this . _currentTimestampFormat ) ;
82
85
this . _bufferedOutputChannel . append (
83
86
// Timestamps should only be added at the beginning of a line.
84
87
// Look for newlines except at the very end of the string.
85
- ( lastDataEndedWithNewline ? timestamp : '' ) +
86
- text . replace ( / \n (? ! $ ) / g, "\n" + timestamp )
88
+ ( lastDataEndedWithNewline ? timestamp : "" ) +
89
+ text . replace ( / \n (? ! $ ) / g, "\n" + timestamp ) ,
87
90
) ;
88
91
lastDataEndedWithNewline = text . endsWith ( "\n" ) ;
89
92
} else {
@@ -109,14 +112,14 @@ export class SerialPortCtrl {
109
112
}
110
113
111
114
public async changePort ( newPort : string ) : Promise < void > {
112
- if ( newPort === this . _currentPort ) return ;
115
+ if ( newPort === this . _currentPort ) { return ; }
113
116
this . _currentPort = newPort ;
114
- if ( ! this . isActive ) return ;
117
+ if ( ! this . isActive ) { return ; }
115
118
await this . close ( ) ;
116
119
}
117
120
118
121
public async stop ( ) : Promise < boolean > {
119
- if ( ! this . isActive ) return false ;
122
+ if ( ! this . isActive ) { return false ; }
120
123
121
124
await this . close ( ) ;
122
125
if ( this . _bufferedOutputChannel ) {
@@ -128,7 +131,7 @@ export class SerialPortCtrl {
128
131
129
132
public async changeBaudRate ( newRate : number ) : Promise < void > {
130
133
this . _currentBaudRate = newRate ;
131
- if ( ! this . isActive ) return ;
134
+ if ( ! this . isActive ) { return ; }
132
135
await this . stop ( ) ;
133
136
await this . open ( ) ;
134
137
}
@@ -140,9 +143,12 @@ export class SerialPortCtrl {
140
143
private close ( ) : Promise < void > {
141
144
return new Promise ( ( resolve , reject ) => {
142
145
this . _port . close ( ( err ) => {
143
- if ( err ) reject ( err ) ;
144
- this . _port = undefined ;
145
- resolve ( ) ;
146
+ if ( err ) {
147
+ reject ( err ) ;
148
+ } else {
149
+ this . _port = undefined ;
150
+ resolve ( ) ;
151
+ }
146
152
} )
147
153
} ) ;
148
154
}
0 commit comments