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

Adapter for modbus/more complex protocols #335

Open
mStirner opened this issue Jun 7, 2023 · 0 comments
Open

Adapter for modbus/more complex protocols #335

mStirner opened this issue Jun 7, 2023 · 0 comments

Comments

@mStirner
Copy link
Member

mStirner commented Jun 7, 2023

Its possible to create stream in objectMode=true, which accept a object as write input.
With this its possible to implement more complex/user input based protocols like modbus.

Example:

const { Transform } = require("stream");

class Encode extends Transform {

    constructor(opts) {
        super({
            ...opts,
            objectMode: true
        });
    }

    _transform({
        id = 255,
        addr = 0,
        data = null,
        fnc = Encode.FNC_CODE_4,
        size = 2
    }, enc, cb) {

        addr = Buffer.from(`${addr}`);
        //fnc = Buffer.from(`${fnc}`);
        let crc = Buffer.from([
            0xAB,
            0xCD,
            0xEF
        ]);

        let msg = Buffer.concat([
            Buffer.from([id]),
            fnc,
            Buffer.from([addr]),
            crc
        ]);

        cb(null, msg);

    }

    static FNC_CODE_1 = Buffer.from([0x01]);
    static FNC_CODE_2 = Buffer.from([0x02]);
    static FNC_CODE_3 = Buffer.from([0x03]);
    static FNC_CODE_4 = Buffer.from([0x04]);

}


const encode = new Encode();

encode.on("data", (data) => {
    console.log("Message", data);
});


encode.write({
    addr: 1234,
    size: 2,
    //fnc: Encode.FNC_CODE_1,
    data: Buffer.from("foo bar")
});

See https://github.com/OpenHausIO/playground/tree/main/object-mode-streams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant