2

Only 2 arduinos can normally be linked via serial port because TX and RX need to be cross-linked, and connecting multiple TXs together would cause conflict.

RS485 allows multiple nodes connected in parallel on a 2 wire differential 'bus', using a TX ENABLE pin to allow one master to transmit while all other slaves listen.

I suspect it should be possible to similarly connect multiple serial RXs together to give a common listening bus, and be able to use TX ENABLE on any one node at a time to transpose its RX and TX to allow its TX to transmit to the other RXs. This might be physically done using a changeover relay swapping RX with TX, but is it possible to somehow temporarily switch the TX and RX assignments on demand by software?

I doubt the actual pin assignments can be changed in-situ, but perhaps the same results are achievable with some programming wizards sleight-of-hand trickery?

  • What about SPI? That's designed for single-master multi-slave. Perhaps you could explain what you're trying to achieve in greater detail. – CharlieHanson Jan 20 '16 at 11:38
  • I am trying to create a text-based arduino clustering system for circumventing arduino resource limitations, which I try to explain in this link... arduino.stackexchange.com/questions/19608/… – Electroguard Jan 20 '16 at 13:13
  • How much do you know about network protocols? What it seems that you need answers an age-old situation of multiple nodes with interlinked communication. This is answered todaywith Ethernet as the 'default' machine to machine link, but there have been several iterations of network communications/protocols/topology. Start with ALOHA, and then maybe Token Ring. Working through the timeline of network comms I'm sure you'll find something that balances your needs versus advantages/disadvantages. – CharlieHanson Jan 20 '16 at 13:22
  • 1
    TX and RX pins are fixed to the hardware, so you can't swap assignments (Assuming Arduino Uno here). You could try softwareSerial, if you want a software only solution. Or do something like this half-duplex UART – Gerben Jan 20 '16 at 13:46
  • You can try by connecting RX and TX together and then setting the TX in high impedance with pinMode(1,INPUT). If this does not work (because the atmega overrides that), you can just put a tristate buffer on the TX pin. Or, better, a SPDT analog switch: it's the same as the "changeover relay", except that it is integrated. An example is the Analog ADG839, but there are tons of that. – frarugi87 Jan 20 '16 at 14:04
2

On an AVR-based Arduino, you can selectively enable the transmitter and receiver side of the UART (c.f. the datasheet of your MCU). Thus, you could in principle just connect all the TX and RX together, and make sure only one transmitter is enabled at a time, while all the others are in high impedance. If you do this, I would strongly recommend you put a current-limiting resistor in series with each TX, just in case.

For example, on an Arduino Uno (Atmega328P), you can use the following functions:

static inline void enable_tx()  { UCSR0B |=  _BV(TXEN0); }
static inline void disable_tx() { UCSR0B &= ~_BV(TXEN0); }
static inline void enable_rx()  { UCSR0B |=  _BV(RXEN0); }
static inline void disable_rx() { UCSR0B &= ~_BV(RXEN0); }

Edit: Beware that Serial.begin() enables both the transmitter and the receiver.

  • Thank you Edgar Bonet, that is the type of 'wizardry' I was wondering if was possible. – Electroguard Jan 20 '16 at 14:26
1

It sounds to me like you are misunderstanding the purpose of the TX Enable pin and you are trying to come up with some convoluted method to do exactly what TX Enable actually does.

Under normal circumstances, in RS485, the UART's RX pin is connected to the bus. When TX Enable is asserted the RX pin is disconnected and the TX pin is connected, thus allowing any node on the bus to be a transmitter and all the other nodes a receiver.

The main thing to watch out for though is that only one node is ever a transmitter at a time, otherwise you get a collision which means that the data is completely garbled.

The simplest way of stopping that happening is to have one node nominated as the master and all the other nodes as slaves. The master first initiates communication by making a request to a slave. The slave will then respond with data (it becomes the transmitter and the master a receiver) to send data to the master. That way the only node that is a transmitter is either the master making the request, or the slave that is responding to a request.

If you want "random" peer-to-peer communication where any node could become a transmitter at any time it becomes a whole lot more complex. There are things you should implement to try to avoid, or deal with, collisions, including monitoring the bus and only transmitting after a certain amount of idle time has been detected, reading the bus while writing to it (requires more hardware) and confirming that the data you read from the bus is the same as the data you wrote to the bus, and random delays before retrying a failed communication. That last one is probably the trickiest to get right, since random numbers on Arduinos aren't really random unless you "seed" them from a truly random source. You don't want both ends delaying for the same amount of time to retry otherwise it will never complete.


Since you apparently want to achieve that kind of thing but without using RS485, it all gets much more complex.

No, you cannot "swap" the functionality of the TX and RX pins in the Arduino. BUT you can do serial communication (albeit somewhat heavyweight) using pure software. Take a look at the SoftwareSerial library - you will most likely need to make modifications to that to achieve what you want.

To do it externally with hardware is possible, but the end result will most likely be more complex than just using a simple RS485 interface chip which are very inexpensive and easy to get hold of.

Yes, you could use relays (clunky) or buffers, or multiplexers, etc, but why would you when RS485 does what you want?

  • No, what I am trying to do is achieve the same sort of master/slave multi-node capability as RS485, but WITHOUT using RS485, just using the standard serial port TX and RX. – Electroguard Jan 20 '16 at 12:07
  • Ah, I see. Your question wasn't clear at all. You should make it more explicit in your question. – Majenko Jan 20 '16 at 12:08
  • It was just a thought that popped into my head when I discovered my batch of RS485 interfaces came as conjoined siamese twins which I cannot snap apart even after scoring the join with a sharp blade, so I must now try to carefully hacksaw them apart. If it was possible to achieve similar RS485 multi-node capability via software using the standard serial RX and TX it could have made my initial steps for developing arduino cluster capability using Nick Gammons RS485 library a liitle bit less 'hardwarey' is all, and perhaps may even have made the clunky little interfaces unnecessary. – Electroguard Jan 20 '16 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.