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

RTU over socket (second PR) #514

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

1038642892
Copy link
Contributor

The previous PR was undone because of a bug, happy to submit patches to work rather than undoing next time.

I've reproduced the issues, tested and issued a patch on this branch, with the help of information provided by @Jalle19.

1038642892 and others added 9 commits August 20, 2023 09:14
* Modbus RTU over TCP support (no MBAP header)

* fixing test issues

* test file

* Added missing server functions.

---------

Co-authored-by: 1038642892 <1038642892@users.noreply.github.com>
Bump version to fix an issue on ARM platforms
@Jalle19
Copy link

Jalle19 commented Sep 6, 2023

I'll try to find some time to test this soon

@Caitianci433
Copy link

Hello,
When I set the timeout and read it twice

client.setTimeout(500);
await client.readHoldingRegisters(0,10);
await client.readHoldingRegisters(0,10);

It's time out!

I think there may be a problem with the following code

this._port._transactionIdWrite += 1;

@@ -903,7 +907,9 @@ class ModbusRTU extends EventEmitter {
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);

this._port._transactionIdWrite += 1;

Choose a reason for hiding this comment

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

I think this line should be removed

@1038642892
Copy link
Contributor Author

Hello, When I set the timeout and read it twice

client.setTimeout(500); await client.readHoldingRegisters(0,10); await client.readHoldingRegisters(0,10);

It's time out!

I think there may be a problem with the following code

this._port._transactionIdWrite += 1;

Do you have a full example please? Here's what I've just tried.

const ModbusRTU = require("../index");

const modbusClient = new ModbusRTU()
modbusClient.setID(1)
modbusClient.setTimeout(500) // 5 seconds

modbusClient.connectRTUBuffered("/dev/pts/5", {
    debug: true,
    baudRate: 19200,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
}, read);

async function read() {
    await modbusClient.readHoldingRegisters(0,10)
        .then(console.log)
        .catch(console.err);
    await modbusClient.readHoldingRegisters(0,10)
        .then(console.log)
        .catch(console.err);
}
/* eslint-disable no-console, no-unused-vars, spaced-comment */

// create an empty modbus client
const ModbusRTU = require("../index");

const holdingRegisters = {0: 1, 1: 2, 2: 3};
const coils = {};
const inputRegisters = {};
const discreteInputs = {};

const vector = {
    getInputRegister: function(addr) {
        return inputRegisters[addr];
    },
    getMultipleInputRegisters: function(startAddr, length) {
        const values = [];
        for (let i = 0; i < length; i++) {
            values[i] = inputRegisters[startAddr + i];
        }
        return values;
    },
    getDiscreteInput: function(addr) {
        return discreteInputs[addr];
    },
    getHoldingRegister: function(addr) {
        return holdingRegisters[addr];
    },
    setRegister: function(addr, value) {
        holdingRegisters[addr] = value;
        return;
    },
    getMultipleHoldingRegisters: function(startAddr, length) {
        const values = [];
        for (let i = 0; i < length; i++) {
            values[i] = holdingRegisters[startAddr + i];
        }
        return values;
    },
    getCoil: function(addr) {
        return coils[addr];
    },
    setCoil: function(addr, value) {
        coils[addr] = value;
        return coils[addr];
    },
    readDeviceIdentification: function() {
        return {
            0x00: "MyVendorName",
            0x01: "MyProductCode",
            0x02: "MyMajorMinorRevision",
            0x05: "MyModelName",
            0x97: "MyExtendedObject1",
            0xab: "MyExtendedObject2"
        };
    }
};

// set the server to answer for modbus requests
console.log("server listening...");
const serverTCP = new ModbusRTU.ServerSerial(vector,
    {
        port: "/dev/pts/6",
        debug: true,
        unitID: 1
    },
    {
        baudRate: 19200,
        dataBits: 8,
        stopBits: 1,
        parity: "none"
    }
);

serverTCP.on("initialized", function() {
    console.log("initialized");
});

serverTCP.on("socketError", function(err) {
    console.error(err);
    serverTCP.close(closed);
});

function closed() {
    console.log("server closed");
}

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

Successfully merging this pull request may close these issues.

None yet

4 participants