Search code examples
collisionavruartatmegars485

Broadcasting and Fetch Data On UART


I have a computer (that use as server) and several board with Atmega microcontroller Something like:

computer and board

The computer connect to board on UART & RS485 (with a USB to RS485 converter)(I have limitation that lead to I could not use ModBus). I want to broadcast a message from server over bus and fetch the ID from of each board (Board ID is 4 digit).

When the boards receive broadcast message and try to send their own ID and the server receive some fake ID and I think it related to Collisions problem when all boards want to send data in one time.

After I search about this problem found a way that put a constant in each board that save a special delay for send data and when board receive broadcast message send ID with that delay...in this way it work fine and I dont see the Collisions but have some problem:

  1. May be the delay number of 2 board be same.
  2. Good way for small count of board.
  3. Extra process when want to install board on bus.

Anybody know with this problem and could help me how to solve this problem with better solution?


Solution

  • You are mentioning Modbus in your question although some of your other stated facts seem to deviate from there (like 4-digit device numbers, and Modbus only has 1-255). Also, Modbus does not support responses to broadcast messages. I thus doubt a bit you are actually using Modbus.

    A scheme you could use (and that is classically used in MA networks) would be:

    1. Once a broadcast is received, have the clients scan the bus for responses for a time frame based on its station ID. If your client can see one, have it wait a minimum bus time (the time a module needs to answer your broadcast message based on current bus timing + the round trip for the master acknowledging the broadcast answer) plus an additional time based on its module ID, then go back to (1)
    2. If a client sees the bus unoccupied for the specified time, send back a broadcast answer.
    3. Have the master acknowledge the broadcast response from this client with the shortest possible message.
    4. If a client that has sent a broadcast response does not receive a proper ack, go back to (1)

    This is not 100% secure and absolutely not according to the Modbus specification, but could work.

    * is a transmission, - is a "wait"
    
    ****  (Bus master broadcast)
    
        --------- station 100 waits 100ms
        ------------------ station 200 waits 200ms
                 **** Station 100 sends broadcast response
                    ------------------ station 200 sees bus active and waits another 200ms
                     *** master acknowledges broadcast response of 100
                        ------------------ station 200 sees bus active again and waits 200ms from last seen activity
                                          **** Station 200 has seen bus quiet for 200ms and sends broadcast response
                                              *** master acks brc response of 200
    

    This can take quite a bit of time and needs the waiting times finely adjusted against the transmission time of broadcast responses and response acks, but can work, and actually is implemented that way in a lot of CSMA/CD networks.