Search code examples
javascripttcpmodbusmodbus-tcp

Modbus serial implementation


I'm trying to write registers using modbus-serial library. This is my code:

    const ModbusRTU = require("modbus-serial");
    
    const gatewayIpAddress = "XXX.XXX.XXX.XXX";
    const tcpPort = 502;
    const unitId = 1;
    
    const storePallet = async (shuttle) => {
        console.log("Storepallet function");
        console.log("ShuttleNumber: " + shuttle);
        const client = new ModbusRTU();
    
        try {
            await client.connectTCP(gatewayIpAddress, { port: tcpPort });
            console.log("Connected to Radioshuttle device over Modbus TCP");
    
            // Write store request
            await client.setID(unitId);
            await client.writeRegister(0, 1); // Address 0 represents tag #4 BIT 02.0.1
    
            console.log("Pallet stored successfully");
            client.close();
        } catch (error) {
            console.error("Error storing pallet:", error);
            client.close();
            throw error;
        }
    };
    
    storePallet(1).catch((error) => {
        // Handle the error here
        console.error("Unhandled error:", error);
    });

The problem is that the device is not responsive to this code, here is the documentation:

Set up of the connecting Modbus TCP Client (Master)
General
RS Gateway IP Address: XXX.XXX.XXX.XXX
TCP Port: 502
Unit ID / Slave address: 1
Read Radioshuttle output data.
Function code: 04 – Read Input Registers
Read Data Start address: 1
Read Data Size: 10
Write Radioshuttle input data.
Function code: 16 – Write Multiple Registers
Write Data Start address: 1
Write Data Size: 5

Registers - RS Input tags:

Register 1 and 2

and for the store request:

Tag # Format Address Name
4 BIT 002.0.1 Store request

Description:
Set to 1 by MCS to initiate the command: Store pallet in racking.
Set to 0 by MCS, when RS reply with RS Output tag # 5 set to 1.

Am I doing something wrong?


Solution

  • It seems that i have been working wrong with the registers, as simply changing the code to this for store pallet command worked:

     const ModbusRTU = require("modbus-serial");
        
        const gatewayIpAddress = "XXX.XXX.XXX.XXX";
        const tcpPort = 502;
        const unitId = 1;
        
        const storePallet = async (shuttle) => {
            console.log("Storepallet function");
            console.log("ShuttleNumber: " + shuttle);
            const client = new ModbusRTU();
        
            try {
                await client.connectTCP(gatewayIpAddress, { port: tcpPort });
                console.log("Connected to Radioshuttle device over Modbus TCP");
        
                // Write store request
                await client.setID(unitId);
                await client.writeRegister(1, 2);
        
                console.log("Pallet stored successfully");
                client.close();
            } catch (error) {
                console.error("Error storing pallet:", error);
                client.close();
                throw error;
            }
        };
        
        storePallet(1).catch((error) => {
            // Handle the error here
            console.error("Unhandled error:", error);
        });