Skip to content

Commit 3d9bd18

Browse files
Add callback during port check
1 parent 3803ad6 commit 3d9bd18

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

kernel/src/kernel.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ export class EmbeddedKernel extends BaseKernel {
7474
// Reconnect the device or connect for the first time
7575
this.outputResponse("Reconnect command detected, reconnecting device...");
7676
await this.serviceContainer.deviceService.disconnect();
77-
await this.serviceContainer.deviceService.connect();
78-
this.outputResponse('Device Connected!');
77+
await this.serviceContainer.deviceService.connect(this.outputResponse);
7978
}
8079

8180
try {

kernel/src/services/DeviceService.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Transport } from 'esptool-js';
22

33
export class DeviceService {
44
private port: SerialPort | null = null;
5+
private portChecked: boolean = false;
56
private transport: Transport | null = null;
67
private isDeviceConnected: boolean = false;
78
private deviceType: string = '';
@@ -45,44 +46,47 @@ export class DeviceService {
4546
this.port = port;
4647

4748
// 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) {
5451
console.log('[requestPort]: Port is available');
5552
this.transport = new Transport(port);
5653
}
54+
else{
55+
console.log('[requestPort]: Port is already open by another application');
56+
}
5757

5858
} catch (err) {
5959
console.error('[requestPort]: Failed to get port:', err);
6060
throw err;
6161
}
6262
}
6363

64-
async connect(): Promise<void> {
64+
async connect(logCallback: (msg: string) => void = console.log): Promise<void> {
6565
await this.requestPort();
6666

6767
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?');
6973
}
7074

7175
try {
7276
if (this.isDeviceConnected) {
73-
console.log('Already connected, skipping connection');
77+
logCallback('Already connected, skipping connection');
7478
return;
7579
}
7680

7781
if (!this.port.readable && !this.port.writable) {
7882
this.transport?.connect()
7983
} else {
80-
console.log('Port is already open, skipping connection');
84+
logCallback('Port is already open, skipping connection');
8185
}
8286

8387
this.isDeviceConnected = true;
8488
} catch (err) {
85-
console.error('Failed to connect:', err);
89+
logCallback('Failed to connect:', err);
8690
throw err;
8791
}
8892

0 commit comments

Comments
 (0)