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 pathserialportctrl.ts
172 lines (155 loc) · 4.94 KB
/
serialportctrl.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as os from "os";
import { OutputChannel } from "vscode";
import { VscodeSettings } from "../arduino/vscodeSettings";
interface ISerialPortDetail {
path: string;
manufacturer: string;
vendorId: string;
productId: string;
}
export class SerialPortCtrl {
public static get serialport(): any {
if (!SerialPortCtrl._serialport) {
SerialPortCtrl._serialport = require("node-usb-native").SerialPort;
}
return SerialPortCtrl._serialport;
}
public static async list(): Promise<ISerialPortDetail[]> {
try {
const lists = SerialPortCtrl.serialport.list();
return lists;
} catch (err) {
throw err;
}
}
private static _serialport: any;
private _currentPort: string;
private _currentBaudRate: number;
private _currentSerialPort = null;
public constructor(port: string, baudRate: number, private _outputChannel: OutputChannel) {
this._currentBaudRate = baudRate;
this._currentPort = port;
}
public get isActive(): boolean {
return this._currentSerialPort && this._currentSerialPort.isOpen;
}
public get currentPort(): string {
return this._currentPort;
}
public open(): Promise<any> {
this._outputChannel.appendLine(`[Starting] Opening the serial port - ${this._currentPort}`);
return new Promise((resolve, reject) => {
if (this._currentSerialPort && this._currentSerialPort.isOpen) {
this._currentSerialPort.close((err) => {
if (err) {
return reject(err);
}
this._currentSerialPort = null;
return this.open().then(() => {
resolve();
}, (error) => {
reject(error);
});
});
} else {
this._currentSerialPort = new SerialPortCtrl.serialport(this._currentPort, { baudRate: this._currentBaudRate, hupcl: false });
this._outputChannel.show();
this._currentSerialPort.on("open", () => {
if (VscodeSettings.getInstance().disableTestingOpen) {
this._outputChannel.appendLine("[Warning] Auto checking serial port open is disabled");
return resolve();
}
this._currentSerialPort.write("TestingOpen" + "\r\n", (err) => {
// TODO: Fix this on the serial port lib: https://github.com/EmergingTechnologyAdvisors/node-serialport/issues/795
if (err && !(err.message.indexOf("Writing to COM port (GetOverlappedResult): Unknown error code 121") >= 0)) {
this._outputChannel.appendLine(`[Error] Failed to open the serial port - ${this._currentPort}`);
reject(err);
} else {
this._outputChannel.appendLine(`[Info] Opened the serial port - ${this._currentPort}`);
resolve();
}
});
});
this._currentSerialPort.on("data", (_event) => {
this._outputChannel.append(_event.toString());
});
this._currentSerialPort.on("error", (_error) => {
this._outputChannel.appendLine("[Error]" + _error.toString());
});
}
});
}
public sendMessage(text: string): Promise<any> {
return new Promise((resolve, reject) => {
if (!text || !this._currentSerialPort || !this.isActive) {
resolve();
return;
}
this._currentSerialPort.write(text + "\r\n", (error) => {
if (!error) {
resolve();
} else {
return reject(error);
}
});
});
}
public changePort(newPort: string): Promise<any> {
return new Promise((resolve, reject) => {
if (newPort === this._currentPort) {
resolve();
return;
}
this._currentPort = newPort;
if (!this._currentSerialPort || !this.isActive) {
resolve();
return;
}
this._currentSerialPort.close((err) => {
if (err) {
reject(err);
} else {
this._currentSerialPort = null;
resolve();
}
});
});
}
public stop(): Promise<any> {
return new Promise((resolve, reject) => {
if (!this._currentSerialPort || !this.isActive) {
resolve(false);
return;
}
this._currentSerialPort.close((err) => {
if (this._outputChannel) {
this._outputChannel.appendLine(`[Done] Closed the serial port ${os.EOL}`);
}
this._currentSerialPort = null;
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
public changeBaudRate(newRate: number): Promise<any> {
return new Promise((resolve, reject) => {
this._currentBaudRate = newRate;
if (!this._currentSerialPort || !this.isActive) {
resolve();
return;
}
this._currentSerialPort.update({ baudRate: this._currentBaudRate }, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}