diff --git a/src/serialmonitor/serialMonitor.ts b/src/serialmonitor/serialMonitor.ts index 2dc847b4..64f2ecb1 100644 --- a/src/serialmonitor/serialMonitor.ts +++ b/src/serialmonitor/serialMonitor.ts @@ -61,6 +61,8 @@ export class SerialMonitor implements vscode.Disposable { }); this.serialMonitorApi = await getSerialMonitorApi(Version.latest, extensionContext); + + this.checkForUndefinedSerialMonitorApi(); } public get initialized(): boolean { @@ -68,6 +70,8 @@ export class SerialMonitor implements vscode.Disposable { } public async selectSerialPort(): Promise { + this.checkForUndefinedSerialMonitorApi(true); + const ports = await this.serialMonitorApi.listAvailablePorts(); if (!ports.length) { vscode.window.showInformationMessage("No serial port is available."); @@ -113,6 +117,8 @@ export class SerialMonitor implements vscode.Disposable { } public async openSerialMonitor(restore: boolean = false): Promise { + this.checkForUndefinedSerialMonitorApi(true); + if (!this.currentPort) { const ans = await vscode.window.showInformationMessage("No serial port was selected, please select a serial port first", "Select", "Cancel"); if (ans === "Select") { @@ -151,6 +157,8 @@ export class SerialMonitor implements vscode.Disposable { } public async closeSerialMonitor(port?: string): Promise { + this.checkForUndefinedSerialMonitorApi(true); + const portToClose = port ?? this.currentPort; let closed = false; if (portToClose) { @@ -165,6 +173,17 @@ export class SerialMonitor implements vscode.Disposable { this.serialMonitorApi.dispose(); } + private checkForUndefinedSerialMonitorApi(showError: boolean = false): void { + const errorString = "Serial Monitor API was not retrieved. You may not have the most recent version of the Serial Monitor extension installed."; + if (this.serialMonitorApi === undefined) { + if (showError) { + Logger.notifyUserError("UndefinedSerialMonitorApi", new Error(errorString)); + } else { + Logger.traceError("UndefinedSerialMonitorApi", new Error(errorString)); + } + } + } + private updatePortListStatus(port?: string) { const dc = DeviceContext.getInstance(); if (port) {