Write to Modbus TCP holding registers

Hi
I want to create a ModbusTCP slave(server) using NodeRed where I can write values from my Lorawan gateway so that a PLC can connect to the modbus slave and retrieve the decoded data.

I am trying to write value 1601.5 to modbus holding register 400001 and 40002 what am i a doing wrong ? I have a PC program that connects to the modbus server and reads 40.001 to 40.010 but no value appears.

Functioncode:

var fc=16;
var sa=40001;
var addresses=2;
var value=1601.5;
buf=Buffer.alloc(4);
buf.writeFloatBE(value);
//buf.writeFloatBE(16001.5);
var values=[(buf[0]*256+buf[1]),(buf[2]*256)+buf[3]]
msg.slave_ip="192.168.1.31";
msg.payload={"value":values , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };
return msg;

I dont have much experience with buffer methods .. does this work ?

var fc = 16;
var sa = 1;
var addresses = 2;

var value = 1601.5;
var buf = Buffer.alloc(4);
buf.writeFloatBE(value);
node.warn({ "buf": buf });

node.warn(buf.readInt16BE(2));
node.warn(buf.readInt16BE(0));

var values = [buf.readInt16BE(0), buf.readInt16BE(2)]
//msg.slave_ip = "192.168.1.31";
msg.payload = { "value": values, 'fc': fc, 'unitid': 1, 'address': sa, 'quantity': addresses };

return msg;

The node-red-contrib-buffer-parser package has a buffer maker node that simplifies all this data conversion. You could generate a buffer from your values then send that to a buffer parser node that converts the buffer to int16 values suitable for modbus.

No I get no result on your code either

Thats strange because with my modbus simulator it was ok ..
(by the way .. you can upload images directly on the forum .. no need for an external site)

Are you sure you have the IP of the device correctly configured in the modbus node and that you are on the same network ?
i ask because i noticed you had in your code

and in you last post

image

@Steve-Mcl i was going to recommend your node's but since Johan was already in a Function node i thought of having a go with JS ... Buffer-parser is a must when working with Modbus.

1 Like

Hi I have had success by setting the MODBUS Read node like this. I am reading registers from a VSD. MODBUS can be tricky as some devices do not require the first digit like I am monitoring register 42006 but due to me selecting FC3 it nows that it starts with 40000 so I only needed to put in 2006. When I entered 42006 it was looking at 442006. Sometimes the registers need to be read 1 less than actual so the manual indicated that I need to read 42007 but to read it you need to enter 1 less.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.