Skip to content

Commit 0029922

Browse files
committed
Update node-serial to 11.0.1
1 parent 799f459 commit 0029922

File tree

5 files changed

+403
-881
lines changed

5 files changed

+403
-881
lines changed

cli/can-flash-serial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function canFlashSerial(deviceId: string) {
1515
return false;
1616
}
1717

18-
if (device.vendorId === '0483' && device.productId.toUpperCase() === '374B') {
18+
if (device.vendorId === '0483' && device.productId?.toUpperCase() === '374B') {
1919
// DISCO-L475VG
2020
console.log(SERIAL_PREFIX, 'Detected ST B-L475E-IOT01A board, but failed to read config.');
2121
console.log(SERIAL_PREFIX, 'This can be because the device is not running the right firmware ' +

cli/find-serial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function findSerial(whichDeviceIndex: number | undefined) {
2020
filteredDevices = allDevices.filter(d => d.path.indexOf('ttyACM') > -1 || d.path.indexOf('ttyUSB') > -1);
2121
}
2222
else {
23-
filteredDevices = allDevices.filter(d => d.manufacturer.indexOf('Standard port types') === -1);
23+
filteredDevices = allDevices.filter(d => d.manufacturer?.indexOf('Standard port types') === -1);
2424
}
2525

2626
if (filteredDevices.length === 0) {

cli/serial-connector.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
// tslint:disable: no-unsafe-any
22

33
// tslint:disable-next-line
4-
const serialPort = require('serialport');
4+
import { SerialPort } from 'serialport';
5+
import { PortInfo as SerialPortListItem } from '@serialport/bindings-interface';
56
import { EventEmitter } from 'events';
67
import TypedEmitter from 'typed-emitter';
78
import { ISerialConnector } from '../shared/daemon/iserialconnector';
89

910
// Don't open same port twice
10-
let serialPorts: { [index: string]: { message: typeof serialPort } } = { };
11-
12-
interface SerialPortListItem {
13-
path: string;
14-
manufacturer: string;
15-
serialNumber: string;
16-
pnpId: string;
17-
locationId: string;
18-
vendorId: string;
19-
productId: string;
20-
}
11+
let serialPorts: { [index: string]: SerialPort } = { };
2112

2213
export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
2314
connected: () => void;
@@ -26,14 +17,14 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
2617
close: () => void;
2718
}>) implements ISerialConnector {
2819
static async list() {
29-
return (await serialPort.list()) as SerialPortListItem[];
20+
return (await SerialPort.list()) as SerialPortListItem[];
3021
}
3122

3223
private _connected: boolean;
3324
private _echoSerial: boolean;
3425
private _path: string;
3526
private _baudrate: number;
36-
private _serial: typeof serialPort;
27+
private _serial: SerialPort | null = null;
3728
private _dataHandler: (a: Buffer) => void;
3829

3930
constructor(path: string, baudrate: number, echoSerial: boolean = false) {
@@ -68,7 +59,7 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
6859
this._serial = serialPorts[this._path];
6960
}
7061
else {
71-
this._serial = new serialPort(this._path, { baudRate: this._baudrate });
62+
this._serial = new SerialPort({path: this._path, baudRate: this._baudrate });
7263
serialPorts[this._path] = this._serial;
7364

7465
this._serial.on('close', () => {
@@ -87,17 +78,17 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
8778

8879
// otherwise wait for either error or open event
8980
return new Promise<void>((resolve, reject) => {
90-
this._serial.once('error', (ex: any) => {
81+
this._serial?.once('error', (ex: any) => {
9182
this._serial = null;
9283
delete serialPorts[this._path];
9384
reject(ex);
9485
});
95-
this._serial.once('open', () => {
86+
this._serial?.once('open', () => {
9687
this._connected = true;
9788

9889
this.emit('connected');
9990

100-
this._serial.on('error', (ex: any) => this.emit('error', ex));
91+
this._serial?.on('error', (ex: any) => this.emit('error', ex));
10192

10293
resolve();
10394
});
@@ -119,7 +110,7 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
119110
}
120111

121112
async setBaudRate(baudRate: number) {
122-
await this._serial.update({
113+
await this._serial?.update({
123114
baudRate: baudRate
124115
});
125116
this._baudrate = baudRate;
@@ -130,14 +121,15 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
130121
}
131122

132123
async disconnect() {
124+
if (!this._serial) throw new Error('Serial is null');
133125
this._serial.off('data', this._dataHandler);
134126
return true;
135127
}
136128

137129
async getMACAddress() {
138130
let list = await SerialConnector.list();
139131
let l = list.find(j => j.path === this._path);
140-
return l ? l.serialNumber : null;
132+
return l?.serialNumber ?? null;
141133
}
142134

143135
async hasSerial() {

0 commit comments

Comments
 (0)