Serial multiplexer

Hi there,

Does anyone perhaps know of a serial multiplexer library, i.e., a way of having multiple serial connections over one physical serial port? I have been looking at libraries that provide serial packeting, but I could not find one that supports multiple connections or provides a drop-in replacement for something like HardwareSerial.

Alternatively, would anyone know of a standard serial protocol like COBS or SLIP that supports multiple connections?

With kind regards,
Jeroen.

Are you looking for a piece of hardware that switches one pair of UART Tx & Rx lines to one of several pairs of remote device Tx & Rx lines? Like an ordinary 1-to-n multiplexer (x2) so each device is individually selected via the multiplexer?

Or are you looking for a serial bus - like RS485 - where several devices are on the same bus and are "selected" by providing an address in the data packet?

The latter.

Specifically, I am looking for a way for two different libraries that expect to be the sole owner of a serial interface to share it. On the data link layer something like packets with a source and/or destination field will do the trick. For the libraries I would like to present a class that inherits from Stream to trick them into thinking that they are talking to a real serial port.

An obvious solution would be to run serial over IP over serial, but I am afraid that this will be a bit too much overhead for the average Arduino.

Perhaps something like this (or better) exists already?

Basically you can’t do this.
A multiplexer can only look at one input at a time. And while it is looking at one data can start arriving at the other. So you will miss the start of the byte.

The way to get more serial ports is to get an Arduino that has more than one, like the four on the Mega, or to buy an external board that adds two extra serial ports, I think Adafruit has one.

These have internal FIFO buffers so you don’t miss any data coming in when you are not looking at that port.

So using a protocol like an RS485 is a half duplex protocol which requires a master to orchestrate data transfer. This can get tricky.

I think I may not have explained this well enough.

The idea is to have several class instances that act like a serial interface, they all generate packets (tagged per instance) which are sent over one physical serial port.

Of course, on the other end we need something to undo all of this, but I see no obvious reason why this should not work.

Ah, ok. I think I get it ...

On device #1, you have several virtual serial ports that you can print/write to. Each Tx message gets tagged with the corresponding virtual com port number, fed into a single queue and then fed out of a single hardware UART Tx channel.

On device #2, the hardware UART pulls out each received message, reads the "tag" and puts the message into the correct virtual serial port Rx buffer.

The same process happens the opposite way round when device #2 passes messages back to device #1.

It sounds like you could use a typical RS485 network. It uses the same same bit rates and format as RS232, but RS485 serial adapters to produce the balanced signals.

https://www.cuidevices.com/blog/rs-485-serial-interface-explained

Yes, I think we are on the same page now.

A common use case for this would be to run a communication protocol over one of the virtual serial ports, while on an other port some debugging logs can be written to using print().

I just wanted to know if you, or anyone else here, is aware of something that implements this idea before I spend time on it.

Thanks for the suggestion, but I am currently not looking for a solution for a particular project. Preferably, I would like to have a software only solution.

RS232 serial is not intended to support multiple devices connected to a single line pair, which is why RS485 and similar were developed.

You still have not clearly explained what you are trying to do.

I am sorry for the confusion.

I don't actually want to have multiple devices connected to one line, I want multiple connections over the same line. This could also be phrased as having multiple "virtual" interfaces that, under the hood, communicate over the same physical interface.

I think markd833 explains it well in his last post.

[edit] And I mean "connection" in the sense of a network connection.

What does Arduino have to do with this?

One of each pair of endpoints will be on an Arduino.

Perhaps the example above illustrates it? It would be nice to run a communication protocol at the same time some library chatters debug information (which would normally interfere with the protocol).

For that sort of application, most people develop their own protocol.

I've never seen a "library" that supports a generalized version of something like that, but if you find a suitable example, please post a link. Others might find it useful.

I am actually planning to make one, I was just wondering whether something like that existed already to save me the effort.

Small update.

I have a proof of concept working now. On the Arduino it basically looks like this.

#include <serialMux.h>

SerialMux muxA(Serial);
SerialMux muxB(Serial);


void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  if (muxA.available()) {
    // Ping.
    muxA.write(muxA.read() + 1);
  }
  
  // Log messages.
  muxB.print("System time: ");
  muxB.println(millis());
}

On the host side I have a program that creates multiple "serial" ports (pseudo terminals actually), one of them I connect to using pySerial to run a ping like script, the other one I connect to using picocom to see the log messages.

New update.

Proof of concept versions of the Arduino library and matching host library are now available.

Seems to me no reason to do this yourself. If you use the MODBUS protocol over RS485 connections everything is already available (software drivers and inexpensive TTL-RS485 converters). Its been around for decades, can go over long distances quickly (much better than RS232), and is a common standard in industrial applications. Works for a single host and dozens of clients over a single twisted pair (plus ground) daisy-chained between devices.

Look in the Library Manager for "ArduinoModbus" and here is a link to one of many interface boards based on the MAX485 IC: https://www.amazon.com/ANMBEST-Transceiver-Arduino-Raspberry-Industrial-Control/dp/B088Q8TD4V/ Commercially, the cable used is Belden 3105A, but that would cost you at least an arm and a leg. Some people just use a pair in an Ethernet cable.

Thanks for your suggestion, but as mentioned before, I am looking for a software only solution (I assumed that the Development/Libraries section was meant for these types of questions).

This is primarily meant for hobbyists who want to do some debugging while controlling the board via the only communication line available to them.

Reminds me of what was done decades ago, where multiple RS-232 lines would go into a box that multiplexed them over a single high-speed connection to a similar box at the other end that split them back out into separate lines. Think at the time it was referred to as a concentrator.

Your software approach should work, as long as the libraries can accept a serial object instead of being hard-coded to use Serial.