Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 45dbb8d

Browse files
authored
Add fixes from code review (#1328)
* remove native serial dependencies * fixes from code review * Revert "remove native serial dependencies" This reverts commit 6586a66. * add vid pid support * code review fixes * revert stop to return bool
1 parent 548103b commit 45dbb8d

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@
503503
},
504504
"arduino.enableUSBDetection": {
505505
"type": "boolean",
506-
"default": true
506+
"default": false,
507+
"description": "USB Detection is currently not working"
507508
},
508509
"arduino.disableTestingOpen": {
509510
"type": "boolean",

src/serialmonitor/serialMonitor.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ export class SerialMonitor implements vscode.Disposable {
103103
const foundPort = lists.find((p) => {
104104
// The pid and vid returned by SerialPortCtrl start with 0x prefix in Mac, but no 0x prefix in Win32.
105105
// Should compare with decimal value to keep compatibility.
106-
// if (p.productId && p.vendorId) {
107-
// return parseInt(p.productId, 16) === valueOfPid && parseInt(p.vendorId, 16) === valueOfVid;
108-
// }
106+
if (p.productId && p.vendorId) {
107+
return parseInt(p.productId, 16) === valueOfPid && parseInt(p.vendorId, 16) === valueOfVid;
108+
}
109109
return false;
110110
});
111111
if (foundPort && !(this._serialPortCtrl && this._serialPortCtrl.isActive)) {

src/serialmonitor/serialportctrl.ts

+48-20
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,42 @@ interface ISerialPortDetail {
1111
port: string;
1212
desc: string;
1313
hwid: string;
14-
// vendorId: string;
15-
// productId: string;
14+
vendorId: string;
15+
productId: string;
1616
}
1717

1818
export class SerialPortCtrl {
1919

20+
/**
21+
* Launches the serial monitor to check which external usb devices are connected.
22+
*
23+
* @returns An array of ISerialPortDetail from external serial devices.
24+
*
25+
*/
2026
public static list(): Promise<ISerialPortDetail[]> {
2127
// TODO: Wrap this in a try catch block, catch error if no serial monitor at path
2228
const stdout = execFileSync(SerialPortCtrl._serialCliPath, ["list-ports"]);
2329
const lists = JSON.parse(stdout);
30+
lists.forEach((port) => {
31+
const vidPid = this._parseVidPid(port["hwid"]);
32+
port["vendorId"] = vidPid["vid"];
33+
port["productId"] = vidPid["pid"];
34+
});
2435
return lists;
2536
}
2637

38+
/**
39+
* Parse out vendor id and product id from the hardware id provided by the device.
40+
*
41+
* @param hwid: The hardware information for a sepcific device
42+
*
43+
* @returns vendor id and product id values in an array. Returns null if none are found.
44+
*/
45+
private static _parseVidPid(hwid: string): any {
46+
const result = hwid.match(/VID:PID=(?<vid>\w+):(?<pid>\w+)/i);
47+
return result !== null ? result["groups"] : [null, null];
48+
}
49+
2750
private static get _serialCliPath(): string {
2851
let fileName: string;
2952
if (os.platform() === "win32") {
@@ -56,17 +79,18 @@ export class SerialPortCtrl {
5679
return this._currentPort;
5780
}
5881

59-
public open(): Promise<any> {
82+
public open(): Promise<void> {
6083
this._outputChannel.appendLine(`[Starting] Opening the serial port - ${this._currentPort}`);
6184
this._outputChannel.show();
6285

6386
if (this._child) {
64-
this._child.stdin.write("close\n");
87+
this.stop();
6588
}
66-
this._child = spawn(SerialPortCtrl._serialCliPath,
67-
["open", this._currentPort, "-b", this._currentBaudRate.toString(), "--json"])
6889

6990
return new Promise((resolve, reject) => {
91+
this._child = spawn(SerialPortCtrl._serialCliPath,
92+
["open", this._currentPort, "-b", this._currentBaudRate.toString(), "--json"])
93+
7094
this._child.on("error", (err) => {
7195
reject(err)
7296
});
@@ -75,51 +99,56 @@ export class SerialPortCtrl {
7599
const jsonObj = JSON.parse(data.toString())
76100
this._outputChannel.append(jsonObj["payload"] + "\n");
77101
});
78-
resolve(true);
102+
// TODO: add message check to ensure _child spawned without errors
103+
resolve();
104+
// The spawn event is only supported in node v15+ vscode
105+
// this._child.on("spawn", (spawn) => {
106+
// resolve();
107+
// });
108+
79109
});
80110
}
81111

82-
public sendMessage(text: string): Promise<any> {
112+
public sendMessage(text: string): Promise<void> {
83113
return new Promise((resolve, reject) => {
84114
if (!text || !this._currentSerialPort || !this.isActive) {
85-
resolve(false);
115+
resolve();
86116
return;
87117
}
88118

89119
this._currentSerialPort.write(text + "\r\n", (error) => {
90120
if (!error) {
91-
resolve(true);
121+
resolve();
92122
} else {
93123
return reject(error);
94124
}
95125
});
96126
});
97127
}
98128

99-
public changePort(newPort: string): Promise<any> {
129+
public changePort(newPort: string): Promise<void> {
100130
return new Promise((resolve, reject) => {
101131
if (newPort === this._currentPort) {
102-
resolve(true);
132+
resolve();
103133
return;
104134
}
105135
this._currentPort = newPort;
106136
if (!this._currentSerialPort || !this.isActive) {
107-
resolve(false);
137+
resolve();
108138
return;
109139
}
110140
this._currentSerialPort.close((err) => {
111141
if (err) {
112142
reject(err);
113143
} else {
114144
this._currentSerialPort = null;
115-
resolve(true);
145+
resolve();
116146
}
117147
});
118148
});
119149
}
120150

121-
public stop(): Promise<any> {
122-
this._child.stdin.write('{"cmd": "close"}\n');
151+
public stop(): Promise<boolean> {
123152
return new Promise((resolve, reject) => {
124153
if (!this.isActive) {
125154
resolve(false);
@@ -138,18 +167,17 @@ export class SerialPortCtrl {
138167
});
139168
}
140169

141-
public changeBaudRate(newRate: number): Promise<any> {
142-
// this._outputChannel.appendLine(this.isActive.toString());
170+
public changeBaudRate(newRate: number): Promise<void> {
143171
return new Promise((resolve, reject) => {
144172
this._currentBaudRate = newRate;
145173
if (!this._child || !this.isActive) {
146-
resolve(true);
174+
resolve();
147175
return;
148176
} else {
149177
try {
150178
this.stop();
151179
this.open();
152-
resolve(true);
180+
resolve();
153181
} catch (error) {
154182
reject(error);
155183
}

0 commit comments

Comments
 (0)