Skip to content

Commit

Permalink
Update to node-serialport v12
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 committed Oct 3, 2023
1 parent c96eab7 commit 7eacf58
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 43 deletions.
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@types/node": "^14.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/serialport": "^8.0.0",
"@types/w3c-web-serial": "^1.0.2",
"@types/ws": "8.0.0 - 8.5",
"@types/yargs": "^17.0.0",
Expand All @@ -69,7 +68,7 @@
"express": "^4.16.4",
"flatten-svg": "^0.3.0",
"optimize-paths": "^1.2.2",
"serialport": "^9.2.0 || ^12.0.0",
"serialport": "^12.0.0",
"svgdom": "0.1.16",
"wake-lock": "^0.2.0",
"web-streams-polyfill": "^3.0.3",
Expand Down
30 changes: 11 additions & 19 deletions src/__tests__/ebb.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
const { SerialPortMock: SerialPort } = require('serialport')
import { MockBinding } from '@serialport/binding-mock';

jest.doMock('serialport', () => ({
SerialPort: SerialPort,
}));
import {EBB} from "../ebb";
import {SerialPortSerialPort} from "../serialport-serialport";
import { default as NodeSerialPort } from "serialport";
import MockBinding from "@serialport/binding-mock";

(() => {
let oldBinding: any;
beforeAll(() => {
oldBinding = NodeSerialPort.Binding;
NodeSerialPort.Binding = MockBinding;
});
afterAll(() => {
NodeSerialPort.Binding = oldBinding;
MockBinding.reset();
});
})();

describe("EBB", () => {
afterEach(() => {
Expand All @@ -30,9 +22,9 @@ describe("EBB", () => {
it("firmware version", async () => {
const port = await openTestPort();
const ebb = new EBB(port);
(port as any)._port.binding.emitData(Buffer.from('aoeu\r\n'));
((port as any)._port.port).emitData(Buffer.from('aoeu\r\n'));
expect(await ebb.firmwareVersion()).toEqual('aoeu');
expect((port as any)._port.binding.recording).toEqual(Buffer.from("V\r"));
expect((port as any)._port.port.recording).toEqual(Buffer.from("V\r"));
})

it("enable motors", async () => {
Expand All @@ -41,11 +33,11 @@ describe("EBB", () => {
const oldWrite = (port as any)._port.write;
(port as any)._port.write = (data: string | Buffer | number[], ...args: any[]) => {
if (data.toString() === 'V\r')
(port as any)._port.binding.emitData(Buffer.from('test 2.5.3\r\n'))
(port as any)._port.port.emitData(Buffer.from('test 2.5.3\r\n'))
return oldWrite.apply((port as any)._port, [data, ...args])
};
(port as any)._port.binding.emitData(Buffer.from('OK\r\n'));
(port as any)._port.port.emitData(Buffer.from('OK\r\n'));
await ebb.enableMotors(2);
expect((port as any)._port.binding.recording).toEqual(Buffer.from("EM,2,2\rV\r"));
expect((port as any)._port.port.recording).toEqual(Buffer.from("EM,2,2\rV\r"));
})
})
1 change: 0 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ declare module '*.svg' {
}
declare module 'svgdom';
declare module 'wake-lock';
declare module '@serialport/binding-mock';
declare module 'color-interpolate';
declare module 'colormap';

Expand Down
17 changes: 11 additions & 6 deletions src/serialport-serialport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from "events";
import { default as NodeSerialPort } from "serialport";
import { SerialPort as NodeSerialPort } from "serialport";
import { OpenOptions } from "@serialport/bindings-interface"

function readableStreamFromAsyncIterable<T>(iterable: AsyncIterable<T>) {
const it = iterable[Symbol.asyncIterator]();
Expand All @@ -17,6 +18,9 @@ function readableStreamFromAsyncIterable<T>(iterable: AsyncIterable<T>) {
}
}, { highWaterMark: 0 });
}
interface SerialPortOpenOptions extends Omit<OpenOptions, 'parity'> {
parity?: ParityType;
}

export class SerialPortSerialPort extends EventEmitter implements SerialPort {
private _path: string;
Expand All @@ -37,8 +41,9 @@ export class SerialPortSerialPort extends EventEmitter implements SerialPort {
}

public open(options: SerialOptions): Promise<void> {
const opts: NodeSerialPort.OpenOptions = {
const opts: SerialPortOpenOptions = {
baudRate: options.baudRate,
path: this._path,
}
if (options.dataBits != null)
opts.dataBits = options.dataBits as any
Expand All @@ -53,7 +58,7 @@ export class SerialPortSerialPort extends EventEmitter implements SerialPort {
flowControl?: FlowControlType | undefined;
*/
return new Promise((resolve, reject) => {
this._port = new NodeSerialPort(this._path, opts, (err) => {
this._port = new NodeSerialPort(opts, (err: any) => {
this._port.once('close', () => this.emit('disconnect'))
if (err) reject(err)
else {
Expand All @@ -66,7 +71,7 @@ export class SerialPortSerialPort extends EventEmitter implements SerialPort {
this.writable = new WritableStream({
write: (chunk) => {
return new Promise((resolve, reject) => {
this._port.write(Buffer.from(chunk), (err, _bytesWritten) => {
this._port.write(Buffer.from(chunk), (err: Error) => {
if (err) reject(err)
else resolve()
// TODO: check bytesWritten?
Expand All @@ -82,7 +87,7 @@ export class SerialPortSerialPort extends EventEmitter implements SerialPort {
dtr: signals.dataTerminalReady,
rts: signals.requestToSend,
brk: signals.break
}, (err) => {
}, (err: Error) => {
if (err) reject(err)
else resolve()
})
Expand All @@ -96,7 +101,7 @@ export class SerialPortSerialPort extends EventEmitter implements SerialPort {
}
public close(): Promise<void> {
return new Promise((resolve, reject) => {
this._port.close((err) => {
this._port.close((err: Error) => {
if (err) reject(err)
else resolve()
})
Expand Down
10 changes: 6 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import "web-streams-polyfill/es2018"
import express from "express";
import http from "http";
import path from "path";
import { default as NodeSerialPort } from "serialport";
import { PortInfo } from "@serialport/bindings-interface"
import { WakeLock } from "wake-lock";
import WebSocket from "ws";
import { SerialPortSerialPort } from "./serialport-serialport";
import { EBB } from "./ebb";
import { Device, PenMotion, Motion, Plan } from "./planning";
import { formatDuration } from "./util";
import { autoDetect } from '@serialport/bindings-cpp';

export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb") {
const app = express();
Expand Down Expand Up @@ -245,13 +246,14 @@ function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function isEBB(p: NodeSerialPort.PortInfo): boolean {
function isEBB(p: PortInfo): boolean {
return p.manufacturer === "SchmalzHaus" || p.manufacturer === "SchmalzHaus LLC" || (p.vendorId == "04D8" && p.productId == "FD92");
}

async function listEBBs() {
const ports = await NodeSerialPort.list();
return ports.filter(isEBB).map((p) => p.path);
const Binding = autoDetect()
const ports = await Binding.list();
return ports.filter(isEBB).map((p: { path: any; }) => p.path);
}

async function waitForEbb() {
Expand Down

0 comments on commit 7eacf58

Please sign in to comment.