我需要我的程序执行一个命令,在Bash中看起来像这样:echo P1-12=175>/dev/servoblaster"。如何在Linux上的Mono/c#中做到这一点?我写了下面的类:
public static class ServoBlasterPWM
{
private static FileStream devFile;
public static bool IsRunning
{
get;
private set;
}
public static bool Initialize()
{
try
{
devFile = File.Open("/dev/servoblaster", FileMode.Open);
}
catch (Exception e)
{
Log.Error("Error while setting up port for Servo Blaster: " + e.Message);
return false;
}
Log.Success("ServoBlaster is set up.");
IsRunning = true;
return true;
}
public static void SetPWMOutput(int pin, int valueSteps)
{
//byte[] bytes = Encoding.ASCII.GetBytes("P1-" + pin.ToString() + "=" + valueSteps.ToString());
byte[] bytes = Encoding.ASCII.GetBytes("P1-12=175");
devFile.Write(bytes, 0, bytes.Length);
devFile.Flush();
Console.WriteLine("Wrote to file");
}
public static void Shutdown()
{
devFile.Close();
IsRunning = false;
Log.Info("Servo Blaster has been shut down.");
}
}
这段代码不起作用。我也尝试过StreamWriter而不是FileStream,但是它崩溃了,出现了一个异常,说流不支持查找。我不知道该怎么做。请帮助。
尝试将字符串替换为数组代码:
byte[] bytes = ASCIIEncoding.UTF8.GetBytes("P1-12=175") ;
或:
byte[] bytes = ASCIIEncoding.Default.GetBytes("P1-12=175") ;