Sign in to follow this  
pearsonben98

Modbus write multiple registers

Recommended Posts

Hi, is it possible to write to multiple holding registers at once, kind of in the same way you can read from multiple registers at the same time?

for example, device.mydevice.readHolding32(Station, starting address, datapoints) where datapoints decides how many registers you read from. Is it possible to achieve a similar thing with the write function, such as passing an array containing all values to be added to each register?

Currently I am having to write "device.mydevice.writeholding32(station, starting address, data) multiple times.

Cheers,

Ben

Share this post


Link to post
Share on other sites

You can definitely do it through script.  Just pass an array for data points.  So for example:

device.mydevice.setRegisterS32(1, 100, {1,2,3})

which would set register 100/101 to 1, 102/103 to 2, and 104/105 to 3 (register pairs because of the S32 of course).  Note that it is "Set" not "Write", and is "Register" not "Holding" because Input Registers can't be set, so there is no reason to differentiate on the output side.

Note that with channels, DAQFactory will automatically combine reads of the same type into a minimum number of calls if they are consecutive and the same data type.  It will not fill in blanks, though, so sometimes its best to create dummy channels.  That said, on the output side you cannot do multiple outputs with one query through channels because there is no way to set multiple channels simultaneously.

Share this post


Link to post
Share on other sites

Hi, thanks for the advice, this works properly now.

However, is there a maximum sized array that can be passed to the datapoints field? I currently have a datapoints array with 32 addresses, but i need to write:

device.myDevice.setRegisterS16(station, starting_address, data[0,16])

device.myDevice.setRegisterS16(station, starting_address, data[17,31])

 

I am unable to simply do device.myDevice.setRegisterS16(station, starting_address, data).

 

Cheers,

Ben

Share this post


Link to post
Share on other sites

There is a limit defined in the Modbus spec.  I don't remember off hand what it is, but I think it is 63 bytes of data for RTU, and each register is 2 bytes.  But don't take my word for it on the exact number.  The point is that it is a Modbus limit, not DAQFactory, though DAQFactory enforces it.  Also note that some devices don't support setting of multiple registers.  This is not your case, as it is obviously working with 16 registers.  I simply mention it for others that may have issues trying to set more than one at a time.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this