我目前在我的Raspberry Pi上使用DAQC2 PiPlate,并在其上测试模拟输入。要获取输入,我调用python脚本将其返回。我的问题是我正在使用Visual Studio创建Form GUI,以便当程序在Pi上独立运行时,表单会处理该GUI(在7英寸触摸屏上运行)
我目前已尝试使用StreamReader,该计时器在每次计时器计时时都会读取脚本的输出,然后在随后更新标签。
我的PythonScript类
public PythonScript(string scriptName)
{
_scriptName = scriptName;
}
public string Run()
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "python3";
psi.Arguments = _scriptName;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = psi;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
StreamReader myStreamReader = process.StandardOutput;
string str = myStreamReader.ReadLine();
process.WaitForExit();
process.Close();
return str;
}
我目前在Form1.cs类中的工作,该类处理表单加载和计时器的位置
private void Form1_Load(object sender, EventArgs e)
{
/**
//Maximize form to fill screen
WindowState = FormWindowState.Normal;
FormBorderStyle = FormBorderStyle.None;
Location = new Point(0, 0);
TopMost = true;
Screen currentScreen = Screen.FromHandle(this.Handle);
this.Size = new System.Drawing.Size(currentScreen.Bounds.Width, currentScreen.Bounds.Height);
**/
//set up timer
timer.Interval = (10);
timer.Tick += new EventHandler(timer_tick);
timer.Start();
}
private void timer_tick(object sender, EventArgs e)
{
string value = "";
PythonScript getValue1 = new PythonScript("/home/pi/Desktop/DAQC2_Script.py");
value = getValue1.Run();
label1.Text = value;
}
我的python脚本
import piplates.DAQC2plate as x
value = str(x.getADC(0, 1))
return value
当前,我的表单可以正常构建和运行,但是它什么也没做。该标签保留默认值“ 00.0”。我确信RaspberryPi及其设备已正确连接,因为我可以执行脚本(没有return语句),并且它读取数据并允许我打印到控制台。
我只是希望将python脚本中的value
每秒更新为label1.Text
。我不仅是c#
的新手,而且还是python的新手,因此非常感谢您的帮助。
答案 0 :(得分:0)
因此,经过一段时间的挖掘,我彻底忽略了问题出在我的python脚本中的可能性。我终于有了使用process.RedirectStandardError的想法,这使我发现我的'return'语句是“功能失调”。我的return语句不仅没有特定的功能,而且我发现根本不需要它。 StreamReader无需脚本中的任何return语句即可处理读数。这是该菜鸟的学习经历。