Skip to content Skip to sidebar Skip to footer

Serial Communication One To One

If this is a stupid question, please don't mind me. But I spent some time trying to find the answer but I couldn't get anything solid. Maybe this is a hardware question, but I figu

Solution 1:

Multiple clients (e.g. Arduinos) communicating with one server (e.g. a desktop computer) is commonly done with the serial variant:

RS-485

This is a simple method widely used in industrial settings where you want to have many devices connected to one computer via one serial port. This type of arrangement is also called multi-drop, because one cable strings around a building with Tees that tap in and drop lines to each device.

The hardware for this is widely available. You can buy USB serial adapters that provide the hardware interface for a computer. Programmatically the port looks just like an RS232 port. For the Arduino you would just add a transceiver chip. A sea of serial transceivers exists, e.g.

Example computer USB adapter with 485 interfaceSample RS485 transceiver chip from Element14

All the devices hang on the same bus listening at the same time. A simple communication protocol used is just add a device address before every command. For example:

  • 001SETLIGHT1 <- tells Arduino "001" to turn on the light
  • 013SETLIGHT0 <- tells "013" to turn off the light

Any device hanging on the cable ignores commands that do not start with their address. When a device responds, it prepends its address.

  • 001SETLIGHT1DONE <- response from device "001" that the command has been received and executed

The address in the response lets the receiving party know which device was talking.

Solution 2:

Well, your question can be quite wide, so I'm going to layer my answer:

  • On the hardware side, the same pair of wires can work be shared with many devices. It is mostly a question of electronics (maintaining the signal in the good voltage range), and not having all devices writing to the serial port at the same time (or you'll get wreckage).

  • On the software side, on the host, yes you can share the same serial connection to a device with multiple processes. But that's not straight forward. I'll assume you're using an unix (macos or linux):

    • in unix, everything is a file, your serial connection is one too: /dev/ttyACM0 on linux, for example.
    • When you have a process opening that file, it will block it (using ioctl, iirc) so no other process can mess with that file too.
    • Then, you can input and output to that file using the process that opened it, that's all.

But hopefully, it is still possible to share the connection between processes. One of them would simply be to use the tee command, that will be able to get input from one process, and give it back output, and copy the output to another process. You can also do it from within python, by duplicating the file descriptor.

To easily output stuff that can be redirected the unix way (using pipes), you can use socat: http://www.dest-unreach.org/socat/

here's an usage example:

socat -,raw,echo=0,escape=0x0f /dev/ttyACM0,raw,echo=0,crnl

you may want to tweak it for your needs.

Solution 3:

Edit: I forgot about RS-485, which 'jdr5ca' was smart enough to recommend. My explanation below is restricted to RS-232, the more "garden variety" serial port. As 'jdr5ca' points out, RS-485 is a much better alternative for the described problem.

Original: To expand on zmo's answer a bit, it is possible to share serial at the hardware level, and it has been done before, but it is rarely done in practice.

Likewise, at the software driver level, it is again theoretically possible to share, but you run into similar problems as the hardware level, i.e. how to "share" the link to prevent collisions, etc.

A "typical" setup would be two serial (hardware) devices attached to each other 1:1. Each would run a single software process that would manage sending/receiving data on the link.

If it is desired to share the serial link amongst multiple processes (on either side), the software process that manages the link would also need to manage passing the received data to each reading process (keeping track of which data each process had read) and also arbitrate which sending process gets access to the link during "writes".

If there are multiple read/write processes on each end of the link, the handshaking/coordination of all this gets deep as some sort of meta-signaling arrangement may be needed to coordinate the comms between the process on each end.

Either a real mess or a fun challenge, depending on your needs and how you view such things.

Post a Comment for "Serial Communication One To One"