我试图让一个按钮检查文件的每一行,openFileDialog1.FileName
它是否包含字符串之一,“LCD”或“laser”和“on”在同一行,或者它是否包含“laser”和“off”在同一行和字符串“.end”上,然后做一些事情。
我是 C# 新手(本周开始),也不是以英语为母语的人。
我的目标是让我的 Arduino 机器人手臂(我的第一个构建,所以它非常简单)有点可编程,只是为了控制 LCD 并打开或关闭激光(到目前为止)。
顺便说一句,这只是模拟器,所以这就是它从不发送任何串行数据的原因。
下面是问题所在的代码片段,问题是当我在模拟器中“运行”代码时,它似乎正在一次检查所有行,因为在它检查的代码中,这是
LCD = hello
laser = on
LCD = 000
laser = off
它只将 LCD 设置为 000,我之前单独检查了激光 = 代码,它在那里没有工作,但是当我尝试它时private void Form3_Load(object sender, EventArgs e)
它工作得很好,所以底线每个代码中的最后一个 LCD 命令有效,激光代码从不工作。
我也想让每行代表 1 秒,所以每行需要一秒钟才能继续下一行。
间隔为timer1
1000(一秒)
private void timer1_Tick(object sender, EventArgs e)
{
int lineNumber = richTextBox1.GetLineFromCharIndex(richTextBox1.TextLength);
string[] lines = File.ReadAllLines(openFileDialog1.FileName);
try
{
for (int i = 0; i < lineNumber; i++)
{
if (lines[i].Contains("LCD"))
{
label1.Text = lines[i].Remove(0, 6);
}
if (lines[i].Contains("laser") && lines[i].Contains("On"))
{
pictureBox4.Show();
}
if (lines[i].Contains("laser") && lines[i].Contains("Off"))
{
pictureBox4.Hide();
}
if (lines[i].Contains(".end"))
{
button2.PerformClick();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Form3", MessageBoxButtons.OK, MessageBoxIcon.Error);
}