0

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?

Brits
  • 14,829
  • 2
  • 18
  • 31
Deesak
  • 153
  • 1
  • 16
  • 1
    Can you please show us the full output that results from running your code (would be worthwhile trying a read to see if that works). It would also be useful if you confirm that you can make the change using a tool like [mbpoll](https://github.com/epsilonrt/mbpoll)/[modpoll](https://www.modbusdriver.com/modpoll.html) to confirm the device works as expected (some info on the device would also be helpful; its not really clear what "Address 02.0.1" means). – Brits Jul 05 '23 at 01:58
  • Hello, the code returns nothing, it just runs succesfully. I have never used those tools, but next time i can try it. It is a radioshuttle that stores pallets into a storage, i was trying to initiate store request with the code provided, but unsuccessfuly. When i changed the code to .writeRegisters(1,1) the shuttle started to go forward, so i think it is connected but the writing of the registers is not correct. – Deesak Jul 10 '23 at 08:44
  • Ok sounds like your code is working (Modbus command is being sent). Unfortunately without more info on the device (model, ideally a link to a manual) we can't help with the registers (every device is different). – Brits Jul 10 '23 at 08:49
  • I would like to share the documentation, but its not online anywhere. Here is a we transfer link to it: we.tl/t-Coe6Awkgvd I dont know how to share it other way, so hopefully this is fine. – Deesak Jul 10 '23 at 09:22
  • 1
    OK, so a home request requires writing register 2 with bit 0 of byte 0 set (I think!). So `.writeRegisters(1,1)` is probably sending a "Home Request" (there is some guess work here, it's much easier to follow manuals that give an example with the wire format!). I'd expect `.writeRegisters(1,2)` to be a Store request and `.writeRegisters(1,4)` a retrieval request. Note that you probably need to subtract 1 from the "REG", as I have done, to get the value needed by `modbus-serial` (PDU vs register numbering if you really want to know!). – Brits Jul 10 '23 at 20:23
  • Thank you for editing the question as well as for your answer, im not very experienced with stack so I appreciate the help. The documentation is the only thing they gave me. I dont have the device with me so next time i will try your suggested edits and hopefully it will work. I will update you after the tests. – Deesak Jul 11 '23 at 10:19
  • Thank you for your advice, the changes worked and shuttle is listening to commands. Now I will try to work on reading output tags. – Deesak Jul 25 '23 at 07:22

1 Answers1

0

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);
    });
Deesak
  • 153
  • 1
  • 16