Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to FIFO FILE, Linux & Mono(C#)

I want to do what I wrote in the title. But I just simply can't get my head around it. I also googled everythng. I want to write strings to file of special type FIFO, created by mkfifo (I think). If there are any other suggestions how to do this, you are welcome.

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();
    }
}
like image 291
Vili Volcini Avatar asked Jun 21 '13 17:06

Vili Volcini


People also ask

Which command is used to create a FIFO file?

FIFOs are created using mknod(2), mkfifo(3C), or the mknod(1M) command. They are removed using unlink(2) or the rm(1) command.

What is a FIFO file in Linux?

A FIFO special file sends data from one process to another so that the receiving process reads the data first-in-first-out (FIFO). A FIFO special file is also called a named pipe, or a FIFO . A FIFO special file can also be shared by a number of processes that were not created by forks.

Does Linux use FIFO?

Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available.


1 Answers

SOLUTION!!!! PI-BLASTER WORKS :D :D (lost 2 days of life because of this) write.flush was critical, btw.

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();
    }
}
}
like image 152
Vili Volcini Avatar answered Sep 18 '22 04:09

Vili Volcini