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
168 lines (145 loc) · 6.42 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
/*--------------------------------------------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------*/
import * as vscode from "vscode";
import * as constants from "../common/constants";
import { DeviceContext } from "../deviceContext";
import * as Logger from "../logger/logger";
import { SerialPortCtrl } from "./serialportctrl";
export interface ISerialPortDetail {
comName: string;
manufacturer: string;
vendorId: string;
productId: string;
}
export class SerialMonitor {
public static DEFAULT_BAUD_RATE: number = 9600;
public static listBaudRates(): number[] {
return [300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000];
}
private _currentPort: string;
private _currentBaudRate: number;
private _portsStatusBar: vscode.StatusBarItem;
private _openPortStatusBar: vscode.StatusBarItem;
private _baudRateStatusBar: vscode.StatusBarItem;
private _serialPortCtrl: SerialPortCtrl = null;
constructor() {
this._currentBaudRate = SerialMonitor.DEFAULT_BAUD_RATE;
this._portsStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 2);
this._portsStatusBar.command = "arduino.selectSerialPort";
this._portsStatusBar.tooltip = "Select Serial Port";
this._portsStatusBar.show();
this._openPortStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 3);
this._openPortStatusBar.command = "arduino.openSerialMonitor";
this._openPortStatusBar.text = `$(key)`;
this._openPortStatusBar.tooltip = "Open Port";
this._openPortStatusBar.show();
this._baudRateStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 4);
this._baudRateStatusBar.command = "arduino.changeBaudRate";
this._baudRateStatusBar.tooltip = "Baud Rate";
this._baudRateStatusBar.text = SerialMonitor.DEFAULT_BAUD_RATE.toString();
this.updatePortListStatus(null);
}
public async selectSerialPort() {
let lists = await SerialPortCtrl.list();
if (!lists.length) {
vscode.window.showInformationMessage("No serial port is available.");
return;
}
let 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);
}));
if (chosen && chosen.label) {
this.updatePortListStatus(chosen.label);
}
}
public async openSerialMonitor() {
if (this._serialPortCtrl) {
await this._serialPortCtrl.changePort(this._currentPort);
} else {
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate);
}
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()) {
let 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() {
let rates = SerialMonitor.listBaudRates();
let 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;
}
let selectedRate: number = parseInt(chosen, 10);
await this._serialPortCtrl.changeBaudRate(selectedRate);
this._currentBaudRate = selectedRate;
this._baudRateStatusBar.text = chosen;
}
public async closeSerialMonitor(port: string) {
if (this._serialPortCtrl) {
if (port && port !== this._currentPort) {
// Port is not opened
return;
}
await this._serialPortCtrl.stop();
this.updatePortStatus(false);
} else if (!port) {
Logger.notifyUserWarning("closeSerialMonitorError", new Error(constants.messages.SERIAL_PORT_NOT_STARTED));
}
}
private updatePortListStatus(port: string) {
const dc = DeviceContext.getIntance();
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();
} else {
this._openPortStatusBar.command = "arduino.openSerialMonitor";
this._openPortStatusBar.text = `$(key)`;
this._openPortStatusBar.tooltip = "Open Serial Monitor";
this._baudRateStatusBar.hide();
}
}
}