通过SSH命令在树莓派上执行Python(使用Csharp和SharpSSH)

0 投票
2 回答
1681 浏览
提问于 2025-04-18 09:31

我在通过SSH执行Python脚本时遇到了问题(使用C#和SharpSSH)。

我已经成功连接,可以输入一些简单的命令,比如“startx”和“sudo shutdown ...”。

但是,当我尝试运行一个Python脚本时,程序就卡住了,什么也不发生。我用“putty”做了同样的操作,结果一切正常。

这个Python脚本可以运行,并且我能得到数据(打印 'No Laser, fast movement: Dx=', stepx, ' Dy=', stepy),这些数据对我的应用很重要。

C# SharpSSH连接

            ssh = new SshStream(host, user, password);

            //Set the end of response matcher character
            ssh.Prompt = "#";
            //Remove terminal emulation characters
            ssh.RemoveTerminalEmulationCharacters = true;

            //Reading from the SSH channel
            string response = ssh.ReadResponse();
            return response;

C#启动Gcode_executer.py不工作,导致程序卡住(在电脑上),而树莓派没有运行Python文件。

        try
        {
            ssh.Write("sudo python cnc/Gcode_executer.py");  // Write ssh to RPI
                                                             // works it in putty
            if (ssh.CanRead == true)
            {
                return ssh.ReadResponse(); // return string from terminal from RPI
            }
            else
            {
                return "No response";
            }
        }
        catch
        {
            return "Catch NO response";  
        }

我该怎么做才能解决这个问题呢?

2 个回答

0

试着在树莓派上写一个脚本,这个脚本可以运行.py文件,然后调用这个脚本。

0

现在可以用了。

我的Python脚本有一些子文件,所以必须进入那个文件夹,直到找到正确的子文件路径。

撰写回答