Setters within a library not working

Hi all,

Having some issues with some code. I’ve modified the modbus master library to have some getter() and setter() methods, so I can construct a blank ModbusMaster, like ModbusMaster node() and then provide it with paramters using the setter methods.

The problem is, the setter methods currently don’t seem to work.

Let’s take these snippets.

DigiRail2A.cpp:

I create a new instance of this in my top level, like so:

DigiRail2A digiR(1, SERIAL_8E1, A5, 4800, 245);

DigiRail2A.cpp looks like this:

ModbusMaster node();
uint16_t baudRate = 4800;

DigiRail2A::DigiRail2A(uint8_t _serialPort, uint32_t _serialConfig, uint8_t _txPin,  uint16_t _baudRate, uint8_t _address) {
  node.setSerialPort(_serialPort);
  node.setSerialConfig(_serialConfig);
  node.setSlaveAddress(_address);
  baudRate = _baudRate;
  node.enableTXpin(_txPin);
}

float DigiRail2A::readValue(uint8_t channel, float lowLimit, float highLimit) {

  Serial.print("[readValue]: readValue called for Channel ");
  Serial.print(channel);
  Serial.print(" on Serial Port ");
  Serial.print(node.getSerialPort());
  Serial.print(", with Baud set at ");
  Serial.print(baudRate);
  Serial.print(", and Slave Address ");
  Serial.println(node.getSlaveAddress());
.
.
.

When I call DigiRail2A.readValue(....), the prints that I’ve put at the top of this message only display the default values that ModbusMaster assigns to the slave address (etc.), and NOT what I’ve just set it to in the constructor for DigiRail2A.

Within ModbusMaster.cpp, my getters/setters all follow the same pattern, i.e.:


uint8_t  _u8MBSlave = 1;			
.
.
.
void ModbusMaster::setSlaveAddress(uint8_t u8MBSlave){
	_u8MBSlave = u8MBSlave;
}

uint8_t ModbusMaster::getSlaveAddress() {
	return _u8MBSlave;
}
.
.
.

Can anyone help? I’m not sure what I’m doing wrong here. I’ve tried to strip out the relevant parts of code, as not to C+P large chunks, but let me know if any more would help! I’m not sure whether it’s the setters not working, or whether my init. of ModbusMaster node() doesn’t get updated properly?

Thanks

Let me ping someone that might be able to help, @rickkas7 or @blave

are you able to assist?

Thanks @KyleG

Try: digiR.readValue rather than DigiRail2A.readValue.

1 Like

Thanks @timx - I am actually doing this, with an instance that I created. Simply wrote DigiRail.readValue to demonstrate the function call I was making.

Thanks.