smarmengol / Modbus-Master-Slave-for-Arduino

Modbus Master-Slave library for Arduino
GNU Lesser General Public License v2.1
473 stars 328 forks source link

Write Multiple Coils #60

Open psystein opened 2 years ago

psystein commented 2 years ago

Hello,

Am rather new to the embedded programming world. I come from the LabView Graphical Programming Dimension.

At present Building a Hobby Project. Trying to Control multiple Relays on a ModBus RTU Slave using a Arduino Mega. On the Slave Relay Board, the relays are controller using the Coils assigned to each relay.

The system am building, demands me to turn On Multiple Relays (Write Multiple Coils). However, Even though i have defined a Telegram with the appropriate function code (15) and the required address, the multiple relays don't turn onn.

I digged into the library and found a comment that write multiple coils are yet to be implemented. I tried understanding the code and it looks like the functionality has been implemented.

So am now confused and would appreciate someone to clarify if this library can be used to write multiple coils in a single query.

Thanks.

aleksander1084 commented 2 years ago

Hi, I'm curently working on a modbus library for my device (unfortunately I can't use this library directly due to the specific hardware). But since I decided to use this library as kind of reference maybe I can answer your question.

Implementation of query for Modbus Function 15 (0x0F), Write Multiple Coils is currently implemented, but sice this library uses telegram.au16reg pointer to the array as source of registers/values for all modbus function you have to properly prepare this data before running query. Trick is that Modbus transmits relays in case of this function in specific order 7-0, 15-8, 23-16, 31-24 (I'm not sure is counting from 0 proper in Modbus bus documentation is not exacly clear about it). So if you will prepare data input ptoperly you can use this implementation.