Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServerSerial types and usage #550

Merged
merged 6 commits into from
Apr 4, 2024
Merged
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
21 changes: 21 additions & 0 deletions ServerSerial.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as events from 'events';
import { SerialPortOpenOptions } from "serialport"
import { FCallback, IServiceVector } from './ServerTCP';
import { ErrorCallback } from '@serialport/stream';

export class ServerSerial extends events.EventEmitter {
constructor(vector: IServiceVector, options: IServerSerialOptions);
close(cb: FCallback): void;
}

type IServerSerialOptions = SerialPortOpenOptions<any> & {
debug?: boolean,
unitID?: number,
openCallback?: ErrorCallback
}

export declare interface ServerSerial {
on(event: 'socketError', listener: FCallback): this;
on(event: 'error', listener: FCallback): this;
on(event: 'initialized', listener: FCallback): this;
}
6 changes: 3 additions & 3 deletions ServerTCP.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class ServerTCP extends events.EventEmitter {
close(cb: FCallback): void;
}

interface IServiceVector {
export interface IServiceVector {
getCoil?:
((addr: number, unitID: number, cb: FCallbackVal<boolean>) => void) |
((addr: number, unitID: number) => Promise<boolean>) |
Expand Down Expand Up @@ -58,5 +58,5 @@ export declare interface ServerTCP {
on(event: 'initialized', listener: FCallback): this;
}

type FCallbackVal<T> = (err: Error | null, value: T) => void;
type FCallback = (err: Error | null) => void;
export type FCallbackVal<T> = (err: Error | null, value: T) => void;
export type FCallback = (err: Error | null) => void;
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ModbusRTU} from "./ModbusRTU";
export * from "./ServerTCP";
export * from "./ServerSerial";

export default ModbusRTU;
5 changes: 3 additions & 2 deletions servers/serverserial.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class ServerSerial extends EventEmitter {
const optionsWithBindingandSerialport = Object.assign({}, serialportOptions, optionsWithBinding);

// create a serial server
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport);
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport, options.openCallback);

// create a serial server with a timeout parser
modbus._server = modbus._serverPath.pipe(new ServerSerialPipeHandler(optionsWithSerialPortTimeoutParser));
Expand All @@ -253,7 +253,7 @@ class ServerSerial extends EventEmitter {
// remember open sockets
modbus.socks = new Map();

modbus._server.on("open", function() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, can you give some description what this do, and why this change is needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The open event never gets emitted.
I think it may be because of how ServerSerialPipeHandler works?
But the open event from SerialPort isn't piped and so you never receive an initialized event either.

modbus._serverPath.on("open", function() {
modbus.socks.set(modbus._server, 0);

modbusSerialDebug({
Expand All @@ -270,6 +270,7 @@ class ServerSerial extends EventEmitter {
modbus.socks.delete(modbus._server);
});

modbus.emit("initialized");
});

modbus._server.on("data", function(data) {
Expand Down
Loading