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

Add line ending for serial port #504

Merged
merged 2 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export const statusBarPriority = {
OPEN_PORT: 30,
BAUD_RATE: 40,
BOARD: 60,
ENDING: 70,
};
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export async function activate(context: vscode.ExtensionContext) {
registerNonArduinoCommand("arduino.selectSerialPort", () => serialMonitor.selectSerialPort(null, null));
registerNonArduinoCommand("arduino.openSerialMonitor", () => serialMonitor.openSerialMonitor());
registerNonArduinoCommand("arduino.changeBaudRate", () => serialMonitor.changeBaudRate());
registerNonArduinoCommand("arduino.changeEnding", () => serialMonitor.changeEnding());
registerNonArduinoCommand("arduino.sendMessageToSerialPort", () => serialMonitor.sendMessageToSerialPort());
registerNonArduinoCommand("arduino.closeSerialMonitor", (port) => serialMonitor.closeSerialMonitor(port));

Expand Down
31 changes: 29 additions & 2 deletions src/serialmonitor/serialMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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";
import { SerialPortCtrl, SerialPortEnding } from "./serialportctrl";

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

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];
}
Expand All @@ -43,10 +45,14 @@ export class SerialMonitor implements vscode.Disposable {

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(() => {
Expand Down Expand Up @@ -78,6 +84,12 @@ export class SerialMonitor implements vscode.Disposable {
this._baudRateStatusBar.tooltip = "Baud Rate";
this._baudRateStatusBar.text = SerialMonitor.DEFAULT_BAUD_RATE.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;
Expand Down Expand Up @@ -144,7 +156,7 @@ export class SerialMonitor implements vscode.Disposable {
return;
}
} else {
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate, this._outputChannel);
this._serialPortCtrl = new SerialPortCtrl(this._currentPort, this._currentBaudRate, this._ending, this._outputChannel);
}

if (!this._serialPortCtrl.currentPort) {
Expand Down Expand Up @@ -195,6 +207,19 @@ export class SerialMonitor implements vscode.Disposable {
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) {
Expand Down Expand Up @@ -230,11 +255,13 @@ export class SerialMonitor implements vscode.Disposable {
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();
}

}
Expand Down
18 changes: 15 additions & 3 deletions src/serialmonitor/serialportctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ interface ISerialPortDetail {
productId: string;
}

export enum SerialPortEnding {
"No line ending",
"Newline",
"Carriage return",
"Both NL & CR",
}

export class SerialPortCtrl {
public static get serialport(): any {
if (!SerialPortCtrl._serialport) {
Expand All @@ -36,10 +43,12 @@ export class SerialPortCtrl {
private _currentPort: string;
private _currentBaudRate: number;
private _currentSerialPort = null;
private _ending: SerialPortEnding;

public constructor(port: string, baudRate: number, private _outputChannel: OutputChannel) {
public constructor(port: string, baudRate: number, ending: SerialPortEnding, private _outputChannel: OutputChannel) {
this._currentBaudRate = baudRate;
this._currentPort = port;
this._ending = ending;
}

public get isActive(): boolean {
Expand Down Expand Up @@ -69,7 +78,7 @@ export class SerialPortCtrl {
this._currentSerialPort = new SerialPortCtrl.serialport(this._currentPort, { baudRate: this._currentBaudRate });
this._outputChannel.show();
this._currentSerialPort.on("open", () => {
this._currentSerialPort.write("TestingOpen", (err) => {
this._currentSerialPort.write("TestingOpen", "Both NL & CR", (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}`);
Expand Down Expand Up @@ -99,7 +108,7 @@ export class SerialPortCtrl {
return;
}

this._currentSerialPort.write(text, (error) => {
this._currentSerialPort.write(text, SerialPortEnding[this._ending], (error) => {
if (!error) {
resolve();
} else {
Expand Down Expand Up @@ -166,4 +175,7 @@ export class SerialPortCtrl {
});
});
}
public changeEnding(newEnding: SerialPortEnding) {
this._ending = newEnding;
}
}
16 changes: 15 additions & 1 deletion vendor/node-usb-native/lib/serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ SerialPort.prototype.isOpen = function() {
return this.fd !== null && !this.closing;
};

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

switch (ending) {
case 'Newline':
buffer = Buffer.concat([buffer, Buffer.from('\n')]);
break;
case 'Carriage return':
buffer = Buffer.concat([buffer, Buffer.from('\r')]);
break;
case 'Both NL & CR':
buffer = Buffer.concat([buffer, Buffer.from('\r\n')]);
break;
default:
break;
}

debug(`write ${buffer.length} bytes of data`);
SerialPortBinding.write(this.fd, buffer, (err) => {
if (err) {
Expand Down