Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

RS485 bus monitoring issue

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
azny

Bascom Member



Joined: 25 Aug 2008
Posts: 36
Location: usa

usa.gif
PostPosted: Fri Jul 11, 2014 3:32 pm    Post subject: RS485 bus monitoring issue Reply with quote

Hi All;

I am working on a RS485 multinode system using a PC for the master and the ATmega164p and the MAX481CSA RS485 chip in each slave node. each slave node has it's own internal address. I use the simple CONFIG PRINT command and use a control pin as output to the RE/DE of the '481-- exactly like the BASCOM manual for RS485 topic says.

When i poll each slave by using its explicit address everything works fine. My question is what about an "all call" i.e. a command from the PC to request each slave send their address so i can build up a table in the PC of whose connected to the system. when i try and do an "all call", of course, each slave responds to the command, and they all send out their addresses at the same time causing chaos. there must be some method to having them orderly send out their data so that collisions are avoided.

All the CONFIG PRINT command does is make sure the RE/DE line is toggled properly from TX to RX but it does not help with monitoring the RS485 bus to see when it's available to send data.

I do not have access to the slaves to set individual delay's which would solve the problem, nor do i want to poll every known address, see who responds, and build up my table from there.

what i think i need is some code that can be installed in the slaves to test the RS485 bus to see when it's busy and when it's free, once it's free, then PRINT the data to be sent.

the general algorithm i use is:

(1) slave is default listening to bus
(2) use ISCHARWAITING loop to build up the command string sent from the PC, i.e. "get_all_address"
(3) parse/test the string to validate the command

(4) monitor the bus to see when it's free to send [THIS IS WHERE I NEED YOUR HELP]

(5) send back reply to the PC

thanks in advance for your help and suggestions

(BASCOM-AVR version : 2.0.7.5 , Latest : 2.0.7.7 )
Back to top
View user's profile
rkumetz

Bascom Member



Joined: 23 Jan 2007
Posts: 165
Location: Vermont

usa.gif
PostPosted: Fri Jul 11, 2014 4:03 pm    Post subject: Reply with quote

I have never seen an all-call done on RS485 but here is how I would try it:

1. Have the master send the request.
2. Each slave starts listening for replies.
3. When the response from "myaddress - 1" occurs then I send my "I am here"
4. If no response occurs for a time equivalent to either my address x the time of the
I am here packet or my address - last address (if replies have occured) then I reply.

I think it may actually be easier to have the master poll every possible address.
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Fri Jul 11, 2014 4:06 pm    Post subject: Reply with quote

Hi,

As each slave has it's own address why not just wait a time * slave address, before sending the "I am here" packet.

I know at least one System that uses exactly method.

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
azny

Bascom Member



Joined: 25 Aug 2008
Posts: 36
Location: usa

usa.gif
PostPosted: Fri Jul 11, 2014 7:50 pm    Post subject: Reply with quote

thanks RK and ID-- but both of you must have missed the line in my original post

"I do not have access to the slaves to set individual delay's which would solve the problem, nor do i want to poll every known address, see who responds, and build up my table from there."

no question that polling each address (RK response) or adding individual delays to the slaves to stagger the response (ID response) and seeing who responds solves A problem of collisions-- but not THE problem i'm asking about here.

there must be a way that without a priori knowing either the address or the delay time that the slaves can send out their data staggered such that they dont collide.

the solution, i guess, would have to be some sort of monitoring the bus for activity, and then, when it's quiet whoever grabs the bus first transmits their data, while the others wait-- the cycle repeats until all addresses are captured by the PC

but how to do this is the question??
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sat Jul 12, 2014 12:28 am    Post subject: Reply with quote

Hello azny

Why not try a random number for a delay before sending for all-call (broadcast)

There is also an ability to put an address into the uart hardware of the AVRs that would be useful in setups like this as it fires an interrupt when the address is received but its not implemented in bascom.

Regards Paul
Back to top
View user's profile
azny

Bascom Member



Joined: 25 Aug 2008
Posts: 36
Location: usa

usa.gif
PostPosted: Sun Jul 13, 2014 3:32 am    Post subject: Reply with quote

Hi Paul;

yep thought of that too, it's an easily implementable solution for a few slaves but once the number of slaves increase the maximum random delay time must increase to decrease the probability of collisions due to random delays coinciding. Of course this problem could be overcome by sending out multiple all calls until the probability that you missed one slave is virtually zero.

But, again, this is a more "stop gap" solution than the sure fired one of being able to monitor the bus to see when the line is free to grab.

Is there a way to monitor the bus through the UART on the ATmega164P or by monitoring the RO/DI lines on the MAX481??
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sun Jul 13, 2014 9:07 am    Post subject: Reply with quote

