Linux – Sharing a serial port between two processes

linuxserial

As it is not possible to directly share a serial port between two processes using Linux, I am looking for another way to achieve this, I have heard about socat but could not find a concrete example of how to realize the following:

Split one physical serial port (/dev/ttyUSB0) into two virtual ports, one for reading and one for writing, as one process only needs to send data, and one only needs to receive data.

I can not modify the sending application unfortunately.

Best Answer

In linux it always was a problem to get exclusive access to the serial ports as the only way to restrict unwanted interferention is the user/group access rights.

With time the convention had been established how to tell the other processes not to open the serial port device when some process is willing to 'own' it: the process that is going to open the serial (parallel) port first check for lock file in the /tmp directory (other standards may use /var/lock directory), for example /tmp/LCK..ttyS0 in case of the ttyS0 device and writes its PID into that file. Then it reads it back and if the PID matches then the process took the ownership of the port. After the port is not needed any more the lock file is deleted by the process.

Thus you may open it from different processes provided these processes uids or gids has rw access on the device

Related Question