2013-06-21 21 views
5

我想要做我在標題中寫的內容。但我根本無法理解它。我也搜索了每一個。我想寫字符串到由mkfifo(我認爲)創建的特殊類型的FIFO文件。如果有任何其他建議如何做到這一點,歡迎您。寫入FIFO文件,Linux和單聲道(C#)

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(); 
    } 
} 

回答

5

解決方案!!!! PI-BLASTER WORKS:D:D(因此而失去2天的生命) 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(); 
    } 
} 
}