Recent

Author Topic: [SOLVED] How to read from a serial device without sudo-rights?  (Read 915 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 749
[SOLVED] How to read from a serial device without sudo-rights?
« on: February 24, 2023, 05:18:56 pm »
My program uses unit SynaSer from Synapse to read data from a serial device. Everything is fine, but my program needs sudo rights. I want to avoid this.

In https://wiki.freepascal.org/Hardware_Access#Device_Setup is written:

Quote
Before starting, make sure to authorize your device on Linux. An option is to create a persistent UDEV rule for each device. For example, the following approach apply mode "0064" for the device specified by 'serial', 'idVendor' and 'idProduct' attributes:

Code: Text  [Select][+][-]
  1. # create a new dev rule based on some constants
  2. echo 'SUBSYSTEM=="tty", ATTRS{serial}=="A1004chl", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0664"' | sudo tee /etc/udev/rules.d/50-usb-serial.rules > /dev/null
  3.  
  4. # reload rules
  5. sudo udevadm control --reload-rules
  6.  
  7. # re-add all devices
  8. sudo udevadm trigger
  9.  
  10. # test
  11. ls -al /dev/ttyUSB0
  12.  
  13. # you may want to reboot your system
  14. sudo reboot

I don't understand this, but it sounds to me like this could be a way to avoid sudo-rights. Is this correct?

If yes, please help me to find the correct commands for my case. My program uses '/dev/ttyS0'. I don't know Vendor-ID or Product-ID etc. I'm a bloody beginner to this kind of Linux stuff, so some explainations what your command does would be very welcome.

I'm on Linux Ubuntu 18.04. Thanks in advance.
« Last Edit: February 24, 2023, 06:30:18 pm by Hartmut »

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: How to read from a serial device without sudo-rights?
« Reply #1 on: February 24, 2023, 05:23:22 pm »
The first hit I got when searching for the topic was this

You would have to figure out how things are setup on your linux distro but it should be similar.

For vendor/product id see this
« Last Edit: February 24, 2023, 05:27:33 pm by TRon »

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: How to read from a serial device without sudo-rights?
« Reply #2 on: February 24, 2023, 05:27:15 pm »
The udev rules are needed.

They set up the device with the correct ownership.

You can't rely on simply changing the owner/group/permissions as the device nodes are created in a temporary directory and the names are not always the same.

In the udev rules can also make provide a consistent device name that you can use.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: How to read from a serial device without sudo-rights?
« Reply #3 on: February 24, 2023, 05:30:39 pm »
The udev rule setup is the same for most mainstream Linux distros (Debian/Ubuntu and based on them, arch and based on it, Fedora and based on it, and many others, and has not changed in over 10 years).


MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: How to read from a serial device without sudo-rights?
« Reply #5 on: February 24, 2023, 06:09:23 pm »
Most distreaux have a dialout group for the serial ports, and users authorised to use them should be a member.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: How to read from a serial device without sudo-rights?
« Reply #6 on: February 24, 2023, 06:25:13 pm »
Most distreaux have a dialout group for the serial ports, and users authorised to use them should be a member.

Ah, of course. As long as the device name is not changing, just being in the correct group should be sufficient.

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: How to read from a serial device without sudo-rights?
« Reply #7 on: February 24, 2023, 06:30:00 pm »
The solution was:
   sudo usermod -a -G dialout $USER
and to log off and log in again.

I did not know how to find out, if device '/dev/ttyS0' is owned by user group 'dialout', so I just tried it and it worked :-)

Thanks to all who helped me.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: [SOLVED] How to read from a serial device without sudo-rights?
« Reply #8 on: February 24, 2023, 06:36:52 pm »
ls -lt /dev/tty* | head     and so on, where that will list the most-recently-hotplugged devices first.

You /might/ find that you need to mess around with udev rules if a particular family of serial devices isn't correctly being moved into the dialout group when hotplugged, but I don't recall having to do that for a significant number of years.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: How to read from a serial device without sudo-rights?
« Reply #9 on: February 24, 2023, 06:37:10 pm »
The solution was:
   sudo usermod -a -G dialout $USER
and to log off and log in again.

Good to hear!

I did not know how to find out, if device '/dev/ttyS0' is owned by user group 'dialout', so I just tried it and it worked :-)

Code: Text  [Select][+][-]
  1. ls -l /dev/ttyS0

Thanks to all who helped me.

MarkMLI's answer was the best one.

 

TinyPortal © 2005-2018