Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从后台运行的进程中读取标准输出_C#_Linux_Process_Bluetooth_.net Core - Fatal编程技术网

C# 无法从后台运行的进程中读取标准输出

C# 无法从后台运行的进程中读取标准输出,c#,linux,process,bluetooth,.net-core,C#,Linux,Process,Bluetooth,.net Core,因此,在Linux下,我必须通过命令rfcomm connect hci0 xx:xx:xx:xx:xx:xx连接到蓝牙设备,该命令启动蓝牙连接,但必须保持运行才能保持连接 我必须以.NET核心程序的形式编写所有内容 几秒钟后运行该命令将输出以下行: 已将/dev/rfcomm0连接到通道1上的xx:xx:xx:xx:xx:xx:xx 按CTRL-C键进行挂断 从这个输出中,我必须得到/dev/rfcomm0部分,这样我就可以用SerialPortReader读取它,如果出现问题,比如说,没有更

因此,在Linux下,我必须通过命令rfcomm connect hci0 xx:xx:xx:xx:xx:xx连接到蓝牙设备,该命令启动蓝牙连接,但必须保持运行才能保持连接

我必须以.NET核心程序的形式编写所有内容

几秒钟后运行该命令将输出以下行:
已将/dev/rfcomm0连接到通道1上的xx:xx:xx:xx:xx:xx:xx
按CTRL-C键进行挂断

从这个输出中,我必须得到
/dev/rfcomm0
部分,这样我就可以用
SerialPortReader
读取它,如果出现问题,比如说,没有更多的数据传入,我必须终止进程并重新开始,直到我有一个良好的连接

我的逻辑是这样的:

while(!Terminate)
{
   string port = Connect();
   ReadData(port);
   BTProcess.Kill();
}
不要费心于
ReadData(端口)函数,因为我的程序从来没有接近过

Connect()
如下所示:

while (!Connected)
{
    Console.WriteLine("Configuring Process");
    BTProcess = new Process();
    BTProcess.StartInfo.FileName = "rfcomm";
    BTProcess.StartInfo.Arguments = "connect hci0 xx:xx:xx:xx:xx:xx"
    BTProcess.StartInfo.RedirectStandardOutput = true;
    BTProcess.StartInfo.UseShellExecute = false;

    Console.WriteLine("Starting Process");
    BTProcess.Start();

    StreamReader reader = _BTProcess.StandardOutput;

    bool done = false;
    Console.WriteLine("Reading STDOUT now.");
    while (!done) // EDIT: If I do the while with !reader.EndOfStream then it won't even enter into the loop
    {
        Console.Write("-");
        int c = reader.Read(); // Program stops in this line

        if(c != -1)
        {
            port += (char)c;
        }

        Console.Write(c);
        if (c == 0)
        {
            port = "";
            done = true;
            _BTProcess.Kill();
        }
        if (/* String Contains Logic blabla */)
        {
            port = /* The /dev/rfcomm0 stuff */
            Connected = true;
            done = true;
        }
    }
    reader.Close();
}
return port;
我已经检查了输出是否没有重定向到类似STDErr之类的东西,但是没有,它是100%用STDOut编写的

我已经尝试过一种逻辑,比如处理StandardOutput事件的EventHandler,以及一种异步读取它的逻辑,但都没有成功。所有人都有同样的问题,他们都在
Read()处阻塞函数。我的猜测是,可能内部缓冲区没有正确刷新

也许这里有人知道我问题的答案。 注:我知道我的代码不是最好的,也不是最优化的,但它应该仍然可以工作,因为我已经在Windows下用另一个阻塞命令尝试过了,它工作了


提前感谢您对我的帮助。

我通过不直接运行命令解决了问题,因为这会引发缓冲区问题,所以我现在不运行命令
rfcomm connect hci0 xx:xx:xx:xx
,而是运行
stdbuf-o0 rfcomm connect hci0 xx:xx:xx:xx
,以避免缓冲区问题