3

I have successfully installed ServoBlaster, and it runs quite well from the terminal with the command

echo p1-22=250 > /dev/servoblaster 

just like the documentation says.

However, when I try to call it from Mono with the code

string cmd = "echo p1-22=50 > /dev/servoblaster";;
Process proc = new Process();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = cmd; 
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
proc.WaitForExit();

It does nothing and I get no error. What am I doing wrong?

Steve French
  • 209
  • 1
  • 3
  • 14

1 Answers1

2

You can try and use the bash invocation read and execute -c and quoted command line (This is supposed to solve character escaping)

Process proc = new Process();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c 'echo p1-22=250 > /dev/servoblaster'"; 
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
proc.WaitForExit();

But that might not work either so try and use ProcessStartInfo which inherently waits for everything to finish processing before exiting.

 string executable = "/bin/bash";
 string args = "echo p1-22=250 > /dev/servoblaster";

 ProcessStartInfo procInfo = new ProcessStartInfo(executable , args);
 procInfo.UseShellExecute = false;
 procInfo.CreateNoWindow = true;
 procInfo.RedirectStandardOutput = true;
 procInfo.RedirectStandardError = true;

 Process proc = System.Diagnostics.Process.Start(procInfo);
 proc.WaitForExit();       
 int exitCode = proc.ExitCode;
 proc.Close();

I had more luck with running a script file instead of running commands directly. This is most likely cause by character escaping that is required when passing args directly to bash. Making a script file with the commands inside them could solve the problem you are having.

touch servo.sh
chmod +x servo.sh

Add the commands into the script

#!/bin/bash
echo p1-22=250 > /dev/servoblaster
Piotr Kula
  • 17,336
  • 6
  • 66
  • 105