Cannot Connect to Serial Port in Linux (permission denied)

63 views (last 30 days)
Dear guys
I'm using serial port to communicate with a hardware for a HIL simulation. Apparently all was running smooth using Debian 9 and R2020 (not sure a/b version). Now I updated the system to Manjaro and R2021b. No aditional tool is used, just a .m script with serial access.
Since then, serial comms do not work, apparently due to permissions. Which is weird as everything else does work pretty well in system.
This is what happens trying to open serial:
>> hs = serialport("/dev/ttyACM0", 1000000);
Error using serialport (line 116)
Unable to connect to the serialport device at port '/dev/ttyACM0'. Verify that a device is connected
to the port, the port is not in use, and all serialport input arguments and parameter values are
supported by the device.
Additional Information: Unable to create lock file for the port. Permission denied
Error connecting to the serial port.
See related documentation for troubleshooting steps.
  • Device IS connected to the port and listed as available serial
  • The port it NOT in use, since I can open it using Putty, Arduino and a custom interface written in C.
  • serialport call is the same as before, only port name and baud rate parameters are used (1Mbps works in Putty as well)
  • by aditional information given, I tried the following
I've read in forums about permission, so, I first checked this to rule out the problem:
> groups mateusop
wheel audio input lp storage uucp users network power mateusop
> ll /dev/ttyACM0
crw-rw---- 1 root uucp 166, 0 Sep 27 09:55 /dev/ttyACM0
As seen, my user belongs to the uucp group.
I even tried this as suggested in other posts I've read:
> sudo chown mateusop /dev/ttyACM0
> ll /dev/ttyACM0
crw-rw---- 1 mateusop uucp 166, 0 Sep 27 09:55 /dev/ttyACM0
And still matlab fails.
Finally, I'm asking this on matlab forum since every other program works pretty normal:
raw messages shown in ACM0 with putty
"em" is the raw sync message being sent to matlab, seen with Putty.
I think I've ran out of options and ideas.
Thanks for any help.
------- EDIT -----------------------------------------------------------------------------------------------------
I also tried to give permission to ALL:
> sudo chmod 666 /dev/ttyACM0
> ll /dev/ttyACM0
crw-rw-rw- 1 mateusop uucp 166, 0 Sep 27 09:55 /dev/ttyACM0
no sucess too.

Answers (2)

John
John on 23 Nov 2024
I figured out a reasonable solution. Namely for my serial port (/dev/ttyACM0) run:
sudo touch /run/lock/LCK..ttyACM0
sudo chgrp $USER /run/lock/LCK..ttyACM0
sudo chown $USER /run/lock/LCK..ttyACM0
Then the connection works from MATLAB. Why does this work? From the error message above the issue is that MATLAB is unable to acquire a lock file for the serial port so errors out. In Linux lock files are stored under the /var/lock directory with a convention "LCK..{portname}" from: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s09.html
On RHEL systems the /var/lock directory is an alias to /run/lock which by default is writable ONLY by root. This means that when run as a normal user MATLAB does not have permission to create a lock file and errors out unless you run as root. If we instead create a manual lock file that is writable by the user we will run MATLAB with then it can update the file without issue.
It is a bit odd since IIUC MATLAB should respect the presence of an existing lock file and not actually attempt to use the resource but given the alternative I am not complaining.

Johannes Andersson
Johannes Andersson on 20 Dec 2022
You can add your user to the dialout group. You have to logout and login before changes take effect.
sudo usermod -a -G dialout <user>
  3 Comments
Axel
Axel on 10 May 2024
Exactly the same issue on fedora 39. The IO doesn't work. Also add my user to the dialout group. It doesn't work.
John
John on 23 Nov 2024
I have the same issue when running Rocky 9 Linux Matlab 2023b. I can access the serial port with other programs under my user (such as screen). There is another post about access to the lock for the port "https://www.mathworks.com/matlabcentral/answers/384904-why-do-i-get-error-no-permission-to-create-lock-file-when-i-try-to-receive-data-via-usbtoserial-in".
Seems it could be a permission issue with the default configuration of RHEL based systems? Running matlab as root resolved the issue as will changing the permissions I suspect although I am hesitant to adjust default file permissions for a core system directory.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!