Recent

Author Topic: Retrieving the result of a 'cat' command into a Lazarus app  (Read 4489 times)

MartynA

  • New Member
  • *
  • Posts: 19
Retrieving the result of a 'cat' command into a Lazarus app
« on: December 09, 2017, 02:34:52 pm »
Hi,

I'm using Rasbian on an RPi 3.

If I execute this

cat /sys/class/gpio/gpio22/value

in Terminal, it correctly reports the state of a push-to-make switch across port#22 and Gnd (with pull-up resistor) as 1 or 0.  The 1 or zero is displayed in Terminal on the next line, of course.

If I do this in my app:

  S := 'cat /sys/class/gpio/gpio22/value >';
  Res := fpsystem(S);

, Res always returns zero, which I guess is telling me that the command executed without error, rather than what the value on port#22  was.  So, My question is, how do I read the 1 or 0 from my Lazarus app? 

Btw, in my app, I want to be able to poll the input from the port many times per second, so I doubt that sending the output of cat to a disk file would be fast enough.

TIA, Martyn
« Last Edit: December 09, 2017, 04:00:14 pm by MartynA »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #1 on: December 09, 2017, 04:32:49 pm »
/sys/class/gpio/gpio22/value  does not exist on my Raspberry Pi 3's all of them run stretch.

But something along this line should work:
Code: Pascal  [Select][+][-]
  1. program catweasel;
  2. {$mode objfpc}{$H+}
  3. uses sysutils, classes;
  4. var s:TStringlist;
  5. begin
  6.   s:= TStringlist.Create;
  7.   try
  8.     if FileExists('/sys/class/gpio/gpio22/value') then
  9.     begin
  10.       s.LoadFromFile('/sys/class/gpio/gpio22/value');
  11.       writeln(s.text);
  12.     end else
  13.       writeln('File does not exist');
  14.   finally
  15.     s.free;
  16.   end;
  17. end.

« Last Edit: December 09, 2017, 04:42:08 pm by Thaddy »
Specialize a type, not a var.

MartynA

  • New Member
  • *
  • Posts: 19
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #2 on: December 09, 2017, 04:44:34 pm »
Not quite sure what you mean by "does not exist".  Physically, port 22 is pin 15 on my RPi3.

And, if I redirect the output of the cat command I quoted to a file, it works fine.  Did you do the necessary set-up:  exporting the port and setting its direction to "In"?

Cheers, Martyn

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #3 on: December 09, 2017, 04:46:39 pm »
Post crossed. Did you check my example code?
Specialize a type, not a var.

MartynA

  • New Member
  • *
  • Posts: 19
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #4 on: December 09, 2017, 05:22:30 pm »
Yes, it works thanks, which would save me the trouble of redirecting it elsewhere.

But the purpose of my q was to see  if there is a way of doing it without accessing the file system.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #5 on: December 09, 2017, 05:28:20 pm »
In linux "everything is a file". It will be fast enough.
Then again, look at several of the GPIO options in the wiki.
http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi
« Last Edit: December 09, 2017, 05:30:01 pm by Thaddy »
Specialize a type, not a var.

MartynA

  • New Member
  • *
  • Posts: 19
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #6 on: December 09, 2017, 05:38:44 pm »
Thanks.

>Then again, look at several of the GPIO options in the wiki.

I have been, that's what my other 2 qs here have been about.

>In linux "everything is a file". It will be fast enough.

I'm not sure that the file system will be fast enough.  It wasn't for the "encapsulated shell calls" example - see my thread here:https://forum.lazarus.freepascal.org/index.php/topic,39177.0.html

It turned out that the reason I couldn't get the encapsulated shell calls to work initially was because the system didn't react fast enough until I introduced some Sleep()s between calls.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #7 on: December 09, 2017, 05:44:54 pm »
I'm not sure that the file system will be fast enough.
It is a *virtual* file system: Linux doesn't access the *disk* but the kernel space for such device directories. A common misconception for people coming from windows.
Specialize a type, not a var.

MartynA

  • New Member
  • *
  • Posts: 19
Re: Retrieving the result of a 'cat' command into a Lazarus app
« Reply #8 on: December 09, 2017, 05:55:43 pm »
Yes, I already got that thanks.

 

TinyPortal © 2005-2018