Author Topic: Changing the address of a Modbus Slave device  (Read 19411 times)

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Changing the address of a Modbus Slave device
« on: June 05, 2010, 03:10:16 PM »
Hi Mark,

I have a simple Modbus serial slave device that will have a Modbus address of 250 on start-up, a Modbus master will communicate with it at address 255 and then assign it another Modbus address that it should use until it is power cycled. (When it will return to using an address of 250).

The slave address is defined within the structure ptrMODBUS_pars and is used in the call fnInitialiseMODBUS_port which sets up a Modbus port.

What is the best way to cleanly change a devices address, note this need to happen after the device had replied to the modbus message that caused the change of address, from memory. fnMODBUSPostFunction is not the correct place to handle this as this is called before the reply is sent back.

I could use a gateway as in this post http://www.utasker.com/forum/index.php?topic=670.msg2882#msg2882 but I would like to make use of all the utasker routines to handle different messages, also my slave will only ever have one address its just that it will change.

Cheers

Martin 

 

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: Changing the address of a Modbus Slave device
« Reply #1 on: June 05, 2010, 08:46:57 PM »
Hi Martin

The address (ucModbus_slave_address) is a member of ptrMODBUS_pars. This can be accessed and changed by the MODBUS application at any time.

There is normally a pre- and/or post-call on each message handling so you could presumably use this to mark that a change is required and then make the change after a short delay - after the response has been sent but before a following query can be received.

Regards

Mark

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Re: Changing the address of a Modbus Slave device
« Reply #2 on: June 06, 2010, 02:00:51 PM »
Thanks Mark,

I was worried that changing the address might break something inside the Modbus Engine, but that's not the case :-). I'll also do some testing it might be that the Modbus module does takes its reply address from the transmitted message rather than from the ptrMODBUS_pars table (or can be modified to). This way the message that wrote a register changing the Modbus address would reply with its old address, before then changing the Modbus engine to use the new address.

UPDATE - I can confirm that the Modbus engine, does take its reply address from the transmitted message so, by putting a line of code  in fnMODBUSPostFunction I can safely update the Modbus slave address :-)

One other question, my transmitting device (Master) will normally be in Master mode all the time but I want to be able to switch it to slave mode so that I can do a firmware upgrade, from another Master device. It looks like when reading the Modbus Documentation that a Modbus port might be able to be configured as a Master and a Slave at the same time in the ptrMODBUS_pars table.

        (MODBUS_MODE_RTU | MODBUS_SERIAL_SLAVE | MODBUS_SERIAL_MASTER | MODBUS_RS485_POSITIVE),         // default to RTU mode as master - serial port 0

It could then respond to slave requests directed at it. Is this a correct assumption? Obviously if my device was in firmware upgrade mode it would stop master transmissions. Alternatively if Master and Slave mode are mutually exclusive I presume just changing the port mode in the parameter table would work.  

UPDATE - Initial tests seem to show that you cannot have Master and Slave modes enabled at the same time on the same port, enabling Master mode stops the port responding in slave mode

Another question is if the above were true, and I could have a port in both Master and Slave modes. Then if a slave transaction to my device was in progress and it then started a modbus master send using fnMODBUS_Master_send would this send be queued until after the Slave transmission had finished.

I know this is a bit of a weird way of using the module, but I was just after an idea of how it might react.

Cheers

Martin
« Last Edit: June 06, 2010, 03:01:11 PM by mhoneywill »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: Changing the address of a Modbus Slave device
« Reply #3 on: June 06, 2010, 09:34:42 PM »
Hi Martin

When updating the address there is a small risk involved in case the response is lost. This may cause the master to repeat the request at the old address, which is no longer being used. It may be a detail that can quite easily be solved by moving the master address unconditionally and falling back to the original if there is no response later.

A master will not respond to slave requests since it has other responsibilities - it must always expect responses and not requests. Therefore it is not foreseen that both master and slave modes are set and active() at the same time.

However I think that the mode of a serial MODBUS interface can also be changed by modifying the interface's parameter. I haven't tried this but there is a good chance that it can be changed on-the-fly to either slave or master mode. You may have to check that master timeouts are correct if starting as a slave and moving to a master but the other direction is probably simpler.

The same is not true for a MODBUS TCP interface since its mode determines the initialisation procedure. A master is always the client and the slave the server. Such a change would be much more complicated since these would need to be stopped and reconfigured - not impossible but probably never a real need(?)

Regards

Mark

Offline mhoneywill

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Re: Changing the address of a Modbus Slave device
« Reply #4 on: June 06, 2010, 10:44:12 PM »
Thanks Mark,

I'm happy with living with the possibility that a slave reply might get lost, as I am also controlling the master and can handle retries etc myself.

Didn't get a chance to try switching from Master to Slave today, but its good to hear that there is a good chance that it will work. I'm only working with serial interfaces so TCP is not an issue for me.

Thanks for the reply I'll keep this topic updated with my results

Cheers

Martin