Recent

Author Topic: Access to ttyUSB* in Lazarus/Linux program  (Read 4250 times)

bvn123

  • New Member
  • *
  • Posts: 17
Access to ttyUSB* in Lazarus/Linux program
« on: January 03, 2021, 11:04:50 pm »
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][+][-]
  1. $ echo "1234" | sudo chmod 666 /dev/ttyUSB0
where "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][+][-]
  1. procedure TForm1.btAccessClick(sender:TObject);
  2. var
  3.   proc: TProcess;
  4. begin
  5.   proc:=TProcess.Create(nil);
  6.   proc.CommandLine:='echo "1234" | sudo chmod 666 /dev/ttyUSB0';
  7.   try
  8.     proc.Execute;
  9.   finally
  10.     proc.Free;
  11.   end;
  12. end;
  13.  

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

  • Sr. Member
  • ****
  • Posts: 272
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #1 on: January 03, 2021, 11:53:13 pm »
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#.28Process..29RunCommand

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #2 on: January 04, 2021, 04:38:26 am »
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!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #3 on: January 04, 2021, 09:09:41 am »
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).

Alternatively use the standard serial.pp unit, or one of the serial comms libraries/components.

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

bvn123

  • New Member
  • *
  • Posts: 17
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #4 on: January 04, 2021, 09:32:51 am »
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?
« Last Edit: January 04, 2021, 09:36:03 am by bvn123 »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #5 on: January 04, 2021, 09:44:15 am »
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.

Add your habitual user to the dialout group.

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

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #6 on: January 04, 2021, 10:31:47 am »
Thanks 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:
Add 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

  • Hero Member
  • *****
  • Posts: 1228
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #7 on: January 04, 2021, 10:41:55 am »
hello,
yes the dialout group is the key for the permission troubleshootings of serial ports in linux  :
Quote
In 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
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #8 on: January 04, 2021, 10:55:57 am »
Udev 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
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

bvn123

  • New Member
  • *
  • Posts: 17
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #9 on: January 04, 2021, 12:27:59 pm »
Udev 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][+][-]
  1. '11-ttyUSB.rules'
(name should be <2 digits>-<name>.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][+][-]
  1. 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

  • Hero Member
  • *****
  • Posts: 6686
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #10 on: January 04, 2021, 12:37:57 pm »
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).

/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
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

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #11 on: January 04, 2021, 01:08:07 pm »
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
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
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

funlw65

  • Full Member
  • ***
  • Posts: 148
    • Visual Pin Configurator for Nucleo 64pin boards
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #12 on: January 04, 2021, 01:48:16 pm »
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.
« Last Edit: January 04, 2021, 01:52:34 pm by funlw65 »
FreePascal 3.2.2, C 10.2.1, D 1.24 under Linux(init,musl,glibc), DragonflyBSD, NetBSD
gui: gtk2, qt5, raylib4.x+raygui3.x, nanovg 
tui: freevision, tvision2, termbox2+widgets, finalcut
db: typhoon-1.11...

bvn123

  • New Member
  • *
  • Posts: 17
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #13 on: January 04, 2021, 02:54:26 pm »
TLazserial 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).

/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

  • Full Member
  • ***
  • Posts: 148
    • Visual Pin Configurator for Nucleo 64pin boards
Re: Access to ttyUSB* in Lazarus/Linux program
« Reply #14 on: January 04, 2021, 03:48:38 pm »
Just restart Linux.
FreePascal 3.2.2, C 10.2.1, D 1.24 under Linux(init,musl,glibc), DragonflyBSD, NetBSD
gui: gtk2, qt5, raylib4.x+raygui3.x, nanovg 
tui: freevision, tvision2, termbox2+widgets, finalcut
db: typhoon-1.11...

 

TinyPortal © 2005-2018