Access to ttyUSB* in Lazarus/Linux program lazarus / 32-Linux ---------------------------------------------------------------------- bvn123 · 2021-01-03 22:04 Hello, I use ttyUSB0 device in Lazarus program for Linux. Now the program works properly after device to be connected to USB then the access to ttyUSB0 to be proveided in the Therminal by next command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ echo "1234" | sudo chmod 666 /dev/ttyUSB0where "1234" is the password. I'd like to provide this access for ttyUSB0 not in Terminal but in Lazarus program and tried the next code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btAccessClick(sender:TObject);var  proc: TProcess;begin  proc:=TProcess.Create(nil);  proc.CommandLine:='echo "1234" | sudo chmod 666 /dev/ttyUSB0';  try    proc.Execute;  finally    proc.Free;  end;end;  It doesn't work. How to provide that kind of access correctly in Lazarus program? I already read this article: https://wiki.lazarus.freepascal.org/Executing_External_Programs Also I tried to use path for chmod command:  /bin/chmod, /root/bin/chmod Thanks. ---------------------------------------------------------------------- Peter H · 2021-01-03 22:53 You need the full path to the binary. If echo is an inbuilt shell command, this doesnt work. Maybe "Runcommand" is better for this purpose? https://wiki.lazarus.freepascal.org/Executing_External_Programs#.28Pro cess..29RunCommand ---------------------------------------------------------------------- lucamar · 2021-01-04 03:38 If you want to use pipes and/or redirection, that won't work: TProcess (or RunCommand, etc. which use it) is meant to run a single command with its own parameters. You'd do better by executing "/bin/bash" and passing to it your command line as its parameters. Then it'll be bash who has the responsability of not only finding the other executables but also of interpreting the pipes/redirections,which it can do easily (as the terminal, which is just a bash "session" proves). HTH! ---------------------------------------------------------------------- MarkMLl · 2021-01-04 08:09 Quote from: lucamar on January 04, 2021, 04:38:26 amIf you want to use pipes and/or redirection, that won't work: TProcess (or RunCommand, etc. which use it) is meant to run a single command with its own parameters. You'd do better by executing "/bin/bash" and passing to it your command line as its parameters. Then it'll be bash who has the responsability of not only finding the other executables but also of interpreting the pipes/redirections,which it can do easily (as the terminal, which is just a bash "session" proves). Alternatively use the standard serial.pp unit, or one of the serial comms libraries/components. MarkMLl ---------------------------------------------------------------------- bvn123 · 2021-01-04 08:32 Thanks for answers. I expected that this is well-known problem which has ready solution. I use TLazSerial component. It is based on synaser library, but the USB-device may be connected in Lazarus program only after permission in terminal. If common serial libraries have some procedures\functions which may provide access for ttyUSB* (not for ttyS*) , what kind of names they conveniently have? echo and chmod - both commands have appropriate files in /bin directory. I also tried this code in Lazarus prog: Code: [Select]proc.CommandLine:='/bin/echo "b747" | sudo -S /bin/chmod 666 /dev/ttyUSB*'; To use /bin/bash? May You write appropriate code for Lazarus, including part with password and chmod command? ---------------------------------------------------------------------- MarkMLl · 2021-01-04 08:44 Quote from: bvn123 on January 04, 2021, 09:32:51 amI use TLazSerial component. It is based on synaser library, but the USB-device may be connected in Lazarus program only after permission in terminal. Add your habitual user to the dialout group. MarkMLl ---------------------------------------------------------------------- ccrause · 2021-01-04 09:31 Quote from: bvn123 on January 04, 2021, 09:32:51 amThanks for answers. I expected that this is well-known problem which has ready solution.The problem is related to permissions and not specific to FPC/Lazarus. The Linux way is as Mark mentioned:Quote from: MarkMLl on January 04, 2021, 09:44:15 amAdd your habitual user to the dialout group. Udev rules allow even more flexibility (filter device path, vendor & product ID, create symbolic link etc.) - see for example this post. ---------------------------------------------------------------------- Jurassic Pork · 2021-01-04 09:41 hello, yes the dialout group is the key for the permission troubleshootings of serial ports in linux  : QuoteIn this example our Linux userid is user and we are on the server called machine. Our USB device is called /dev/ttyUSB0. Check the current permissions and owner/group of the device. [user@machine ~]$ ls -la /dev/ttyUSB0 crw-rw----. 1 root dialout 188, 0 Apr 3 21:16 /dev/ttyUSB0 For this configuration, the owner is root, the group is _dialout_and both the owner/group have read/write permissions. What you need to do is make your login userid part of the group associated with the USB device. For this case, we add the group dialout to our userid user using the usermod command. This command requires root privileges to run. [user@machine ~]$ sudo usermod -a -G dialout user You will need to log out then log back in and now you should have access to the device. Friendly, J.P ---------------------------------------------------------------------- MarkMLl · 2021-01-04 09:55 Quote from: ccrause on January 04, 2021, 10:31:47 amUdev rules allow even more flexibility (filter device path, vendor & product ID, create symbolic link etc.) - see for example this post. Although broadly speaking the stuff in udev controls which group the device is being added to etc., it is still the job of the local operator to make sure that the appropriate user(s) are in that group. There's a whole lot of other things that can be done with POSIX capabilities but they're overkill for this. MarkMLl ---------------------------------------------------------------------- bvn123 · 2021-01-04 11:27 Quote from: ccrause on January 04, 2021, 10:31:47 amUdev rules allow even more flexibility (filter device path, vendor & product ID, create symbolic link etc.) - see for example this post. ccrause, thank You very much, Your link helped. I created file   Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i '11-ttyUSB.rules'(name should be <2 digits>-.rules) and wrote the next string to this file using VID & PID (10C4 & EA60) of my USB- UART adapter and selecting mode 666:   Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SUBSYSTEM=="usb", ATTR{idVendor}=="10C4", ATTR{idProduct}=="EA60", GROUP="plugdev", MODE="0666"Some problem was to place this file to etc/udev/rules.d/ (there isn't etc/udev/rules.dev/ dir in Linux mint) as admin privileges and password are required. But it isn't difficult to describe this process for user. Simple adding a user to dialout group doesn't work (I checked that user is really added to this group, may be rules for the user should be changed too). Jurassic Pork, as I understand, I use Your TLazSerial component, thank You for Your work. I tried this one in Delphi and in Lazarus. May be I downloaded not last archive, but -for Lazarus You already added baudrates br460800 and br500000, but in Delphi user should add them to component -there isn't ttyUSB device in Lazarus, only ttyS and COM, so, user may replace ttyS for ttyUSB in 3 places and change the function to extract device number for ttyUSB As simple COM-port is a rarity now, may be there is sense to reorient Your component for USB adapters (add all baudrates up to br3000000 for Delphi and ttyUSB for Lazarus)? Thanks to all ---------------------------------------------------------------------- MarkMLl · 2021-01-04 11:37 Quote from: bvn123 on January 04, 2021, 12:27:59 pmSimple adding a user to dialout group doesn't work (I checked that user is really added to this group, may be rules for the user should be changed too). /Why/ didn't it work? What does ls -l for the ttyUSBx device show? Did you remember to logout and then back in? That's been standard in Linux for around 20 years, i.e. roughly when /dev/ttyXXX were introduced as an alternative to the older /dev/cuaXXX devices. It might be that you need to use plugdev rather than dialout, but the latter is certainly what works on Debian... I use this stuff heavily. MarkMLl ---------------------------------------------------------------------- Jurassic Pork · 2021-01-04 12:08 Quote from: bvn123 on January 04, 2021, 12:27:59 pmJurassic Pork, as I understand, I use Your TLazSerial component, thank You for Your work. I tried this one in Delphi and in Lazarus. May be I downloaded not last archive, but -for Lazarus You already added baudrates br460800 and br500000, but in Delphi user should add them to component -there isn't ttyUSB device in Lazarus, only ttyS and COM, so, user may replace ttyS for ttyUSB in 3 places and change the function to extract device number for ttyUSB TLazserial is for Lazarus not for Delphi. With the  setupdialog dialogbox   TLazserial detects  the  serial port com available in the O.S.   On Ubuntu and an usb serial port adapter plug in an usb port i can see the /dev/ttyUSB0 port in the setupdialog form.  What is your Linux O.S ? Friendly, J.P ---------------------------------------------------------------------- funlw65 · 2021-01-04 12:48 Quote... Simple adding a user to dialout group doesn't work (I checked that user is really added to this group, may be rules for the user should be changed too). ... Thanks to all The simplest method is to install Arduino from your linux repository and it will fix your problem with an advice if your user does not have the required rights. But sometimes,  there is a need to add the user to one more group, forgot which one... it happened to me on some linux distros, I'll come back if I remember... Edit: I reread your post... if you add your user just to the dialout group is not enough. Be sure that is also member of plugdev group. ---------------------------------------------------------------------- bvn123 · 2021-01-04 13:54 Quote from: Jurassic Pork on January 04, 2021, 01:08:07 pmTLazserial is for Lazarus not for Delphi. ...  What is your Linux O.S ? Yes, the Delphi component only uses the same icon, the name of component is TRS232port. OS Linux mint (linuxmint-20-xfce-64bit.iso was used). Quote from: MarkMLl on January 04, 2021, 12:37:57 pm/Why/ didn't it work? What does ls -l for the ttyUSBx device show? Did you remember to logout and then back in? No, I didn't logout, so now to check this recommendation correctly I should delete the file /etc/udev/rules.d/11-ttyUSB.rules and exclude the user from plugdev group? To logout - is it enough to restart Linux? ---------------------------------------------------------------------- funlw65 · 2021-01-04 14:48 Just restart Linux. ---------------------------------------------------------------------- bvn123 · 2021-01-04 17:53 Quote from: MarkMLl on January 04, 2021, 12:37:57 pm/Why/ didn't it work? What does ls -l for the ttyUSBx device show? Did you remember to logout and then back in? MarkMLl Thank You MarkMLl, it works too, and Your variant is simpler. So, I added to dialout group user me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sudo usermod -a -G dialout me ---------------------------------------------------------------------- MarkMLl · 2021-01-04 18:14 Big grin here. I'm not sure whether having a user in BOTH dialout and plugdev is relevant, since the ttyXXX device will be in one group or the other as shown by  ls -a You need to be aware of the udev stuff, but strictly it's something that you do when you buy a new board and is distinct from setting up user permissions. Assuming that you're using some variant of Arduino, look carefully at the setup instructions that come with whatever type of board you go on to buy: this /particularly/ applies to Teensies where you have to get something JUST RIGHT (i.e. no trailing blank lines etc.) manually. MarkMLl ---------------------------------------------------------------------- bvn123 · 2021-01-04 18:39 Quote from: MarkMLl on January 04, 2021, 07:14:13 pmAssuming that you're using some variant of Arduino, ... MarkMLl Mainly I use CP2102 USB-UART bridges, sometimes FTDI chips when USB to fast parallel port adapter is preferable. ---------------------------------------------------------------------- MarkMLl · 2021-01-04 18:50 A useful thing about (genuine) FTFI chips is that you can "brand" them with a serial number, which makes detecting what's actually plugged in a bit easier. However enumerating device properties is an utter- and I hope that the community will turn an eye to my bad language /bitch/: while you can hook udev hotplug messages using (checks) NetLink, all too often you end up walking a file/directory tree in /sys MarkMLl