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

ServerTCP getMultipleHoldingRegisters and getMultipleInputRegisters don't work with Promises #554

Open
ensehic opened this issue May 13, 2024 · 0 comments

Comments

@ensehic
Copy link

ensehic commented May 13, 2024

I'm having issues implementing async getMultipleInputRegisters and getMultipleHoldingRegisters. See code below.

import { ServerTCP } from 'modbus-serial';

const vector = {
    getMultipleInputRegisters: (addr: number, length: number, unitID: number) => {
        return new Promise((resolve) => {
            resolve([1, 2]);
        });
    },
    getMultipleHoldingRegisters: (addr: number, length: number, unitID: number) => {
        return new Promise((resolve) => {
            resolve([3, 4]);
        });
    }
};

const serverTCP = new ServerTCP(vector, { host: '0.0.0.0', port: 502, unitID: 1 });
serverTCP.on('error', console.log);

When I attempt to read multiple input registers or holding registers (trying to read, say, address 10 and length 2), I receive this error on the client side:

{
    status: 'rejected',
    reason: Error: Modbus exception 4: Slave device failure (device reports internal error)
        at ModbusRTU._onReceive (...)
        at TcpPort.emit (node:events:518:28)
        at TcpPort.emit (node:domain:488:12)
        at Socket.<anonymous> (...)
        at Socket.emit (node:events:518:28)
        at Socket.emit (node:domain:488:12)
        at addChunk (node:internal/streams/readable:559:12)
        at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
        at Socket.Readable.push (node:internal/streams/readable:390:5)
        at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
      modbusCode: 4
    }
}

The error message is rather generic, so I haven't been able to glean anything helpful from it. I have verified that both these methods work when used synchronously. Also all other methods (setCoil, getInputRegister etc.) work fine with Promises. I imagine I'm doing something wrong here, but can't for the life of me figure out what it is. Any help would be appreciated. 🙂

@ensehic ensehic closed this as completed May 14, 2024
@ensehic ensehic reopened this May 14, 2024
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

No branches or pull requests

1 participant