Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-07

How to send operating system commands (Linux on RPi)?

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I need to include the ability to switch on and off the WiFi network connection of a Raspberry Pi3 running Raspbian Jessie into my FreePascal program.

The commands I am looking at now are:
sudo ifup wlan0
sudo ifdown wlan0

How could this be accomplished in a FreePascal program running as user Pi?

The Pi will be multihomed (there is an Ethernet network connection in addition to the WiFi) and it has been preconfigured to connect to a specific SSID in the conf file for WiFi.

I want my program to be able to switch on the device hosting the WiFi Access Point using relays (already done), then command the RPi to connect to the now active Access Point immediately. Time is of importance here and with just switching on the WiFi device I get connection delays varying from 20 seconds to several minutes.

So it seems like a specific command to tell the operating system to connect to WiFi is needed....

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Bo Berglund
Bo Berglund wrote 6 lines
The commands I am looking at now are: sudo ifup wlan0 sudo ifdown wlan0 How could this be accomplished in a FreePascal program running as user Pi?

To be specific I would like to know how I can send any command to the Linux operating system from my FreePascal program?

Just doing this seems not to cut it:

writeln('sudo ifup wlan0');

This just *displays* the text on the program console, but I want it to go to the operating system as a command...

On Windows one has to start cmd with the wanted command as a parameter, what is the corresponding method on Linux from within FreePascal?

Bo Berglund Developer in Sweden

Reply by email

Sven Barth
to Bo Berglund
"Bo Berglund" wrote 24 lines
On Sat, 02 Jul 2016 01:12:34 +0200, Bo Berglund wrote: >The commands I am looking at now are: >sudo ifup wlan0 >sudo ifdown wlan0 > >How could this be accomplished in a FreePascal program running as user >Pi? To be specific I would like to know how I can send any command to the Linux operating system from my FreePascal program? Just doing this seems not to cut it: writeln('sudo ifup wlan0'); This just *displays* the text on the program console, but I want it to go to the operating system as a command... On Windows one has to start cmd with the wanted command as a parameter, what is the corresponding method on Linux from within FreePascal?

Either fpsystem() from unit BaseUnix (this is the FPC variant of the C system() call) or TProcess if you should need to provide password input on a GUI.

Regards,
Sven

Reply by email

Bo Berglund
to Sven Barth: Either fpsystem() from unit BaseUnix (this is the FPC variant of the C system() call) or…
Sven Barth wrote 8 lines
Am 02.07.2016 07:55 schrieb "Bo Berglund" : > On Windows one has to start cmd with the wanted command as a > parameter, what is the corresponding method on Linux from within > FreePascal? Either fpsystem() from unit BaseUnix (this is the FPC variant of the C system() call) or TProcess if you should need to provide password input on a GUI.

Thanks,
this seems to work:

if arg = '1' then
   cmd := 'ifup wlan0'
else if arg = '0' then
   cmd := 'ifdown wlan0';
S := fpsystem('/usr/bin/sudo ' + cmd);

Now I just have to deal with the error when executing an ifdown command when the wlan0 is already down. Strange that a command should result in an error message if the result of the command is already present at the start...

Reminds me of some similar errors on a CNC machine controller I automated 15 years ago:
delete filename
-> ERROR: filename does not exist!

Such messages should not exist either, if one wants to delete a file and it is not existing then the mission is accomplished from the start and there should not be an error message...

In this case the Raspbian system generates:

sudo ifdown wlan0
ifdown: interface wlan0 not configured

and:

sudo ifup wlan0
ifup: interface wlan0 already configured

Well, luckily I saw it so I can handle these "errors"

Bo Berglund Developer in Sweden

Reply by email