在Linux和Mono(C#)中写入FIFO文件

6

我希望能够实现标题中所写的内容,但我无法理解它。我也尝试了谷歌搜索所有相关内容。我想将字符串写入特殊类型的文件FIFO(我认为是通过mkfifo创建的)。如果有其他建议,请随时提出。

static class PWM
{

    static string fifoName = "/dev/pi-blaster";

    static FileStream file;
    static StreamWriter write;

    static PWM()
    {
        file = new FileInfo(fifoName).OpenWrite();

        write = new StreamWriter(file, Encoding.ASCII);
    }

    //FIRST METHOD
    public static void Set(int channel, float value)
    {
        string s = channel + "=" + value;

        Console.WriteLine(s);

        write.Write(s);

        // SECOND METHOD
       // RunProgram(s);
    }

    //SECOND METHOD
    static void RunProgram(string s)
    {
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.EnableRaisingEvents = true;

        proc.StartInfo.FileName = "bash";
        string x = "|echo " +s+" > /dev/pi-blaster";
        Console.WriteLine(x);

        proc.StartInfo.Arguments = x;
        proc.StartInfo.UseShellExecute = false;

        proc.StartInfo.RedirectStandardInput = true;

        proc.Start();
       // proc.WaitForExit();
    }
}
1个回答

8
解决方案!!! pi-blaster 可以工作 :D :D (因为这个问题浪费了我两天的时间) 顺便提一下,write.flush 很关键。
namespace PrototypeAP
{
static class PWM
{

    static string fifoName = "/dev/pi-blaster";

    static FileStream file;
    static StreamWriter write;

    static PWM()
    {
        file = new FileInfo(fifoName).OpenWrite();

        write = new StreamWriter(file, Encoding.ASCII);
    }

    //FIRST METHOD
    public static void Set(int channel, float value)
    {
        string s = channel + "=" + value + "\n";

        Console.WriteLine(s);

        write.Write(s);
        write.Flush();
    }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接