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

How work with two slave #41

Closed svetlitsky closed 5 years ago

svetlitsky commented 5 years ago

Hello! I can not understand and in the examples there is no option when the master works with two or more slave devices. If there are examples - I will be grateful for the help.

smarmengol commented 5 years ago

Hi, As a "quick and dirty" example, I would suggest you to modify the current advanced Master and declare the second telegram for another slave:

// telegram 0: read registers telegram[0].u8id = 1; // slave address telegram[0].u8fct = 3; // function code (this one is registers read) telegram[0].u16RegAdd = 0; // start address in slave telegram[0].u16CoilsNo = 4; // number of elements (coils or registers) to read telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino

// telegram 1: read a single register from another slave telegram[1].u8id = 2; // another slave address telegram[1].u8fct = 3 // function code telegram[1].u16RegAdd = 0; // start address in slave telegram[1].u16CoilsNo = 4; // number of elements (coils or registers) to read telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino

The rest of the code would be the same. Try it, because this simple works.

Cheers,

smarmengol commented 5 years ago

If you need further information about connecting several devices through RS-485, please have a look at my blog:

https://arduino-experience.blogspot.com/

svetlitsky commented 5 years ago

Thanks for the tip! For a complete understanding, need to specify that to exchange different arrays for different slaves, you must specify the names of the arrays with a shift in the elements of the telegram. Be sure to specify the total number of exchange elements in the line "if (u8query> 10) u8query = 0;" where 10 is the total number of elements for all slaves.

Many thanks for the library, it works fine!

Svetlitsky Sergey svetlitsky.sv@gmail.com +7 (912) 4872526

26 февр. 2019 г., в 2:27, samuel notifications@github.com написал(а):

If you need further information about connecting several devices through RS-485, please have a look at my blog:

https://arduino-experience.blogspot.com/ https://arduino-experience.blogspot.com/ — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino/issues/41#issuecomment-467190009, or mute the thread https://github.com/notifications/unsubscribe-auth/ARkE0VB2fvVrryOoUzyO2PILk5Htx64Sks5vRFUwgaJpZM4bP3k5.