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

Change slave ID during setup #50

Closed Ghuru42 closed 4 years ago

Ghuru42 commented 4 years ago

This might be very simple, but I'm stuck. I want to use a digital input where I have a jumper to select if my device is slave id 5 or 6, and I cant figure out how to do this with the modbus init. Anyone got any suggestions?

scottyanke commented 4 years ago

I am doing basically the same thing for address selection, but am using pins and dip switches instead of a jumper. This is my working code on a STM32F103 MCU: Modbus slave(1,0,0); // it needs a non-zero address, but will be changed by the code // The four pins connected to the dip switch for this devices address pinMode(PA0,INPUT_PULLUP); pinMode(PA1,INPUT_PULLUP); pinMode(PA2,INPUT_PULLUP); pinMode(PA3,INPUT_PULLUP); // Read from the dip switches to determine what this devices address is for modbus communications myId = digitalRead(PA0) | (digitalRead(PA1) << 1) | (digitalRead(PA2) << 2) | (digitalRead(PA3) << 3); slave.setID(myId); // set the id. From this point on it will only respond on the modbus to this.