Coder Social home page Coder Social logo

Change SLAVE_ID about arduinomodbusslave HOT 14 CLOSED

yaacov avatar yaacov commented on August 23, 2024
Change SLAVE_ID

from arduinomodbusslave.

Comments (14)

yaacov avatar yaacov commented on August 23, 2024

No, you can add a Modbus::setSlaveID function to update the unitID private var [1] [2].

[1] https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.cpp#L43
[2] https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.h#L93

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

p.s
a pull request is welcome :-)

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

Hello. You do not understand me ... :( I would like to change the value of SLAVE_ID during program operation

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

This is not the fault of the library, but the lack of my knowledge of arduino ..

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

I would like the program to read the SLAVE_ID from the EEPROM memory and then run the Modbus slave (SLAVE_ID, CTRL_PIN); How can I run Modbus slave (SLAVE_ID, CTRL_PIN) in Void Setup or Void Loop?

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

No, the slave-id is set in the constructor [1]

Once it is set, it can not be changed. If you add a new method to the Modbus class [2] that update the private unitID var [3], you will be able to change the unit id (that was set by SLAVE_ID in the constructor) while program is running ( e.g. inside loop or setup)

[1] https://github.com/yaacov/ArduinoModbusSlave/blob/master/examples/simple/simple.ino#L38
[2] https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.h#L73
[3] https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.h#L93

an example of such a method:

/**
 * Set Unit ID of modbus object.
 *
 * @param unitID the modbus slave id.
 */
void Modbus::setUnitID(uint8_t _unitID)
{
    // set modbus slave unit id
    unitID = _unitID;
}

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

Changing CTRL_PIN is more tricky :-) , the easiest way is to create a new modbus object with a new Serial stream [1] and a new ctrl pin.

Check carefully that you must get the ctrl_pin at runtime, it may get a little interesting if you have little experience with arduino.

[1] https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.cpp#L39

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

In ModbusSlave.h i change:
`class Modbus {
public:
Modbus(uint8_t unitID, int ctrlPin);
Modbus(Stream &serial, uint8_t unitID, int ctrlPin);
void begin(unsigned long boud);
int poll();
int readCoilFromBuffer(int offset);
uint16_t readRegisterFromBuffer(int offset);
void writeCoilToBuffer(int offset, int state);
void writeRegisterToBuffer(int offset, uint16_t value);
uint8_t writeStringToBuffer(int offset, uint8_t *str, uint8_t length);
CallBackFunc cbVector[CB_MAX];
void Modbus::setUnitID(uint8_t _unitID)
{
// set modbus slave unit id
unitID = _unitID;
}
private:
Stream &serial;
uint32_t timeout;
uint32_t last_receive_time;
uint16_t calcCRC(uint8_t *buf, int length);

int ctrlPin = -1;
uint8_t unitID;
uint8_t lengthIn;
uint8_t bufIn[MAX_BUFFER];
uint8_t bufOut[MAX_BUFFER];

};
#endif
`

This is ok?

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

it's nicer to add the function in the xxx.c file, and only the header void setUnitID(uint8_t _unitID); in the xxx.h file

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

`
#define CTRL_PIN 4
#define BAUDRATE 9600
#define SLAVE_ID 5

Modbus slave(SLAVE_ID, CTRL_PIN);

void setup()
{
SLAVE_ID = 12; //how i can change? This is no good.. :(
.
.
.
}
`

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

any ideas?

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

once you have the setUnitID method header defined in the .h and body defined in the .c file you can call it slave.setUnitID(12)

from arduinomodbusslave.

g6pl avatar g6pl commented on August 23, 2024

Thank you very much.
Everything works :)
Happy New Year!

from arduinomodbusslave.

yaacov avatar yaacov commented on August 23, 2024

Happy New Year!

Happy New Year 🎉 🎈

from arduinomodbusslave.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.