我正在制作使用CAD绘图生成G代码的SW,然后将其发送到arduino。到目前为止一切都那么好,一切都在arduino(Arduino UNO,我使用Arduino IDE)上工作,除了一件事。在一种形式中,用于实际运行G代码并显示工具的位置。当我运行程序时,C#app发送一行G代码,Arduino接收它,运行它。它发送了一些步骤,它已经完成,它工作并显示正常。完成后,它会发送一条"OK"
消息(确实如此),之后该消息代码应发送另一行G-Code,但事实并非如此。谁能帮助我?感谢
C#中的代码: 单击运行按钮:
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button1.Text = "<RUNNING>";
Thread.Sleep(2000);
while (!port.IsOpen)
{
}
if (port.IsOpen)
{
data_comm();
}
}
读取端口:
public void Read()
{
while (port.IsOpen)
{
try
{
string message = port.ReadLine();
if (message == "OK")
{
data_comm();
}
else
{
SetText(message);
}
}
catch (TimeoutException) { }
}
}
发送数据(G-Code):
public void data_comm()
{
textBox3.Text = SortedList;
string[] word = SortedList.Split(' ');
if (word[0] == "G1")
{
string x = Math.Round(double.Parse(word[1].Remove(0, 1), CultureInfo.InvariantCulture) / precission_X_Y, 0, MidpointRounding.ToEven).ToString().PadLeft(6, '0');
string y = Math.Round(double.Parse(word[2].Remove(0, 1), CultureInfo.InvariantCulture) / precission_X_Y, 0, MidpointRounding.ToEven).ToString().PadLeft(6, '0');
port.WriteLine("#G01X" + x + "Y" + y);
}
else if (word[0] == "G0")
{
string x = Math.Round(double.Parse(word[1].Remove(0, 1), CultureInfo.InvariantCulture) / precission_X_Y, 0, MidpointRounding.ToEven).ToString().PadLeft(6, '0');
string y = Math.Round(double.Parse(word[2].Remove(0, 1), CultureInfo.InvariantCulture) / precission_X_Y, 0, MidpointRounding.ToEven).ToString().PadLeft(6, '0');
port.WriteLine("#G00X" + x + "Y" + y);
}
i++;
}
从Arduino读取位置:
private void SetText(string text)
{
Graphics g = pictureBox1.CreateGraphics();
g.TranslateTransform(0, pictureBox1.Height);
g.ScaleTransform(0.3f, -0.3f);
if (this.textBox1.InvokeRequired && this.textBox2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
try
{
x_pos = Single.Parse(text.Substring(1, 6)) * 0.07f;
y_pos = Single.Parse(text.Substring(8, 6)) * 0.07f;
this.textBox1.Text = Math.Round(x_pos, 0, MidpointRounding.ToEven).ToString();
this.textBox2.Text = Math.Round(y_pos, 0, MidpointRounding.ToEven).ToString();
g.DrawRectangle(p, x_pos, y_pos, 1, 1);
string[] word = SortedList[i].Split(' ');
}
catch { }
}
}