@@ -2,6 +2,7 @@ import { Transport } from 'esptool-js';
2
2
3
3
export class DeviceService {
4
4
private port : SerialPort | null = null ;
5
+ private portChecked : boolean = false ;
5
6
private transport : Transport | null = null ;
6
7
private isDeviceConnected : boolean = false ;
7
8
private deviceType : string = '' ;
@@ -45,44 +46,47 @@ export class DeviceService {
45
46
this . port = port ;
46
47
47
48
// Check if the port is open by another application (or ourselves in another tab)
48
-
49
- if ( ! await this . checkPort ( ) ) {
50
- console . log ( '[requestPort]: Port is already open by another application' ) ;
51
- // throw new Error('Port is already open by another application');
52
- }
53
- else {
49
+ this . portChecked = await this . checkPort ( )
50
+ if ( this . portChecked ) {
54
51
console . log ( '[requestPort]: Port is available' ) ;
55
52
this . transport = new Transport ( port ) ;
56
53
}
54
+ else {
55
+ console . log ( '[requestPort]: Port is already open by another application' ) ;
56
+ }
57
57
58
58
} catch ( err ) {
59
59
console . error ( '[requestPort]: Failed to get port:' , err ) ;
60
60
throw err ;
61
61
}
62
62
}
63
63
64
- async connect ( ) : Promise < void > {
64
+ async connect ( logCallback : ( msg : string ) => void = console . log ) : Promise < void > {
65
65
await this . requestPort ( ) ;
66
66
67
67
if ( ! this . port ) {
68
- throw new Error ( 'No port selected' ) ;
68
+ logCallback ( 'No port selected, try again.' ) ;
69
+ }
70
+
71
+ if ( ! this . portChecked ) {
72
+ logCallback ( 'Port cannot be opened.\nIs the port already open in another application or tab?' ) ;
69
73
}
70
74
71
75
try {
72
76
if ( this . isDeviceConnected ) {
73
- console . log ( 'Already connected, skipping connection' ) ;
77
+ logCallback ( 'Already connected, skipping connection' ) ;
74
78
return ;
75
79
}
76
80
77
81
if ( ! this . port . readable && ! this . port . writable ) {
78
82
this . transport ?. connect ( )
79
83
} else {
80
- console . log ( 'Port is already open, skipping connection' ) ;
84
+ logCallback ( 'Port is already open, skipping connection' ) ;
81
85
}
82
86
83
87
this . isDeviceConnected = true ;
84
88
} catch ( err ) {
85
- console . error ( 'Failed to connect:' , err ) ;
89
+ logCallback ( 'Failed to connect:' , err ) ;
86
90
throw err ;
87
91
}
88
92
0 commit comments