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

Commit fedccc6

Browse files
authored
Add line ending for serial port
* Add line ending for serial port * add arduino.changeEnding into test
1 parent ace3b76 commit fedccc6

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

src/common/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export const statusBarPriority = {
3333
OPEN_PORT: 30,
3434
BAUD_RATE: 40,
3535
BOARD: 60,
36+
ENDING: 70,
3637
};

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export async function activate(context: vscode.ExtensionContext) {
170170
registerNonArduinoCommand("arduino.selectSerialPort", () => serialMonitor.selectSerialPort(null, null));
171171
registerNonArduinoCommand("arduino.openSerialMonitor", () => serialMonitor.openSerialMonitor());
172172
registerNonArduinoCommand("arduino.changeBaudRate", () => serialMonitor.changeBaudRate());
173+
registerNonArduinoCommand("arduino.changeEnding", () => serialMonitor.changeEnding());
173174
registerNonArduinoCommand("arduino.sendMessageToSerialPort", () => serialMonitor.sendMessageToSerialPort());
174175
registerNonArduinoCommand("arduino.closeSerialMonitor", (port) => serialMonitor.closeSerialMonitor(port));
175176

src/serialmonitor/serialMonitor.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as vscode from "vscode";
55
import * as constants from "../common/constants";
66
import { DeviceContext } from "../deviceContext";
77
import * as Logger from "../logger/logger";
8-
import { SerialPortCtrl } from "./serialportctrl";
8+
import { SerialPortCtrl, SerialPortEnding } from "./serialportctrl";
99

1010
export interface ISerialPortDetail {
1111
comName: string;
@@ -20,6 +20,8 @@ export class SerialMonitor implements vscode.Disposable {
2020

2121
public static DEFAULT_BAUD_RATE: number = 115200;
2222

23+
public static DEFAULT_ENDING: SerialPortEnding = SerialPortEnding["No line ending"];
24+
2325
public static listBaudRates(): number[] {
2426
return [300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000];
2527
}
@@ -43,10 +45,14 @@ export class SerialMonitor implements vscode.Disposable {
4345

4446
private _baudRateStatusBar: vscode.StatusBarItem;
4547

48+
private _endingStatusBar: vscode.StatusBarItem;
49+
4650
private _serialPortCtrl: SerialPortCtrl = null;
4751

4852
private _outputChannel: vscode.OutputChannel;
4953

54+
private _ending: SerialPortEnding;
55+
5056
private constructor() {
5157
const dc = DeviceContext.getInstance();
5258
dc.onDidChange(() => {
@@ -78,6 +84,12 @@ export class SerialMonitor implements vscode.Disposable {
7884
this._baudRateStatusBar.tooltip = "Baud Rate";
7985
this._baudRateStatusBar.text = SerialMonitor.DEFAULT_BAUD_RATE.toString();
8086
this.updatePortListStatus(null);
87+
88+
this._endingStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, constants.statusBarPriority.ENDING);
89+
this._ending = SerialMonitor.DEFAULT_ENDING;
90+
this._endingStatusBar.command = "arduino.changeEnding";
91+
this._endingStatusBar.tooltip = "Serial Port Line Ending";
92+
this._endingStatusBar.text = `No line ending`;
8193
}
8294
public get initialized(): boolean {
8395
return !!this._outputChannel;
@@ -144,7 +156,7 @@ export class SerialMonitor implements vscode.Disposable {
144156
return;
145157
}
146158
} else {
147-
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate, this._outputChannel);
159+
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate, this._ending, this._outputChannel);
148160
}
149161

150162
if (!this._serialPortCtrl.currentPort) {
@@ -195,6 +207,19 @@ export class SerialMonitor implements vscode.Disposable {
195207
this._baudRateStatusBar.text = chosen;
196208
}
197209

210+
public async changeEnding() {
211+
const chosen: string|undefined = await vscode.window.showQuickPick(Object.keys(SerialPortEnding)
212+
.filter((key) => {
213+
return !isNaN(Number(SerialPortEnding[key]));
214+
}), { placeHolder: "Select serial port ending" });
215+
if (!chosen) {
216+
return;
217+
}
218+
this._ending = SerialPortEnding[chosen];
219+
this._serialPortCtrl.changeEnding(this._ending);
220+
this._endingStatusBar.text = chosen;
221+
}
222+
198223
public async closeSerialMonitor(port: string, showWarning: boolean = true): Promise<boolean> {
199224
if (this._serialPortCtrl) {
200225
if (port && port !== this._serialPortCtrl.currentPort) {
@@ -230,11 +255,13 @@ export class SerialMonitor implements vscode.Disposable {
230255
this._openPortStatusBar.text = `$(x)`;
231256
this._openPortStatusBar.tooltip = "Close Serial Monitor";
232257
this._baudRateStatusBar.show();
258+
this._endingStatusBar.show();
233259
} else {
234260
this._openPortStatusBar.command = "arduino.openSerialMonitor";
235261
this._openPortStatusBar.text = `$(plug)`;
236262
this._openPortStatusBar.tooltip = "Open Serial Monitor";
237263
this._baudRateStatusBar.hide();
264+
this._endingStatusBar.hide();
238265
}
239266

240267
}

src/serialmonitor/serialportctrl.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ interface ISerialPortDetail {
1111
productId: string;
1212
}
1313

14+
export enum SerialPortEnding {
15+
"No line ending",
16+
"Newline",
17+
"Carriage return",
18+
"Both NL & CR",
19+
}
20+
1421
export class SerialPortCtrl {
1522
public static get serialport(): any {
1623
if (!SerialPortCtrl._serialport) {
@@ -36,10 +43,12 @@ export class SerialPortCtrl {
3643
private _currentPort: string;
3744
private _currentBaudRate: number;
3845
private _currentSerialPort = null;
46+
private _ending: SerialPortEnding;
3947

40-
public constructor(port: string, baudRate: number, private _outputChannel: OutputChannel) {
48+
public constructor(port: string, baudRate: number, ending: SerialPortEnding, private _outputChannel: OutputChannel) {
4149
this._currentBaudRate = baudRate;
4250
this._currentPort = port;
51+
this._ending = ending;
4352
}
4453

4554
public get isActive(): boolean {
@@ -69,7 +78,7 @@ export class SerialPortCtrl {
6978
this._currentSerialPort = new SerialPortCtrl.serialport(this._currentPort, { baudRate: this._currentBaudRate });
7079
this._outputChannel.show();
7180
this._currentSerialPort.on("open", () => {
72-
this._currentSerialPort.write("TestingOpen", (err) => {
81+
this._currentSerialPort.write("TestingOpen", "Both NL & CR", (err) => {
7382
// TODO: Fix this on the serial port lib: https://github.com/EmergingTechnologyAdvisors/node-serialport/issues/795
7483
if (err && !(err.message.indexOf("Writing to COM port (GetOverlappedResult): Unknown error code 121") >= 0)) {
7584
this._outputChannel.appendLine(`[Error] Failed to open the serial port - ${this._currentPort}`);
@@ -99,7 +108,7 @@ export class SerialPortCtrl {
99108
return;
100109
}
101110

102-
this._currentSerialPort.write(text, (error) => {
111+
this._currentSerialPort.write(text, SerialPortEnding[this._ending], (error) => {
103112
if (!error) {
104113
resolve();
105114
} else {
@@ -166,4 +175,7 @@ export class SerialPortCtrl {
166175
});
167176
});
168177
}
178+
public changeEnding(newEnding: SerialPortEnding) {
179+
this._ending = newEnding;
180+
}
169181
}

test/extension.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ suite("Arduino: Extension Tests", () => {
4444
"arduino.selectSerialPort",
4545
"arduino.openSerialMonitor",
4646
"arduino.changeBaudRate",
47+
"arduino.changeEnding",
4748
"arduino.sendMessageToSerialPort",
4849
"arduino.closeSerialMonitor",
4950
"arduino.reloadExample",

vendor/node-usb-native/lib/serialport.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ SerialPort.prototype.isOpen = function() {
216216
return this.fd !== null && !this.closing;
217217
};
218218

219-
SerialPort.prototype.write = function(buffer, callback) {
219+
SerialPort.prototype.write = function(buffer, ending, callback) {
220220
if (!this.isOpen()) {
221221
debug('write attempted, but port is not open');
222222
return this._error(new Error('Port is not open'), callback);
@@ -226,6 +226,20 @@ SerialPort.prototype.write = function(buffer, callback) {
226226
buffer = Buffer.from(buffer);
227227
}
228228

229+
switch (ending) {
230+
case 'Newline':
231+
buffer = Buffer.concat([buffer, Buffer.from('\n')]);
232+
break;
233+
case 'Carriage return':
234+
buffer = Buffer.concat([buffer, Buffer.from('\r')]);
235+
break;
236+
case 'Both NL & CR':
237+
buffer = Buffer.concat([buffer, Buffer.from('\r\n')]);
238+
break;
239+
default:
240+
break;
241+
}
242+
229243
debug(`write ${buffer.length} bytes of data`);
230244
SerialPortBinding.write(this.fd, buffer, (err) => {
231245
if (err) {

0 commit comments

Comments
 (0)