For instance Profibus-DP. Usually one master is used for real-time data, while the other is mainly used for configuration. A token is passed between masters, so each master knows when it is allowed to access the slaves.
What do you really want? That is what is your "efficiency" criterion. For embedded system a lot of folks consider worst case delay. For normal design worst case is when all nodes have data to transmit. Clearly, "last node" have to wait on all previous nodes. So, best "efficiency" would be sum of transmit times for data. On top ot that you have various overhead: bus turnaround times, time for polling or token passing, possible timeout in case of fixed time slots. AFAICS polling messages from master to slave may be very small, so they should at most double time: you have twice as many messages compared to case when each node "magically" knows when to transmit. You can play some tricks, like group poll, when nodes are ordered (numbered) and each node in the group is supposed to send its data, starting from first. Of course, it means that each node must be capable of detecting end of transmission from lower numbered node. Token passing may be slightly more efficient, as data messages may serve double duty of passing token to next node: so you save cost of polling messages.
But you basically fight with constant factor of 2. If you need more improvement, then you need bigger change. Say faster links or star topology. In star topology intermediate nodes may aggregate messages, so you can significantly decrease number of messages going to maser node. And it is easier to provide fast links to limited number of "hub" nodes, than to have fast links everywhere. But as downside, there is more complexity and potentially lower reliability...
Suppose you have a bus with 20-30 nodes: one master is the central unit, the other are slaves that get a code from a keypad. When a code is typed on the keypad, it is transmitted to the master. If it is ok, master sends an ack to the slave that get the code so it enabled an electric lock and the door is unlocked.
With a polling scheme, master must sends a poll request continuously and the the slaves answer, most of the time, with no data. It appears to me a very inefficient approach.
What's the worst case, i.e. the delay between pressing OK button on the keypad and the door opening? It is the sum of 30 (number of slaves) polling requests and replies. If a single request takes 100ms, the worst case should be around 3 seconds. I'd like to reduce this time to 100ms.
It is impossible with polling approach. However the bus is idle (without real data) most of the time. I could avoid polling at all and instruct the slave to transmit when it wants. I think the probability of a conflict with another slave is very low and I could reach the requirement of 100ms above. However I sholud design the behaviour with a conflict, even rare, happens.
The Problem is that RS485 is not designed with this sort of architecture in mind. Yes,you could build a protocol which the slave listens first and makes sure no one else is talking, Then send the message (with good error detection built in) and see if it gets a response (maybe first an ACK and then later an order to go ahead). If you don't get the ack, you presume that your message got garbled so you send again after some random wait designed that you and whoever you conflicted with won't send again at the same time. MAybe even the master sends a NAK that it got garbage.
Much better would be use a driver that can reliably detect a conflict with known results. You send your message and listen for conflicts. If you get a conflict you stop sending immediately.
If the system uses a driver where say 0 always wins over a 1, and the bit time is longer than the network transmission time, then if two drivers start at the same time, whoever send the first 0 when the other wants to send a 1 will win and get their message through. The other backs off, waits for the message and ack to clear, then does their message.
100ms for round trip looks very long. You should be able to do with very short (3-4 bytes, maybe 8 if you add strong cryptography) messages, so at 9600 even with delays for turnaround you should be closer to 20-30 ms.
And I do not understand why you wait for OK? Typing on keypad takes some time and it is likely that node will be polled before number is finished. Then node can ask for polling with shorter delay. If only small number of nodes asks for more frequent polling, then master can respect this giving you shorter effective response time. If several (most of) keypads are busy, then clearly master can not quickly serve all of them, but that is exactly situation where nodes talking without polling would lead to trouble.
You can bring down average, but worst case will be bad: in case of conflict you need enough delay to significanlty reduce risk of repeated conflict. And detection of conflicts may be problematic. You may sell more, as during demo salesman can arrange for no conflicts and clients will see best case. But you may get bad publicity, as ocasional, much longer than usual delay will look like malfunction.
Does the user have to press a single key or enter a multidigit access code ?
In the latter case, do the access code processing locally including feedback beeps and only send a complete sequence to the master. When the user enters the first digit, answer to the master poll "data will soon be arriving" i.e. there is a user at the slave. The master can then poll those slaves more frequently which have users at the slave and hence read the full access code with the frequent polls.
The wire network must be huge, if you are forced to use 300 bits/s:-)
If you can use 9600 bits/s i.e. 1 ms character time and a 2 byte poll and 1 byte Ack/Nak that takes 3 ms plus some additional time for Rx/Tx and Tx/Rx delays. I would expect a 5 ms transaction time and 150 ms poll cycle,
It sounds that you are trying to reinvent the CAN bus protocol ?
You can even use RS-485 transceivers chips (Data out to Transmit enable, Tx input to fixed "0"). You need a CAN control chip, e.g. SJA1000 for the master and some simpler for slaves. There are several uCs with built in CAN controllers usable as slaves.
With CAN, there are some snags. The maximum net payload is 64 _bits_ (8 bytes) so if more data needs to be transmitted, you may have to use some higher level protocols above CAN. The other snag is that all stations must hear each other well within a single _bit_ time to handle conflicts. This limits the length of the bus and/or maximum bit rate that can be used e.g. 250 kbit/s with 250 m bus cable. At lower speeds the range can be extended.
That methodology is used in CAN, it is also used in I2C, and I suspect other systems. It is close to the method used for ethernet on coax (but that uses a slightly different method because the line might be longer than allowing collision detection in a single bit.
The difference is that in coaxial Ethernet CSMA/CD collisions _both_ messages are lost and _both_ needs to be retransmitted at a later time. In CAN bus, the higher priority message gets through despite a collision, only the lower priority message needs to be retransmitted. For this reason a much higher bus loading are possible in CAN, while in Ethernet high collision rates will kill the traffic completely much earlier.
The dominant/recessivebit protocol used in CAN may also be used with other transports than RS-485 or dedicated CANbus transceivers. One could even use with 20 mA current loop by putting all stations into the sane current loop, but the optoisolator photo transistor Vce(max) may limit the number of stations in the same loop to about a dozen.
Yes, the Ethernet protocal loses the whole message because the bit time isn't large comparared to the propagation delay across the system, so you might not know of the collision until it is too late to get off.
CAN/I2C require a bit time longer than network propagation, so the transmitter knows after the first collision that it has lost, and the master that was sending the dominant bit gets control.
As you say, you can use dominant/recessive bit physical layer with many protocals, even pure asynchronous serial (though that may need a bit of external logic to get you off the bus before you corrupt the message, like the 485 with data on the enable.
That will never be possible. If 30 those slaves simultaneously trigger a request, then at least one of them will have to wait for the communication to all the others to be done before it can get its reply.
The worst case for a bus of 30 slaves cannot possibly be exactly as quick as the optimum case for a single slave.
The only way that could work is if there is no bus at all, but rather a star topology: each slave has its own, dedicated connection to the master. I'll assume that this untenable because of the cabling costs.
That makes no sense at all. If the bus is so idle, why are you polling so slowly? Why would you poll at any less than the maximum possible communication rate, while at the same being concerned about too long a response time?
But I'll second upsidedown's hint: you appear to be trying rather unnecessarily hard to reinvent CAN.
On thing to note, if you are worried about WORSE case performance even under full load, the synchronous polling is probably the best option. If every keypad got the last key press at exactly the same time, the shortest maximum delay would have the master go at full speed and interrogate each slave and process it. This eliminates all the loses due to conflicts and re-transmitting.
If you do use a CAN bus style arbitration, and can make the protocol not lose frames in the case of a conflict, and the 'high priority' slaves will wait long enough between mesages to give everyone a chance to send their message, then that might get a bit faster as the amount thqt needs to be sent is less, but the protocol overheads to wait to see the bus is idle might eat that up.
The asyncronous messaging with arbitration is probably faster at 'average' delay under lighter loads. This is just the way these things tend to work.
Another option is to use a dominate/recessive bus drive and the master sense out a query request, and with a fixed timing any slave that has a request replies with its address, and sees if someone else answer too, and drops out on conflict. (This method is used on the SMB Bus). The master can then poll at a base rate, and then if it gets a reply, immediate ask for more requests until it gets a no request pending answer.
Many thanks to all of you that spent some time to answer my question.
Some additional details. The example I wrote is... just an example and not exactly the real application. The system has a central unit and many slaves installed in different places of a building (imagine a keypad, a sensor and so on). This is a home/building automation application.
After first connection the master sends many configuration data to slaves (around 2-4kB), so CAN bus is not ideal because of its small packets (however I can split data on different packets). Then the bus stays idle for most of the time. The user could interact with a slave and only in that case the slave has something to tell to the master.
Yes, you are right when you say that putting a known limit on the worst case can be done with a polling approach. However my goal is not limit the worst case, but minimizing the "normal"/mean delay in the slave->master->slave transaction. I know this mean of delays can be reduced a lot, because the bus is idle most of the time, so the probability of a conflict is very very low.
Even number of slaves was just an example. I can think of systems with
50 or 100 slaves and in those cases the polling approach performance would be very bad.
Do you know of real wired cable used in home/building automation? I heard about KNX on twisted pair, but I think the complexity and cost of the system would increase very fast.
Splitting up the network may also make your cabling easier. If you use a single 485 bus for the entire building, your single cable has to pass every node and has to go in and out every room. And when installing additional nodes, they have to be placed in te same loop. Cable length will also be an issue with such a setup. A single break in the cable disables the entire network and you have no easy way to determine where to start looking for the fault.
With a single controller with multiple ports, you can poll all ports simultaneously.
You can also create a tree network, but that requires additional branch controllers. Have for example a central controller connected to the trunk and then a branch controller on each floor/room. With 10 branch controllers, the master only has to poll 10 nodes. And if you have 10 nodes on each branch, the branch controllers also have to poll only 10 nodes. So you still poll 100 end nodes, but in 1/10th of the time. And because you require shorter bus cables, the baudrate can be increased as well, further reducing required poll time.
Stef (remove caps, dashes and .invalid from e-mail address to reply by mail)
Do you guys know we just passed thru a BLACK HOLE in space?
CAN is a short-range protocol. If your bus is over about 20-30 m, forget CAN. (People do use it for longer distances, at low speeds, but it's horrible for the task. And CANOpen and other "standards" look like they were designed specifically to be as complex and inefficient as possible.) For a short range, small message bus on a closed network entirely under your control, CAN is great.
You should try to think about having multiple bus segments with fewer nodes per segment, and some sort of aggregator or gateway that combines them.
I think KNX is mainly used in lifts, but I'm not sure. There are a large number of "standards" used for building automation, with a variety of pros and cons, standardisation processes, membership fees, complexities, etc.
As Don said, often the cost of the wiring is the real price. But that can depend on where you are (the ratio of electrician hourly rate to cable price per meter varies hugely around the world) and the stage the building is at. Pulling one more cable while the building is being constructed, is practically free. Installing hidden wiring in a building in heavy use is massively expensive.
So sometimes using existing Ethernet networks, wireless networks (either Wifi or a dedicated wireless network), or even power-line communication can be cheaper than wiring.
If you want the newest and coolest solution, try two-wire Ethernet. Long-range does 2 km point to point, while short range is (IIRC) 40 m on a multi-drop bus - though I haven't seen any PHYs for short range two-wire Ethernet as yet.
If you try to run CAN at 1 Mbit/s it is quite critical with limited distance and strict rules how long branches you can connect from the main bus. Ideally the main bus should be routed through all nodes without branch cables.
At 250 kbits/s you can still run about 250 m and the branch lengths are not that critical. Should be sufficient for home automation,
CanOpen is great for setting up the system with EDS (Electronic Data Sheet) or inquiring the node capabilities with SDOs request messages.
For real-time communication the PDOs are mapped directly to real-time CAN-frames, so no performance issues.
I assumed that the OP is designing both the master and slaves.
I have designed both CAN as well as RS-485 networks with 60 - 100 nodes and in practice, most problems were related to the electro- mechanical reliability of the cabling harnesses.
For this reason, it is a good idea to split the network into multiple segments, each with own independent master.
If the master doesn't have sufficient number of serial ports one solution is to connecting multiple Ethenet/RS-485 or Ethernel/CANbus converters with ordinary Ethernet cabling and Ethernet switches. This may also simplify RS-485 or CAN cable length issues by inserting the converters closer to the nodes,
That can be fine, if the bus is short enough with a small enough loading in comparison to the speed. (Another thing I have done long in the past to make wiring easier is support auto-swapping of D+ and D-, correcting some wiring errors automatically.)
Yes, AFAIK. I haven't used it yet, however.
Of course you could have switches anywhere, giving you a tree just like normal Ethernet.
You can drive power over the two lines as well.
It's hard to get /everything/ you want from a bus!