µTasker Forum
September 09, 2010, 06:57:34 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: January 2010: uTasker utFAT version released for M522XX, SAM7X, LM3SXXXX, LPC2XXX and AVR32 - see utFAT board for full details!
Note that Internet Explorer 8 doesn't work with the forum (unless compatibility view is selected); please use Firefox or Chrome (or other well behaved browsers) as alternative!
 
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Changing the address of a Modbus Slave device  (Read 482 times)
mhoneywill
Full Member
***
Offline Offline

Posts: 115


View Profile
« 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 

 
Logged
mark
Global Moderator
Hero Member
*****
Offline Offline

Posts: 1909



View Profile WWW
« 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
Logged
mhoneywill
Full Member
***
Offline Offline

Posts: 115


View Profile
« 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 » Logged
mark
Global Moderator
Hero Member
*****
Offline Offline

Posts: 1909



View Profile WWW
« 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
Logged
mhoneywill
Full Member
***
Offline Offline

Posts: 115


View Profile
« 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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
Copyright © 2010 utasker.com. Webdesign and Hosting by e-future.ch 2010
Valid XHTML 1.0! Valid CSS!
Page created in 0.162 seconds with 18 queries.