Go Down

Topic: c# Arduino communication trouble (Read 299 times) previous topic - next topic

TomasUNIZA

Feb 28, 2018, 08:19 pm Last Edit: Feb 28, 2018, 08:46 pm by TomasUNIZA
Hi, I am making SW that uses CAD drawing to generate G-code, and then sends it to arduino. So far so good, everything work on arduino (Arduino UNO, I use Arduino IDE), except one thing. In one form, that is used to actually run G-code and show tool possition. When I run the program, C# app sends one line of G-code, Arduino receive it, runs it. It sends number of steps, that it have done, it works and shows ok. After it is done, it sends an "OK" message(it does) and after that message code should send another line of G-Code, but it does not. Could anyone help me? thanks

Code in C#:
Run button click:


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();

            }
        }




Read from arduino:


 public void Read()
        {
            while (port.IsOpen)
            {
                try
                {
                    string message = port.ReadLine();
                    if (message == "OK")
                    {
                        data_comm();
                    }
                    else
                    {
                        SetText(message);
                    }
                }
                catch (TimeoutException) { }
            }
        }




Send g_line:


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++;

        }



Show possition from 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.Split(' ');

                }
                catch { }               

            }
        }

Robin2

It sounds as if you are looking for advice about a C# program?

If so I suspect a C# Forum would be a more fruitful place for your enquiries.

...R

PS ... To make it easy for people to help you please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up