Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cli/can-flash-serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function canFlashSerial(deviceId: string) {
return false;
}

if (device.vendorId === '0483' && device.productId.toUpperCase() === '374B') {
if (device.vendorId === '0483' && device.productId?.toUpperCase() === '374B') {
// DISCO-L475VG
console.log(SERIAL_PREFIX, 'Detected ST B-L475E-IOT01A board, but failed to read config.');
console.log(SERIAL_PREFIX, 'This can be because the device is not running the right firmware ' +
Expand Down
2 changes: 1 addition & 1 deletion cli/find-serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function findSerial(whichDeviceIndex: number | undefined) {
filteredDevices = allDevices.filter(d => d.path.indexOf('ttyACM') > -1 || d.path.indexOf('ttyUSB') > -1);
}
else {
filteredDevices = allDevices.filter(d => d.manufacturer.indexOf('Standard port types') === -1);
filteredDevices = allDevices.filter(d => d.manufacturer?.indexOf('Standard port types') === -1);
}

if (filteredDevices.length === 0) {
Expand Down
32 changes: 12 additions & 20 deletions cli/serial-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
// eslint-disable-next-line
const serialPort = require('serialport');
import { SerialPort } from 'serialport';
import { PortInfo as SerialPortListItem } from '@serialport/bindings-interface';
import { EventEmitter } from 'events';
import TypedEmitter from 'typed-emitter';
import { ISerialConnector } from '../shared/daemon/iserialconnector';

// Don't open same port twice
let serialPorts: { [index: string]: { message: typeof serialPort } } = { };

interface SerialPortListItem {
path: string;
manufacturer: string;
serialNumber: string;
pnpId: string;
locationId: string;
vendorId: string;
productId: string;
}
let serialPorts: { [index: string]: SerialPort } = { };

export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
connected: () => void;
Expand All @@ -29,14 +20,14 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
close: () => void;
}>) implements ISerialConnector {
static async list() {
return (await serialPort.list()) as SerialPortListItem[];
return (await SerialPort.list()) as SerialPortListItem[];
}

private _connected: boolean;
private _echoSerial: boolean;
private _path: string;
private _baudrate: number;
private _serial: typeof serialPort;
private _serial: SerialPort | null = null;
private _dataHandler: (a: Buffer) => void;

constructor(path: string, baudrate: number, echoSerial: boolean = false) {
Expand Down Expand Up @@ -72,7 +63,7 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
this._serial = serialPorts[this._path];
}
else {
this._serial = new serialPort(this._path, { baudRate: this._baudrate });
this._serial = new SerialPort({ path: this._path, baudRate: this._baudrate });
serialPorts[this._path] = this._serial;

this._serial.on('close', () => {
Expand All @@ -91,17 +82,17 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{

// otherwise wait for either error or open event
return new Promise<void>((resolve, reject) => {
this._serial.once('error', (ex: any) => {
this._serial?.once('error', (ex: any) => {
this._serial = null;
delete serialPorts[this._path];
reject(ex);
});
this._serial.once('open', () => {
this._serial?.once('open', () => {
this._connected = true;

this.emit('connected');

this._serial.on('error', (ex: any) => this.emit('error', ex));
this._serial?.on('error', (ex: any) => this.emit('error', ex));

resolve();
});
Expand All @@ -123,7 +114,7 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
}

async setBaudRate(baudRate: number) {
await this._serial.update({
await this._serial?.update({
baudRate: baudRate
});
this._baudrate = baudRate;
Expand All @@ -134,14 +125,15 @@ export class SerialConnector extends (EventEmitter as new () => TypedEmitter<{
}

async disconnect() {
if (!this._serial) throw new Error('Serial is null');
this._serial.off('data', this._dataHandler);
return true;
}

async getMACAddress() {
let list = await SerialConnector.list();
let l = list.find(j => j.path === this._path);
return l ? l.serialNumber : null;
return l?.serialNumber ?? null;
}

async hasSerial() {
Expand Down
Loading