This strategy does not use RS485 but 3 wires TTL. You could send tokens via RS-485 to achieve a similar result however.

I have 6 PICs that need to talk among themselves in two side by side machines. Because they are always less than a metre apart I am using TTL in a peer to peer network.

The tasks each micro does are varied with very different loop times and some cannot be interrupted during critical phases. I devised the following protocol to minimise the time impact on processors in the network that were not directly involved in the present data transfer. The data is pretty sparse, no more than about 1 packet per second per device.

I have three active wires between all PICs. These all have 270 ohm series resistors for protection in case two outputs fight at any time. There are 16F88s, 16F877As and a 18F4550 in the micro network.

Each PIC has an address 1 to 14 as a 4 bit nibble. This nibble is repeated twice to make a byte so address 3 becomes %00110011.

There are three lines, ATTention, ACKnowledge and DATa, used as a data bus running through the system. These all have 10k resistor pullups at each PIC and by default all devices are set to inputs so the lines float high.

When a PIC wants to talk it pulls the ATTention line low and repeatedly sends the destination address out the DATa line. This runs until acknowledged or it times out.

All other micros are running their normal control loops and frequently call a subroutine named CheckComms at safe points within their main loops.

CheckComms essentially does the following:-
a. If the ATTention line is high the subroutine takes no action and immediately returns.
b. If the ATTention line is low, the subroutine grabs the destination address from the DATa line and if the address does not match its own ID, it sets a "NotForMe' flag and returns. The NotForMe flag stays set until the ATTention line is eventually released. If that micro later checks the ATTention line and it is still low and the NotForMe flag is still set it immediately returns so it does not waste any time checking addresses on a data transfer between others.
c. If ATT is low AND the unit ID matches the Destination being polled, the target micro pulls the ACKnowledge line low. The sending party now sees this line go low, stops sending destination addresses and starts repeatedly sending a Checksummed block of data. The receiving party only releases the ACK line after it has received an error free message. The sender holds the ATT line low and continuously repeats the message until it sees the ACK line go high.

There are numerous safety checks for errors such as the destination address must be received at least four times in a row and the upper and lower nibbles must match in all of them. Addresses 00 and FF are not used as these could occur in steady mark or space on the data line. There are maximum numbers of retries and there are repeat timeouts to prevent calls to a nonexistent address from hanging the system. There is LCD indication of comms faults. Collisions cause a comms timeout and the backoff time is determined by the device ID so they are unlikely to recurr.

The message transfer usually takes place within the minimum time but sometimes the message gets repeated a few times.

Communications is Serin/Serout @ 9600 8N1. The message is fixed length (16 active bytes plus header and checksum). Messages shorter just use the first bytes and leave the others with whatever junk they last had in them. Some command packets are only 1 byte but response packets can be up to 8 channels of ADC data.

This system is not foolproof and the bus could be stuck high or low by a faulty micro. The LCD panel indicates this and flashes an alarm LED on the front panel.

I can post an early but working version of the comms routine if you like.

HTH
Brian