Sharing a serial port between two processes
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.
multiple sources (such as stackoverflow.com/questions/8745948/…) confirm this , but if it’s possible anyway, please tell me 😉
did you note the edit to the question you referred: «Edit — This needs to work on Windows, but Mac and Linux would also be nice.» The answer was given for the Windows case. 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, so the convention had been established with time how to tell other processes not to open the serial port device when some process is willing to ‘own’ it. So you may open it from different processes provided the processes uids or gids has rw access on it
So you are simply stating that I can in fact access a serial device from multiple processes? If so, please post it as an answer, I will test it somewhere after the weekend probably, but then you will get your mark 😉
1 Answer 1
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
fldigi Wiki
linux_serial_howto Log in to Edit
Linux Serial Port Setup
You need to be a member of the group that owns the serial port device connected to your rig. On Debian-based systems such as Debian GNU/Linux, Ubuntu and its derivatives (Kubuntu, Xubuntu, Lubuntu etc.) and Mint, just to name a few, this is the «dialout» group. On other systems you can determine the group you need to join with this command:
(If this doesn’t return any files, use »ls -l /dev/ttyS*» instead.)
This should give you a list of one or more serial port devices of the form:
crw-rw---- 1 root uucp 188, 0 Jul 22 02:49 /dev/ttyUSB0
In this case the serial device is owned by user «root» and group «uucp», so you want to add yourself to the «uucp» group. In the examples below we will use «dialout» for the group we need to join.
There are several ways to add yourself to a group.
From a terminal, you can enter the following command
$ sudo adduser (username) dialout
Substitute your login name for (username)
You can check to be sure that you are now a member of the dialout group with the command:
which should respond with the full list of groups you belong to.
There are also Graphical User Interface tools that perform the same basic function. On Lubuntu this is the «users-admin» command which is usually found on the Lubuntu menu item «System Tools | Users and groups». You can of course start that tool from the command line
sudo is used for all of these modifications to ensure that you have the correct read/write privilege for the files that will be changed. You may need to log out and log back in again for the change to take effect.
Mint distribution
Contributed by Charlie, NA5NM
- Goto «Menu» and select «Control Center.»
- In the «Control Center» select «Users and Groups.»
- Under «Users and Groups» you will select «Manage Groups»
- A list of all the different groups will appear so you may scroll through the list to find ‘dialout’ select it then press the «Add» key.
- You need to log out and log back in again for the changes to take effect.
Ubuntu Unity desktop
Contributed by Dave, WB5NHL
- Click on «Dash Home» symbol in upper left corner of the desktop
- Key «users» click on the «Users and Groups» icon
- Select «Manage Groups»
- A list of all the different groups will appear so you may scroll through the list to find ‘dialout’
- Highlight «dialout» and select «Properties»
- If the box next to your name is checked, you are a member of the dialout group. If not, check the box!
- You need to log out and log back in again for the changes to take effect.
Korora (based on Fedora)
Contributed by John, KC5LAA
- Go to “Menu” and select “Administration” > “Users and Groups”
- Under “Edit” select “Preferences” and uncheck “Hide system users and groups”, then exit “Administration”
- Select “Groups” tab
- A list of all the different groups will appear so you may scroll through the list to find “dialout”
- Highlight “dialout” and select “Properties”
- Select “Group Users” tab
- Scroll down through the list of users to find your name.
- Make sure the box next to your name is checked.
- Select “OK” to exit the “Properties” window.
- Exit “Users and Groups” window
You need to log out and log back in again for the changes to take effect.
USB->RS232 adapters
The serial drivers may need to be installed for the device to work properly. After plugging in the device, first check the hardware messages to see the device was detected:
Toward the end of the messages, you should see this:
usb 1-1: new full speed USB device using uhci_and address 2 usb 1-1: configuration #1 chosen from 1 choice
Now, we need to figure out the device vendor number and product number to tell the usbserial driver. For this we can use lsusb. The easiest way to find the information is to first unplug the device, then run lsusb:
You will see a summary list of your USB devices on the system, for example:
Bus 001 Device 001: ID 0000:0000
Now, plug the device back in, and run lsusb again:
Which returns the summary list again, but now showing the new USB device:
Bus 001 Device 002: ID 4348:5523 Bus 001 Device 001: ID 0000:0000
Now, we can load up the driver for this device:
sudo modprobe usbserial vendor=0x4348 product=0x5523
Finally, check dmesg again to ensure it loaded properly.
Near the end, you should see something like:
usbserial_generic 1-1:1.0: generic converter detected usb 1-1: generic converter now attached to ttyUSB0 usbcore: registered new interface driver usbserial_generic
Congratulations, your link is up — in this example, the device can be used on /dev/ttyUSB0. As much fun as you had doing this, there is some chance you want to do this automatically in the future. Just add this line:
usbserial vendor=0x4348 product=0x5523
to /etc/modules with the vendor and product numbers you found. Of course you must user sudo to edit that file.
Read/Write to a Serial Port Without Root?
I’m writing an application to read/write to/from a serial port in Fedora14, and it works great when I run it as root. But when I run it as a normal user I’m unable to obtain the privileges required to access the device (/dev/ttySx). That’s kind of crappy because now I can’t actually debug the damn thing using Eclipse. I’ve tried running Eclipse with sudo but it corrupts my workspace and I can’t even open the project. So I’d like to know if it’s possible to lower the access requirements to write to /dev/ttySx so that any normal user can access it. Is this possible?
7 Answers 7
The right to access a serial port is determined by the permissions of the device file (e.g. /dev/ttyS0 ). So all you need to do is either arrange for the device to be owned by you, or (better) put yourself in the group that owns the device, or (if Fedora supports it, which I think it does) arrange for the device to belong to the user who’s logged in on the console.
For example, on my system (not Fedora), /dev/ttyS0 is owned by the user root and the group dialout , so to be able to access the serial device, I would add myself to the dialout group:
I’m going to accept this one since it got me on the right path. For Fedora, the command usermod is used after a user has been created. The group in question was also dialout for me: sudo usermod -a -G dialout
I had an additional step to make to solve my issue. I came to this question while trying to connect to a micro:bit on port /dev/ttyACM0 using Debian 10.
crw-rw-rw- 1 root dialout 166, 0 Jan 5 16:13 /dev/ttyACM0
This indicates why we can access the port using sudo – it is owned by root. It also shows why adding the non-root user to the dialout group allows access without being root. I added my user account to the dialout group in /etc/group using:
I still could not connect with the micro:bit using a serial port monitor without being root.
The micro:bit is mounted under /media :
ls -ld /media/ drwxr-x—+ 2 root root 4096 Jan 5 12:44
This shows that the directory that the micro:bit is mounted on is owned by root. I needed to change the owner and group of this directory to :
sudo chowner /media/ sudo chgrp /media/
Now I can connect to the micro:bit using a serial port monitor without using sudo.
Your illustration doesn’t illustrate much. rw-rw-rw- is 666, which means that everybody should be able to read from and write to the device. And what is chowner ? And how is a serial port “mounted” under /media ?
Many distributions use group memberships to enable access to serial ports. I don’t know details for Fedora offhand, though.
I think you can add yourself in sudoers file which will allow you to specify a set of commands as command alias which you would be able to execute without password. You can find an excellent tutorial about sudoers file in Ubuntu docs.
You can then run eclipse normally and it’d be able to execute those specific commands without root permissions.
I had the same issue using VSCode PlatformIO trying to connect to an ESP32. Using
This answer has been given before. To quote RalfFriedl, “ chmod -R 777 /dev is a very bad idea.” (And the second -R is just wrong.) To quote unfa, “Giving free access to all users to everything inside /dev/ is extremely unsafe to do. Please don’t do this.”
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Many device access problems can be resolved through group membership changes.
You can find the device name by watching sudo journalctl —follow as you connect your device. OR ls -1 /dev >dev.before , connect the device, wait 10 seconds, ls -1 /dev >dev.after;diff dev. .
Specifically, if ls -l shows that the group permissions (the second » rwx » triplet) is » rw » (e.g.» -rw-rw—- «), then, adding oneself to the group that owns the device will grant rw access.
# change to your device name device="/dev/ttyS0" sudo adduser $USER $(stat -c "%G" $device)
This allows you membership in the group that can rw the device, but there is one more step.
To make all your processes members of the new group, logout and login. Group memberships are set up at login time.
To create a single process in the new group (for testing, prior to logout/login):
or, just type the group name. See man newgrp .