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?

4
  • What Pi OS are you running? Mono currently does not support hardware floating point operating systems - armhf. According to this blog entry support is coming for armhf Nov 3, 2013 at 17:25
  • I am using Debian Wheezy (soft floating point) - I think the error is in how I am calling it, it works perfectly from the terminal/command line, just not from Mono. I can use the camera module, but not the servoblaster. Nov 3, 2013 at 19:30
  • Why do you need to execute a process for this? Why can't you just do the writing to the device directly with this: FileStream file = new FileInfo("/dev/servoblaster").OpenWrite(); StreamWriter write = new StreamWriter(file, Encoding.ASCII); write.Write("p1-22=50"); write.Flush();
    – Moshe Katz
    Nov 8, 2013 at 19:38
  • Well, I didn't think about it for the most part... Is that a faster way? I very new to coding on the RaspberryPi/Mono Nov 8, 2013 at 22:19

1 Answer 1

2
+50

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
1
  • 1
    The first option (the addition of -c) worked! Oddly, the second did not, but I am now moving via mono! Thank you. Nov 6, 2013 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.