P.1 Code Software C# chường trình điều khiểm máy CNC 3D using System; using System.Windows.Forms; using System.IO.Ports; using System.IO; using System.Media; namespace giaodiendktd { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int intlen = 0; // luu gia tri cong com ket noi vao may tinh int m; string trucX; string trucY; string trucZ; string lapthuN;//(thông số gửi cho VĐK) public SoundPlayer baoloi = new SoundPlayer("baodong.wav"); public SoundPlayer ketthuc = new SoundPlayer("hoanthanh.wav"); private void groupBox1_Enter(object sender, EventArgs e) { } private void groupBox3_Enter(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { TTA1.Clear(); TTA2.Clear(); TTA3.Clear(); TTB1.Clear(); TTB2.Clear(); TTB3.Clear(); txtnhan.Clear(); TXTF.Clear(); } private void label1_Click(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { } private void groupBox4_Enter(object sender, EventArgs e) { } private void Pbconnect_Click(object sender, EventArgs e) { if (LbStatus.Text == "Disconnect") { Com.PortName = CbSecCom.Text; Com.Open(); LbStatus.Text = "Connect"; Pbconnect.Text = "Disconnect"; } else { Com.Close(); LbStatus.Text = "Disconnect"; Pbconnect.Text = "Connect"; } } int tdo; private void timer1_Tick(object sender, EventArgs e) { String[] ports = SerialPort.GetPortNames(); if (intlen != ports.Length) { intlen = ports.Length; CbSecCom.Items.Clear(); for (int j = 0; j < intlen; j++) { CbSecCom.Items.Add(ports[j]); } } tdo = tocdo.Value; // vtx.Text = tdo.ToString(); } private void CbSecCom_SelectedIndexChanged(object sender, EventArgs e) { } private void guitt_Click(object sender, EventArgs e) { if (LbStatus.Text == "Connect") { if (TTA1.Text == "" || TTA2.Text == "" || TTA3.Text == "" || TTB1.Text == "" || TTB2.Text == "" || TTB3.Text == "" || TXTF.Text == "") { MessageBox.Show("Chưa có liệu!", "Thông Báo"); } else { float a = float.Parse(TTA1.Text); float b = float.Parse(TTA2.Text); float c = float.Parse(TTA3.Text); float A = float.Parse(TTB1.Text); float B = float.Parse(TTB2.Text); float C = float.Parse(TTB3.Text); float F = float.Parse(TXTF.Text); noisuytuyentinh(a, b, c, A, B, C,F); } } else { MessageBox.Show("COM chưa mở.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void TTA1_TextChanged(object sender, EventArgs e) { } private void TTA2_TextChanged(object sender, EventArgs e) { } // ***** Gửi liệu nội suy cung tròn************* private void guict_Click(object sender, EventArgs e) { if (LbStatus.Text == "Connect") { if (CTA1.Text == "" || CTA2.Text == "" || CTB1.Text == "" || CTB2.Text == "") { MessageBox.Show("Chưa có liệu!", "Thông Báo"); } else { float a = float.Parse(CTA1.Text); float b = float.Parse(CTA2.Text); float A = float.Parse(CTB1.Text); float B = float.Parse(CTB2.Text); noisuycungtron(a, b, A, B, y); } } else { MessageBox.Show("COM chưa mở.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //********************** Xóa liệu cung tròn************* private void xoact_Click(object sender, EventArgs e) { baoloi.Play(); } private void xoadt_Click(object sender, EventArgs e) { txtnhan.Clear(); } private void guidt_Click(object sender, EventArgs e) { string q; if (LbStatus.Text == "Connect") { if (bluZ.Text == "" || bluX.Text == "" || bluY.Text == "" ) { MessageBox.Show("Chưa có liệu!", "Thơng Báo"); } else { q = bluZ.Text; Com.WriteLine(q); q = bluX.Text; Com.WriteLine(q); q = bluY.Text; Com.WriteLine(q); } } else { MessageBox.Show("COM chưa mở.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void label8_Click(object sender, EventArgs e) { } //********************************************************** Action comaction; private void oncom(object sender, SerialDataReceivedEventArgs e) { comaction = nhan; try { this.BeginInvoke(comaction, Com.ReadExisting()); } catch { } } //***************** Chương trình tính bạc ******************* // Chuong trinh tinh can vi dieu khien float canbachai(float a) { float x, i, y; x = 1; for (i = 1; i = Xf) { Axf = X0; } else Axf = Xf; if (Y0 >= Yf) { Ayf = Y0; } else Ayf = Yf; Ax = * X0 + 1; Ay = -2 * Y0 + 1; D = 0; { if (D > 0) { D = D + Ay; Ay = Ay + 2; Ayf = Ayf - 1; if (chieu == "1") { mangy[dem1 + 1] = mangy[dem1] - BLU; mangx[dem1 + 1] = mangx[dem1]; dem1++; } if (chieu == "2") { mangy[dem1 + 1] = mangy[dem1]; mangx[dem1 + 1] = mangx[dem1] - BLU; dem1++; } } if (D 0){ solanthieux=lanlapN/ttxn;} if(ttyn>0){solanthieuy = lanlapN / ttyn;} thieux = (ttx - ttxn) * solanthieux; // vtx.Text = thieux.ToString(); thieuy = (tty - ttyn) * solanthieuy; // vtx.Text = thieuy.ToString(); if (thieux >= thieuy) { lanlapN = (lanlapN + (int)thieux); } if (thieuy > thieux) { lanlapN = (lanlapN + (int)thieuy); } // chằn tọa độ x,y,z, vào if (nhox==0) { trucX = "X+" + ttxn.ToString(); } if (nhox==1) { trucX = "X-" + ttxn.ToString(); } if (nhoy==0) { trucY = "Y+" + ttyn.ToString(); } if (nhoy==1) { trucY = "Y-" + ttyn.ToString(); } // if (nhoz==0) { trucZ = "Z+" + ttzn.ToString(); } // if (nhoz==1) { trucZ = "Z-" + ttzn.ToString(); } if ((xd0==1)&&(xd1==0)) { trucZ = "Z+" + chieusau.ToString(); } if ((xd0 == 0) && (xd1 == 1)) { trucZ = "Z-" + chieusau.ToString(); } if ((xd0 != 1) && (xd1 == 0)) { trucZ = "Z+0"; } if ((xd0 == 0) && (xd1 != 1)) { trucZ = "Z-0"; } lapthuN = "L"+lanlapN.ToString(); // truyen liệu màng hình bluX.Text = trucX; bluY.Text = trucY; bluZ.Text = trucZ; vonglap.Text = lapthuN; xacdinhmag = 1; Com.Write("?");// gửi liệu đến vi điều khiển } /// /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////// /// private void G01() { float x = float.Parse(TTA1.Text); float y = float.Parse(TTA2.Text); float z = float.Parse(TTA3.Text); float a = float.Parse(TTB1.Text); float b = float.Parse(TTB2.Text); float c = float.Parse(TTB3.Text); float Tipo; float Vf = 10; // chương trinh offset // dai duong thang AB double L = Math.Sqrt(Math.Abs(a - x) * Math.Abs(a - x) + Math.Abs(b - y) * Math.Abs(b - y) + Math.Abs(c - z) * Math.Abs(c - z)); gocL.Text = L.ToString(); Tipo = (float.Parse(V.Text) / 60f) / float.Parse(minstepx.Text); Tipo = (1 / Tipo); // dai phep noi suy float F = float.Parse(TXTF.Text); float AL = Vf * (F / 60F) * (Tipo); nhom.Text = AL.ToString(); // Do dai dich chuyen cua moi truc double Ax = (AL / L) * (a - x); double Ay = (AL / L) * (b - y); double Az = (AL / L) * (c - z); gocX.Text = Ax.ToString(); gocY.Text = Ay.ToString(); gocZ.Text = Az.ToString(); // So vong lap noi suy N double T = L / (Vf * F / 60F); double N1 = (T / Tipo); int N = (int)N1; // Gửi liệu tính xung cho trục động tinhxung(Ax, Ay, Az, N); xacdinhmag = 1; } private void tam_Click_1(object sender, EventArgs e) { } // Ham dừng máy private void STOP_Click(object sender, EventArgs e) { if (LbStatus.Text == "Connect") { Com.Write("s"); } else MessageBox.Show("Bạn chưa kết nối cổng Com !"); } private void test_Click(object sender, EventArgs e) { if (LbStatus.Text == "Connect") { Com.Write("o"); } else MessageBox.Show("Bạn chưa kết nối cổng Com !"); } String[] CT = new String[100000]; Int32 tangdem = 0; /*//////////////////////////////////////G02 viết theo bán kính///////////////////////////////////// private void G02() { xacdinhmag = 2; float X0 = float.Parse(TTA1.Text); float Y0 = float.Parse(TTA2.Text); float Xf = float.Parse(CTA1.Text); float Yf = float.Parse(CTA2.Text); double R = double.Parse(CTB2.Text); float blu = float.Parse(minstepx.Text) * 4; float Xj, Yj; double R = Math.Sqrt(Math.Pow(X0 - Xj, 2) + Math.Pow(Y0 - Yj, 2)); X0 = X0 - Xj; Y0 = Y0 - Yj; Xf = Xf - Xj; Yf = Yf - Yj; float N; double Xk, Yk, Dk, tang = blu; int GOC = 0; it = 1; // xác định góc phần tư if (Xf > X0) { if (Y0 > Yf) { GOC = 1; } if (Y0 < Yf) { GOC = 2; } } if (Xf < X0) { if (Yf < Y0) { GOC = 4; } if (Yf > Y0) { GOC = 3; } } ////////////////////////////////////// Tính toan /////////////////////////////////////////// N = (Math.Abs(Xf - X0) + Math.Abs(Yf - Y0)); Xk = X0; Yk = Y0; { tangdem++; Dk = ((Xk * Xk + Yk * Yk) - (R * R)); if (Dk Yf) { GOC=1; } if (Y0 Y0) { GOC = 3; } } ///////////////////////// Tính toan /////////////////////////////////////////// N = (Math.Abs(Xf - X0) + Math.Abs(Yf - Y0)); Xk = X0; Yk = Y0; { tangdem++; Dk=((Xk*Xk+Yk*Yk)-(R*R)); if (Dk Yf) { GOC=3; } if (Y0 Y0) { GOC = 1; } } ///////////////////////// Tính toan /////////////////////////////////////////// N = (Math.Abs(Xf - X0) + Math.Abs(Yf - Y0)); Xk = X0; Yk = Y0; { tangdem++; Dk=((Xk*Xk+Yk*Yk)-(R*R)); if (Dk