Serial bridge?

Hey guys,

So i'm designing something that will be dealing with three serial ports (mega). I have serial devices A, B, and C. At times, I will be reading data from A and interpreting it, then passing the data off to B. Other times, however, I'll want C to have a direct line of communication to A. That is, when I'm not interpreting data for B, I'd like C to be able to talk to A as if it was directly connected to it.

Is this possible to do?

For the passthru, I was thinking of just having a while available loop on A and then immediately sending bytes out to C and vice versa. But I'm thinking this may not be the best way to do it, and at times I may fill up the buffer, no? Any other ideas?

Thanks!

A while serial.available loop would be blocking. Your Arduino would not be able to do anything else for as long as the loop is executing.

You could have a main loop that checks for serial.available and if there's anything in the buffer you could call a function with a while serial.availbale that reads the chars and pass them on.

This is not a perfect solution, what if something arrives on port C while the processor is busy passing data from A to B ?

An interrupt driven system would probably be better, but i don't think you can attach interrupts to the serial pins. Someone else in here probably knows better than me :slight_smile:

Another problem is how will you determine if A should communicate directly with B, or if the data needs to be dealt with before beeing passed on, without actually reading the data, not just pass it on ?

Is this possible to do?

It depends - if the average data rate on the in-bound links is less than the datarate on the uplink (less a little for overheads), then yes, it is probably doable.
You need to do more analysis on the traffic.

Thanks for the replies, guys.

"C" will rarely need to connect to "A." I would even be OK with putting a switch on the device that switches between B and C talking to A. In fact, if its possible to do this strictly at a hardware level (physically connect A to B or A to C) then that's fine too.

To put it in context: most of the time I'll want my Arduino talking to A. It will do some interpretation and pass values off to B.

However, at some times, I'll need to connect a computer ("C") back to A (for software updates, diag, etc). Because of the design and location of A, it will be very hard to physically disconnect the arduino and directly connect the computer to A. So I'll need a way to be able to connect a computer to A when necessary, either through the arduino or around it.

I'm open to any method that will allow me to, at times, connect a computer (C) directly back to the device (A).

To clarify, B and C will never be talking to A at the same time. It's one or the other.

Hope that makes sense!!

Thanks!

You mention in your first post that you will use a mega. These Arduino's use an MCU with 4 independent full duplex serial ports. The ports work with interrupts (on receive) and allow you to exchange data with 4 devices simultaneously. So yes - this is possible.

If you have one mega (e.g. A with 4 ports) and only a single port on B and C you can design your software so that B and C can exchange data, but do so via A. If all "devices" are mega's, you can create a full anarchy with no limitations.

For such an application (given that the devices are indeed micro controllers) you would typically choose a different communication protocol (Hardware serial is point-to-point). One possibility would be Ethernet (additional hardware required), but also the I2C protocol will support this in a bus configuration.

Ben,

Thanks for the reply! I fear you have overestimated my understanding of these things! I'm very new to this.

To put things in context and hopefully better explain what i'm looking to do, I'll give a better example.

I have an engine module as device A. Device B is a logging module. And device C is just a computer with software to configure the engine module.

The whole point of using the arduino is to poll the engine module, get some values, and translate it into something the logging module can use and send it to it. So the engine module (A) will be translating data and passing to logging module (B).

However, at times I will need to reconfigure the engine module. I do this on the computer with the same port the arduino is using to get its data. When this happens, I need the computer (C) to be able to talk directly to the engine module (A) as if it were directly connected to it. At this point I don't care about the logging module (B) being able to talk to the engine computer (A).

So what do you think is the best way to do this?

Another idea - can I cheat with the USB -> UART converter? Since the computer is already using a USB -> serial interface to connect to this device, can I take that out of the equation and hook directly into the USB port? Will it then be as if I was transparently connected to the first serial device?

Thanks so much for the help!

Generally when you want to talk to two serial devices (A to B and A to C) you need a board with two physical ports. There is a board called Arduino Mega that has 4 serial ports and this is one way to go (use it for your engine controller).

For a single serial connection you can have multiple listeners, but only one talker at any given time. If B (the logger) will receive data only, it is possible to have C and B connected to the same port on A (C via USB and A to B direct). A complication from this is that the logger then will receive data intended for both C and B and you would have to format the data in such a way that B would be able to tell the difference and ignore data intended for C (or accept that this data is added to the logg).

There is an Arduino library available (SoftSerial) that will allow you to use two digital IO lines for serial communication. If you only need to send data to the logger - this could be an option.

Some things to consider if you're still at the exploring stage:

If the datalogger requires a true RS-232 data connection, then irrespective of above options you will need additional hardware to condition the signals between the Arduino and the datalogger.

By default, the Arduino (your engine controller) will reset itself when you open the USB port from the PC side. If this is an issue, you will need software on the PC side that allow you to suppress reset.

I'm just tossing out an idea here...

If you don't need multiples at once... you could always build an RS232 Switch controlled by the Arduino. I would imagine that you could use some 4066 (or similar) solid state switches to change the signal flow.

BenF:

I've already purchased the mega as I'm aware that's the one with more than one serial port.

The engine controller is its own device (that's not what i'm building). So is the logging device. I just need the arduino to properly format the data between them. At times, however, I'll need to connect a computer to the engine controller for configuration. Since there is only one serial port on the engine controller, and since this port will be connected to the arduino, i'll need to be able to have the computer access the engine controller either through the arduino or around it. Because of the location of the engine controller, once I plug the serial port on it into something, it stays - it wouldn't be feasible to keep switching between, say, the arduino and the computer connecting to it.

Again, when the computer is connected, I don't need the logger to be connected. It's one or the other.

It's also my understanding that with a Mega I wouldn't need to use softserial, is this correct?

If it would make it easier, think of it as a simple switch. I will connect the engine controller to the arduino through serial, then I will need to switch between the arduino talking to the engine controller and a computer (whether by USB or serial).

Thanks again for all the help!

The engine controller is its own device (that's not what i'm building). So is the logging device. I just need the arduino to properly format the data between them.

In that case you should be able to service the three devices simultaneously without conflict. You would do this in your loop() function along the following lines:

void loop() {
if (Serial1.available()) { // computer
read data into a buffer
when you have all required data

  • format engine command
  • send command to engine
    }
    if (Serial2.available()) { // engine
    read data into a buffer
    when you have all required data
  • format and send to logger
    or
  • return status to computer (if config command)
    }
    if (Serial3.available()) { // logger
    read data into a buffer
    when you have all required data
  • act on the data received
    }
    }

Before connecting the Arduino you need to check serial port requirements for your engine/logger devices and add approriate level converters (e.g. TTL to RS-232).

Perfect, thanks. I suspect this will be one of those things I'll need to play around with to get right.

Thank you again for your help!