You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi guys, what i would like to do is change the SLAVE_ID during the void setup. This because i would like to set the slave address with 5 dip switch. At the moment the SLAVE_ID is declared at the beginning and i don't kwon how to solve this problem.
The SLAVE_DIP is the int variable that i want to use as a SLAVE_ID
Below my code:
//Testato con QModMaster
//https://github.com/yaacov/ArduinoModbusSlave
// https://en.wikipedia.org/wiki/Modbus
#include <ModbusSlave.h>
#include <SoftwareSerial.h>
SoftwareSerial RS485Serial(10, 11); // RX, TX
#define SLAVE_ID 10 // The Modbus slave ID, change to the ID you want to use.
#define SERIAL_BAUDRATE 9600 // Change to the baudrate you want to use for Modbus communication.
// Comment out the following line if your not using RS485
#define RS485_CTRL_PIN 2 // Change to the pin the RE/DE pin of the RS485 controller is connected to.
#define command_open 8
#define command_close 9
#define command_rocking 12
#define feedback_open 3
#define feedback_close 4
#define WatchDog 5
#define DIP_1 A0
#define DIP_2 A1
#define DIP_3 A2
#define DIP_4 A3
#define DIP_5 A4
boolean SET_1;
boolean SET_2;
boolean SET_3;
boolean SET_4;
boolean SET_5;
// Modbus object declaration
Modbus slave(RS485Serial, SLAVE_ID, RS485_CTRL_PIN);
void setup() {
// RS485 control pin must be output
pinMode(2, OUTPUT);
//OUTPUT
pinMode(command_open, OUTPUT);
pinMode(command_close, OUTPUT);
pinMode(command_rocking, OUTPUT);
//INPUT
pinMode(feedback_open, INPUT);
pinMode(feedback_close, INPUT);
pinMode(WatchDog, INPUT);
//DIPSWITCH
pinMode(DIP_1, INPUT);
pinMode(DIP_2, INPUT);
pinMode(DIP_3, INPUT);
pinMode(DIP_4, INPUT);
pinMode(DIP_5, INPUT);
SET_1 = digitalRead(DIP_1);
SET_2 = digitalRead(DIP_2);
SET_3 = digitalRead(DIP_3);
SET_4 = digitalRead(DIP_4);
SET_5 = digitalRead(DIP_5);
int SLAVE_DIP = (SET_1*1)+(SET_2*2)+(SET_3*4)+(SET_4*8)+(SET_5*16)+1;
Serial.println(SLAVE_DIP);
// Register functions to call when a certain function code is received.
slave.cbVector[CB_WRITE_COILS] = writeDigitalOut;
slave.cbVector[CB_READ_DISCRETE_INPUTS] = readDigitalIn;
// Set the serial port and slave to the given baudrate.
RS485Serial.begin(SERIAL_BAUDRATE);
slave.begin(SERIAL_BAUDRATE);
Serial.begin(9600);
}
void loop() {
/* listen for modbus commands con serial port.
*
* on a request, handle the request.
* if the request has a user handler function registered in cbVector.
* call the user handler function.
*/
slave.poll();
}
/**
* Handle Force Single Coil (FC=05) and Force Multiple Coils (FC=15)
* set digital output pins (coils).
*
* Dalla pagina di wikipedia addr: Coil Read-write 1 bit 00001 - 09999
*/
uint8_t writeDigitalOut(uint8_t fc, uint16_t address, uint16_t length) {
Serial.print("FC= ");
Serial.println(fc);
Serial.print("ADDRES= ");
Serial.println(address);
Serial.print("LENGHT= ");
Serial.println(length);
if (address == 3) {
digitalWrite(command_open, slave.readCoilFromBuffer(0));
digitalWrite(command_close, slave.readCoilFromBuffer(1));
digitalWrite(command_rocking, slave.readCoilFromBuffer(2));
}
return STATUS_OK;
}
// Handle the function code Read Input Status (FC=02) and write back the values from the digital input pins (discreet input).
uint8_t readDigitalIn(uint8_t fc, uint16_t address, uint16_t length)
{
Serial.print("FC= ");
Serial.println(fc);
Serial.print("ADDRES= ");
Serial.println(address);
Serial.print("LENGHT= ");
Serial.println(length);
if (address == 0) {
// Write the state of the digital pin to the response buffer.
slave.writeCoilToBuffer(0, digitalRead(feedback_open));
// Write the state of the digital pin to the response buffer.
slave.writeCoilToBuffer(1, digitalRead(feedback_close));
// Write the state of the digital pin to the response buffer.
slave.writeCoilToBuffer(2, digitalRead(WatchDog));
}
return STATUS_OK;
}
Thanks
Pietro
The text was updated successfully, but these errors were encountered:
pietrozanc
changed the title
Change SLAVE ADDRESS in the setup
Change SLAVE ADDRESS in void setup
Oct 29, 2021
pietrozanc
changed the title
Change SLAVE ADDRESS in void setup
Change SLAVE ADDRESS during void setup
Oct 29, 2021
Hi guys, what i would like to do is change the SLAVE_ID during the void setup. This because i would like to set the slave address with 5 dip switch. At the moment the SLAVE_ID is declared at the beginning and i don't kwon how to solve this problem.
The SLAVE_DIP is the int variable that i want to use as a SLAVE_ID
Below my code:
Thanks
Pietro
The text was updated successfully, but these errors were encountered: