使用Mono 4.0,有没有人知道如何写入开放式控制台,即/ dev / pts / 0?

时间:2015-05-22 20:52:14

标签: c# mono

我似乎无法弄清楚如何在CentOS 7.1上重复写入控制台,例如/ dev / pts / 0。我尝试做的是将命令传递给正在侦听命名管道的进程。该进程应该能够将一些输出写回指定的控制台。

使用SerialPort给我一个InvalidArguement异常,我假设它意味着它不知道伪终端文件类型。

我尝试了File.WriteAllText并运行了一次然后当我在管道上发送第二个命令时,它在路径/ dev / pts / 0上给了我一个共享冲突。

我尝试了File.AppendAllText,我收到了一个错误,该流不支持搜索。

我尝试使用FileStream并在路径/ dev / pts / 0上获得与WriteAllText相同的共享冲突。

以下echoCommand函数中的代码显示了我对不同选项所做的注释:

using System;
using System.IO;
using System.IO.Pipes;
using System.IO.Ports;

class PipeListener
{

    static void Main(string[] args)
    {
        Console.WriteLine("\n***Named piper server example ***\n");        
        NamedPipeServerStream pipe = new NamedPipeServerStream("/tmp/test");
        pipe.WaitForConnection();
        StreamReader reader = new StreamReader(pipe);
        int i = 0;
        String tty = "";
        String cmd = "";
        while(i < 10)
        {
            var line = reader.ReadLine();
            tty = Command.getTTY(line);
            cmd = Command.getCommand(line);
            echoCommand(tty, cmd); 
            Console.WriteLine("Recevied command: " +line);
            i++;
        }
        pipe.Close();
    }

    public static void echoCommand(String tty, String command)
    {
        if (tty != "" && File.Exists(tty))
        {
            String output = String.Format("\nYour command was: {0}\n", command);
            byte[] bytes = new byte[output.Length * sizeof(char)];
            System.Buffer.BlockCopy(output.ToCharArray(), 0, bytes, 0, 
                                    bytes.Length);

            /*SerialPort port = new SerialPort(tty, 38400);
            port.Open();
            if (port.IsOpen)
            {
                Console.WriteLine("Echoing command");
                port.Write(output);     
            }
            */

            //File.WriteAllText(tty, output);

            //File.AppendAllText(tty, output); 

            FileStream file = new FileStream(tty, FileMode.Open);
            file.Write(bytes, 0, bytes.Length);
        }
    }
}

class Command
{
    public static String getTTY(String str)
    {
        String[] words = str.Split(' ');
        return words[0];
    }

    public static String getCommand(String str)
    {
        String[] words = str.Split(' ');
        String[] cmd = new String[words.Length-1];
        Array.Copy(words,1,cmd,0, words.Length - 1);
        return String.Join(" ", cmd);
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明问题在于我使用FileStream的方式。 FileStream需要使用正确的文件共享打开PTS。我将在下面发布我的解决方案:

using System;
using System.IO;
using System.IO.Pipes;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;

class PipeListener
{

    static void Main(string[] args)
    {
        Console.WriteLine("\n***Named piper server example ***\n");
        NamedPipeServerStream pipe = new NamedPipeServerStream("/tmp/test");
        pipe.WaitForConnection();
        StreamReader reader = new StreamReader(pipe);
        int i = 0;
        String tty = "";
        String cmd = "";
        while(i < 10)
        {
            Console.WriteLine("Waiting for pipe.");
            var line = reader.ReadLine();
            tty = Command.getTTY(line);
            cmd = Command.getCommand(line);

            echoCommand(tty, cmd);
            Console.WriteLine("Recevied command: " +line);
            i++;
        }
        pipe.Close();
    }

    public static void echoCommand(String tty, String command)
    {
        if (tty != "" && File.Exists(tty))
        {
            String output = String.Format("\nYour command was: {0}\n", command);
            byte[] bytes = new byte[output.Length * sizeof(char)];
            System.Buffer.BlockCopy(output.ToCharArray(), 0, bytes, 0,
                                    bytes.Length);

            FileStream term = new FileStream(tty, FileMode.Open,
                    FileAccess.Write, FileShare.Write);

            term.Write(bytes, 0, bytes.Length);
            term.Flush(true);
            term.Dispose();
        }
    }
}

class Command
{
    public static String getTTY(String str)
    {
        String[] words = str.Split(' ');
        return words[0];
    }

    public static String getCommand(String str)
    {
        String[] words = str.Split(' ');
        String[] cmd = new String[words.Length-1];
        Array.Copy(words,1,cmd,0, words.Length - 1);
        return String.Join(" ", cmd);
    }
}