No multiprocessor mode just helps with an address for the device.
Does it have to be RS485 as I was looking at this and found some nice buss extender ICs for I2C buss that could go to Kilometers in length that used higher voltages like RS232 does.
Saw a note to look at the modbus protocol seems it implements avoidance.

Regards Paul
Back to top
View user's profile
toto







PostPosted: Fri Apr 17, 2015 11:57 am    Post subject: Reply with quote

Hello,

I have a RS485 with 1 Master and 40 Slaves with the same problem.
I think it must exists a flags on the master side to avoid the sending of data at the same time a slave send data too.

Thanks in advance for any help

Best regards
toto
Back to top
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Fri Apr 17, 2015 12:15 pm    Post subject: Reply with quote

My understanding of the concept master/slave : Is 'slave' even allowed to use today? Maybe i should name it client/server.
Anyway, the concept is clear : the slave does nothing at all unless it is told to do something by the master.
A slave that starts to send by itself is not a real slave.
So a master send data to 1 slave, and all slaves listen to that slave. When the address is recognized, the slave responds. And only that slave !
On top of that add a CRC check and all should be well.
If the slaves do not obey to the slave rules it is hard to work with them.

_________________
Mark
Back to top
View user's profile Visit poster's website
toto







PostPosted: Fri Apr 17, 2015 2:19 pm    Post subject: Reply with quote

Hello Albertsm,

Thank you for the explanation.
In my case, the Master send data to the slaves and this data include the slave ID
My doubt is, how many time the Master after send data need to wait until send again to the next slave ID
to avoid data collision. Then i have slaves that can respond quickly and other that need more time

I have today add to my scan routine code this and want tests if it helps

scan_routine:
incr Destiny

'#################### This added today to make tests ################
If Ucsr0a.rxc0 = 1 Then
Tx_comand = 0 : Tx_data(1) = 0 : Tx_data(2) = 0 : Tx_data(3) = 0 : Tx_end = 0
Clear Serialin
Return
End If
'#########################################################

Printbin #1 , Master ; Destiny ; Q_bytes ; Tx_comand ; Tx_end ; Placa_nr ; Crypt ; Word_dbg ; T_done ; Tx_data(1) ; Q_bytes
Load Timer0 , Timer0reload
Enable Timer0
Start Timer0
Return

Best regards
toto
Back to top
toto







PostPosted: Fri Apr 17, 2015 4:13 pm    Post subject: Reply with quote

Hello Albertsm,

Can you please inform about Printbin how it works

Printbin #1 , Master ; Destiny ; Q_bytes ; Tx_comand ; Tx_end ; Placa_nr ; Crypt ; Word_dbg ; T_done ; Tx_data(1) ; Q_bytes

code
code

Printbin waits automaticly to send out the total of byte before execute the next line of code?

Thanks

Best regards
toto
Back to top
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Fri Apr 17, 2015 5:21 pm    Post subject: Reply with quote

To your last question: print doesn't wait when you are using buffered serialout. Then it returns just after all data is copied to the buffer. Otherwise it will return after the last byte is written to the send register (which is NOT the end of transmission as the cpu buffers one (or two?) bytes.

If you are working on a timeout based mechanism you should also make shure you slaves knows about that. I.e. if you define xy milliseconds before Master recognize a timeout you have to make sure a slave is not sending after xy milliseconds (or less with tolerance). A possible solution could be to hook into the serialin - routine (see config serialin - bytematch=all), clear a timeout flag and start a timer on each received byte. The timer overflow then will set the timeout flag. Before the slave wants to send something, he checks the flag. If set, skip sending. (If you are smart you use the TOVx flag in TIFR, no need to have a timer-isr then). Of course there are other possibilities.

But timeout does not solve all problems in a “multi nodes can answer scenario” (double IDs, babbling idiots etc.). I personal do not use RS485 anymore when I have to do 2-way communication with slave nodes - to much headaches. CAN is much more powerful and is doing most of the collision stuff "by itself".
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Fri Apr 17, 2015 9:32 pm    Post subject: Reply with quote

Afterwards start sending your packet. As the bus is half duplex you are also receiving what you are sending, so if you do not receive exactly what you are sending there must be a collision. So go back to start and wait a few mS while nothing is received (bus idle) and restart sending your packet.
_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
toto







PostPosted: Fri Apr 17, 2015 9:40 pm    Post subject: Reply with quote

Hello laborratte,

Thank Your. I not use buffered serialout, only buffered serialin "Ischarwaiting"

In the future i want change to CANBUS, but now i have not the necessary knowlegde and working Bascom examples of CANBUS communication

Best regards
Freundlichen Grüssen
toto
Back to top
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sat Apr 18, 2015 12:56 am    Post subject: Reply with quote

Hello toto

This blog may give you some info http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=9851

Regards Paul
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum