View previous topic :: View next topic |
Author |
Message |
Heath
Joined: 21 Dec 2007 Posts: 41
|
RS485 Design - Master/Slave |
Posted: Fri Dec 28, 2007 9:29 am |
|
|
Howdy folks,
I'm going through a couple designs and was wondering what you guys thought or if you had any suggestions.
My current setup is one controller that will act as a Master and will communicate to all of its possible slaves and the slaves must respond with a temperature status. The slaves address can be configured by hand (something I would like to change). All of these nodes have the same PIC on them: PIC18F6527. The Master will always be talking to all slaves. There should have to be a scenario where a master needs to talk to a particular slave.
Currently, I have two designs:
First, the master will transmit a message and every slave node acts on it and sends a response back. A slave will answer when it is his turn. Slave with an address 1 answers 20ms after the message from the master. Slave 2 answers 40 ms after the message from the master. etc.
Secondly, the master will talk to each node individually. The slave will respond when it is spoken to and only then.
Obviously, there are pros and cons for each. For now, I favor the second approach because it seems very flexible. I am not constrained by that message window. Albeit, the first approach seems to be more suited to meet my needs because I will always have to talk to every node.
The biggest problem I see with the second approach is the slave addressing. There will have to be some extra overhead on the second protocol for the master to figure out what slaves are connected to him and they their addresses are. Then you run into of a slave responding to an equery. When should I respond? This would almost require a protocol like DHCP looking at the MAC address but AFAIK the serial number of the board or PIC is not stored anywhere in the firmware so there isn't a way for me to uniquely identify a board.
Thanks for the comments. |
|
|
yerpa
Joined: 19 Feb 2004 Posts: 58 Location: Wisconsin
|
|
Posted: Fri Dec 28, 2007 10:49 am |
|
|
I think your second approach is better, and it is commonly used in industry, for example: Johnson Controls N2 bus. The master polls the entire range of slave addresses with a simple "Are you there address n?" command. The slave has a short time (in milliseconds) to reply, or else that address will be treated as failed. The master can periodically loop through all available addresses (256 max in N2 bus) to discover new devices. A slave never initiates communication in this scheme.
It all depends how many devices on your bus, the amount of data per device, the polling rate required, and the available baud rate. Good luck, there are a lot of examples out there - google for "rs485 protocol" |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Fri Dec 28, 2007 10:58 am |
|
|
First or Second - why not both?
I often implement a protocol this way, using slave address 0 as a "broadcast" message. Address 1 or more is always directed to a particular slave.
A given message may or may not require a response. If it does, broadcast message responses are sent in a time slot, as you described. Directed message responses occur right away, after a short delay for line turnaround if 2-wire half-duplex.
We use 1 or 2 bcd switches to set the slave id (1-9 or 1-99). Customers are often not comfortabe with hex switches which read 0-F.
Hope this is helpful.
Ken |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Fri Dec 28, 2007 11:00 am |
|
|
I favor the second design but it appears wasteful in the current scenario since the master says the same thing to every slave. If I have 8 slaves the master needs to repeat himself 8 times. (1 to each slave)
I guess I could work out a protocol where the master finds out who is on his bus and keeps a copy that he updates every X seconds. When he sends a message he will attach a delivery order in that message like "Slave A -> Slave C -> Slave Z". Slave A will get the message and pop his name off the list, send a response to the master and send it on to Slave C. Slave C will act accordingly. |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Fri Dec 28, 2007 11:21 am |
|
|
Ken,
I believe my scenario would be where the master is effectively always broadcasting. (i.e. when he has something to say he says the same thing to every node) Given that to be true, it would appear that the first one is the best option even though it has greater limitations and needs to be considering the message size when the windows are framed.
One thing that bugs me about the first design is that I can have one slave and configure it as slave 8 and it will wait a set time before it responds even if it is the only slave. It just appears to me to have design limitation written all over it. :/ |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
master slaves |
Posted: Fri Dec 28, 2007 1:09 pm |
|
|
the safest and simplest way is to talk to each slave by addressing it.
simple and no bus colissions.
joseph |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Sat Dec 29, 2007 5:17 pm |
|
|
The MODBUS protocol supports would support your needs. Network broadcast are supported by sending commands to address 0. All traffic that requires a response is addressed to individual slaves. The protocol is well documented and you should be able to find code examples to work from.
If each device attached to the network has a default address of 1 that is read from EEPROM, you can attach devices one at a time and reassign them to use a different address you don't even need DIP switches or such.
At any rate read the MODBUS specification. Its well thought out and from there you will be better able to figure out what will work for you.
www.modbus.org/docs/PI_MBUS_300.pdf |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun Dec 30, 2007 10:25 pm |
|
|
Ken Johnson wrote: | First or Second - why not both?
I often implement a protocol this way, using slave address 0 as a "broadcast" message. Address 1 or more is always directed to a particular slave.
A given message may or may not require a response. If it does, broadcast message responses are sent in a time slot, as you described. Directed message responses occur right away, after a short delay for line turnaround if 2-wire half-duplex.
Ken |
I often use this technique for supporting distributed data acquisition. The master sends out a "sample all" command to all slaves which effectively synchronizes data acquisition. Each slave then sends to the master in its time slot where time slot one corresponds to slave 1, time slot 2 == slave 2 etc. The master can also address each slave individually using the slave address. This unicast approach can be for data acquisition or to send configuration commands/data, such as sensor specific configuration, to the appropriate slave. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Mon Jan 07, 2008 2:40 pm |
|
|
I've been looking at the modbus driver provided by CCS but I have a few questions on it.
It looks like it will wait 10 MS after it sends a message (at 9600) and check for a response. Am I correct in saying that, according to the MODBUS spec, this isn't correct?
I also don't see a check for the 1.5 delay between characters.
Thanks for the assistance. |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Mon Jan 07, 2008 2:53 pm |
|
|
Hrm. the 10ms wait is just a matter of opinion for a response. that question is answered. Still trying to find the 1.5 delay check. |
|
|
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Jan 08, 2008 4:32 pm |
|
|
Heath wrote: | I've been looking at the modbus driver provided by CCS but I have a few questions on it.
It looks like it will wait 10 MS after it sends a message (at 9600) and check for a response. Am I correct in saying that, according to the MODBUS spec, this isn't correct?
I also don't see a check for the 1.5 delay between characters.
Thanks for the assistance. |
MODBUS protocol requires a slave reply to a masters query after a minumum of 3 byte periods. This pause is required in many implimentations of 485 repeater hardware because the direction of transmition can only change after a pause. At 9600 baud this would be about 3 mS after the message is completely sent. A lot of applications I have worked on have some amount of variation in the time a slave waits before replying I call this jitter. If a slave device always replies after 3 to 5 mS, then assuming that after 10 mS if there has been no reply that there will be no reply is a safe assumption. The master can then sent a new query because the last query sent by the master was not recieved or the CRC was not authinicated by the slave.
If the network saturation is the precent of time that a query or reply is in progress, this would realisticly give you a network saturation of 60% and up depending on packet length. |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Wed Jan 09, 2008 1:04 pm |
|
|
Quote: | One thing that bugs me about the first design is that I can have one slave and configure it as slave 8 and it will wait a set time before it responds even if it is the only slave. It just appears to me to have design limitation written all over it. :/ |
I will read over Modbus when I have 10 minutes - but you could answer the above by having slaves implement collision detection. They only respond to a broadcast when the bus is quiet, and if it's not quiet then back off for a random time (Ethernet style). So the first slave that gets in will be noticed - so if you only have one then everything is fine. |
|
|
|