This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathserialMonitor.ts
270 lines (232 loc) · 10.6 KB
/
serialMonitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import ArduinoContext from "../arduinoContext";
import * as constants from "../common/constants";
import { DeviceContext } from "../deviceContext";
import * as Logger from "../logger/logger";
import { SerialPortCtrl, SerialPortEnding } from "./serialportctrl";
export interface ISerialPortDetail {
comName: string;
manufacturer: string;
vendorId: string;
productId: string;
}
export class SerialMonitor implements vscode.Disposable {
public static SERIAL_MONITOR: string = "Serial Monitor";
public static DEFAULT_BAUD_RATE: number = 115200;
public static DEFAULT_ENDING: SerialPortEnding = SerialPortEnding["No line ending"];
public static listBaudRates(): number[] {
return [300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000];
}
public static getInstance(): SerialMonitor {
if (SerialMonitor._serialMonitor === null) {
SerialMonitor._serialMonitor = new SerialMonitor();
}
return SerialMonitor._serialMonitor;
}
private static _serialMonitor: SerialMonitor = null;
private _currentPort: string;
private _currentBaudRate: number;
private _portsStatusBar: vscode.StatusBarItem;
private _openPortStatusBar: vscode.StatusBarItem;
private _baudRateStatusBar: vscode.StatusBarItem;
private _endingStatusBar: vscode.StatusBarItem;
private _serialPortCtrl: SerialPortCtrl = null;
private _outputChannel: vscode.OutputChannel;
private _ending: SerialPortEnding;
private constructor() {
const dc = DeviceContext.getInstance();
dc.onDidChange(() => {
if (dc.port) {
if (!this.initialized) {
this.initialize();
}
this.updatePortListStatus(null);
}
});
}
public initialize() {
const defaultBaudRate = ArduinoContext.arduinoApp.settings.defaultBaudRate || SerialMonitor.DEFAULT_BAUD_RATE;
this._outputChannel = vscode.window.createOutputChannel(SerialMonitor.SERIAL_MONITOR);
this._currentBaudRate = defaultBaudRate;
this._portsStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.PORT);
this._portsStatusBar.command = "arduino.selectSerialPort";
this._portsStatusBar.tooltip = "Select Serial Port";
this._portsStatusBar.show();
this._openPortStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.OPEN_PORT);
this._openPortStatusBar.command = "arduino.openSerialMonitor";
this._openPortStatusBar.text = `$(plug)`;
this._openPortStatusBar.tooltip = "Open Serial Monitor";
this._openPortStatusBar.show();
this._baudRateStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.BAUD_RATE);
this._baudRateStatusBar.command = "arduino.changeBaudRate";
this._baudRateStatusBar.tooltip = "Baud Rate";
this._baudRateStatusBar.text = defaultBaudRate.toString();
this.updatePortListStatus(null);
this._endingStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.ENDING);
this._ending = SerialMonitor.DEFAULT_ENDING;
this._endingStatusBar.command = "arduino.changeEnding";
this._endingStatusBar.tooltip = "Serial Port Line Ending";
this._endingStatusBar.text = `No line ending`;
}
public get initialized(): boolean {
return !!this._outputChannel;
}
public dispose() {
if (this._serialPortCtrl && this._serialPortCtrl.isActive) {
return this._serialPortCtrl.stop();
}
}
public async selectSerialPort(vid: string, pid: string) {
const lists = await SerialPortCtrl.list();
if (!lists.length) {
vscode.window.showInformationMessage("No serial port is available.");
return;
}
if (vid && pid) {
const valueOfVid = parseInt(vid, 16);
const valueOfPid = parseInt(pid, 16);
const foundPort = lists.find((p) => {
// The pid and vid returned by SerialPortCtrl start with 0x prefix in Mac, but no 0x prefix in Win32.
// Should compare with decimal value to keep compatibility.
if (p.productId && p.vendorId) {
return parseInt(p.productId, 16) === valueOfPid && parseInt(p.vendorId, 16) === valueOfVid;
}
return false;
});
if (foundPort && !(this._serialPortCtrl && this._serialPortCtrl.isActive)) {
this.updatePortListStatus(foundPort.comName);
}
} else {
const chosen = await vscode.window.showQuickPick(<vscode.QuickPickItem[]>lists.map((l: ISerialPortDetail): vscode.QuickPickItem => {
return {
description: l.manufacturer,
label: l.comName,
};
}).sort((a, b): number => {
return a.label === b.label ? 0 : (a.label > b.label ? 1 : -1);
}), { placeHolder: "Select a serial port" });
if (chosen && chosen.label) {
this.updatePortListStatus(chosen.label);
}
}
}
public async openSerialMonitor() {
if (!this._currentPort) {
const ans = await vscode.window.showInformationMessage("No serial port was selected, please select a serial port first", "Yes", "No");
if (ans === "Yes") {
await this.selectSerialPort(null, null);
}
if (!this._currentPort) {
return;
}
}
if (this._serialPortCtrl) {
if (this._currentPort !== this._serialPortCtrl.currentPort) {
await this._serialPortCtrl.changePort(this._currentPort);
} else if (this._serialPortCtrl.isActive) {
vscode.window.showWarningMessage(`Serial monitor is already opened for ${this._currentPort}`);
return;
}
} else {
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate, this._ending, this._outputChannel);
}
if (!this._serialPortCtrl.currentPort) {
Logger.traceError("openSerialMonitorError", new Error(`Failed to open serial port ${this._currentPort}`));
return;
}
try {
await this._serialPortCtrl.open();
this.updatePortStatus(true);
} catch (error) {
Logger.notifyUserWarning("openSerialMonitorError", error,
`Failed to open serial port ${this._currentPort} due to error: + ${error.toString()}`);
}
}
public async sendMessageToSerialPort() {
if (this._serialPortCtrl && this._serialPortCtrl.isActive) {
const text = await vscode.window.showInputBox();
try {
await this._serialPortCtrl.sendMessage(text);
} catch (error) {
Logger.notifyUserWarning("sendMessageToSerialPortError", error, constants.messages.FAILED_SEND_SERIALPORT);
}
} else {
Logger.notifyUserWarning("sendMessageToSerialPortError", new Error(constants.messages.SEND_BEFORE_OPEN_SERIALPORT));
}
}
public async changeBaudRate() {
const rates = SerialMonitor.listBaudRates();
const chosen = await vscode.window.showQuickPick(rates.map((rate) => rate.toString()));
if (!chosen) {
Logger.warn("No rate is selected, keep baud rate no changed.");
return;
}
if (!parseInt(chosen, 10)) {
Logger.warn("Invalid baud rate, keep baud rate no changed.", { value: chosen });
return;
}
if (!this._serialPortCtrl) {
Logger.warn("Serial Monitor have not been started.");
return;
}
const selectedRate: number = parseInt(chosen, 10);
await this._serialPortCtrl.changeBaudRate(selectedRate);
this._currentBaudRate = selectedRate;
this._baudRateStatusBar.text = chosen;
}
public async changeEnding() {
const chosen: string|undefined = await vscode.window.showQuickPick(Object.keys(SerialPortEnding)
.filter((key) => {
return !isNaN(Number(SerialPortEnding[key]));
}), { placeHolder: "Select serial port ending" });
if (!chosen) {
return;
}
this._ending = SerialPortEnding[chosen];
this._serialPortCtrl.changeEnding(this._ending);
this._endingStatusBar.text = chosen;
}
public async closeSerialMonitor(port: string, showWarning: boolean = true): Promise<boolean> {
if (this._serialPortCtrl) {
if (port && port !== this._serialPortCtrl.currentPort) {
// Port is not opened
return false;
}
const result = await this._serialPortCtrl.stop();
this.updatePortStatus(false);
return result;
} else if (!port && showWarning) {
Logger.notifyUserWarning("closeSerialMonitorError", new Error(constants.messages.SERIAL_PORT_NOT_STARTED));
return false;
}
}
private updatePortListStatus(port: string) {
const dc = DeviceContext.getInstance();
if (port) {
dc.port = port;
}
this._currentPort = dc.port;
if (dc.port) {
this._portsStatusBar.text = dc.port;
} else {
this._portsStatusBar.text = "<Select Serial Port>";
}
}
private updatePortStatus(isOpened: boolean) {
if (isOpened) {
this._openPortStatusBar.command = "arduino.closeSerialMonitor";
this._openPortStatusBar.text = `$(x)`;
this._openPortStatusBar.tooltip = "Close Serial Monitor";
this._baudRateStatusBar.show();
this._endingStatusBar.show();
} else {
this._openPortStatusBar.command = "arduino.openSerialMonitor";
this._openPortStatusBar.text = `$(plug)`;
this._openPortStatusBar.tooltip = "Open Serial Monitor";
this._baudRateStatusBar.hide();
this._endingStatusBar.hide();
}
}
}