using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace z8094_4 { public partial class Form1 : Form { //for draw line point struct draw_point { public int x1; public int y1; public int x2; public int y2; }; draw_point line_point; //for drawing picture box Graphics lin_pic; Bitmap lin_img; public Form1() { InitializeComponent(); //show message MessageBox.Show("Please start your ISaGRAF application before press start button.", "Button Clicked", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); //initial form2 //set timer timer1.Interval = 250; timer1.Enabled = true; //create bitmap and draw mesh for draw line lin_img = new Bitmap(line_pic.Size.Width, line_pic.Height); lin_pic = Graphics.FromImage(lin_img); line_pic.Image = lin_img; lin_pic.Clear(Color.White); //start drawing mesh draw_mesh(lin_pic, line_pic.Size.Width, line_pic.Size.Height,10); line_pic.Refresh(); //initial struct line line_point.x1 = 0; line_point.y1 = 240; } private void timer1_Tick(object sender, EventArgs e) { ScanDI();//scan serve on ,Drive and EMG signal. /*scan X,Y,Z DI status*/ ScanXDI(); ScanYDI(); ScanZDI(); ScanProperties(); } private void start_bt_Click(object sender, EventArgs e) { //start motion int step; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(36), out step); if (step==9) { lin_pic.Clear(Color.White); draw_mesh(lin_pic, line_pic.Size.Width, line_pic.Size.Height, 10); } line_pic.Refresh(); Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 1); } private void stop_bt_Click(object sender, EventArgs e) { //stop motion after previos motion done Quicker.UserShare.UserSetCoil(Convert.ToUInt16(2), 1); } //using function private void draw_mesh(Graphics g, int width, int height, int radix) { //drawing mesh on canvas Pen mesh = new Pen(Color.Silver); for (int i = 1; i < width / radix; i++) { g.DrawLine(mesh, i * radix, 0, i * radix, height); } for (int j = 0; j < height / radix; j++) { g.DrawLine(mesh, 0, j * radix, width, radix * j); } mesh.Dispose(); } private void draw_line(Graphics g, draw_point line_point) { Pen line = new Pen(Color.Blue); g.DrawLine(line, line_point.x1, line_point.y1, line_point.x2, line_point.y2); line.Dispose(); } private void ScanDI() { //scan all di value DRV and SRV status byte di_value = 0; Quicker.UserShare.UserGetCoil(Convert.ToUInt16(15), out di_value); if (di_value == 1) { SRV_X.BackColor = Color.Red; } else { SRV_X.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(16), out di_value); if (di_value == 1) { SRV_Y.BackColor = Color.Red; } else { SRV_Y.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(17), out di_value); if (di_value == 1) { SRV_Z.BackColor = Color.Red; } else { SRV_Z.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(18), out di_value); if (di_value == 1) { DRV_X.BackColor = Color.Red; } else { DRV_X.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(19), out di_value); if (di_value == 1) { DRV_Y.BackColor = Color.Red; } else { DRV_Y.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(20), out di_value); if (di_value == 1) { DRV_Z.BackColor = Color.Red; } else { DRV_Z.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(21), out di_value); if (di_value == 1) { EMG.BackColor = Color.Red; switch_step(-1); } else { EMG.BackColor = Color.Black; } } private void ScanProperties() { //scan all intger varible in ISaGRAF int Value; int step; string position; //read Encode X,Y,Z Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(24), out Value); position = "(" + Value.ToString() + " , "; line_point.x2 = Value/500; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(26), out Value); position += Value.ToString() + " , "; line_point.y2 =240-(Value/500); //draw line to picture box draw_line(lin_pic, line_point); line_pic.Refresh(); //swap point line_point.x1 = line_point.x2; line_point.y1 = line_point.y2; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(28), out Value); position += Value.ToString() + " )"; //show current position cur_position.Text = position.ToString(); //read Pulse to move X,Y,Z Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(30), out Value); position = "(" + Value.ToString() + " , "; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(32), out Value); position += Value.ToString() + " , "; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(34), out Value); position += Value.ToString() + " )"; //show next position nxt_position.Text = position.ToString(); Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(36), out step); switch_step(step); } private void ScanXDI() { byte di_value = 0; Quicker.UserShare.UserGetCoil(Convert.ToUInt16(3), out di_value); if (di_value == 1) { LimitP_X.BackColor = Color.Red; } else { LimitP_X.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(4), out di_value); if (di_value == 1) { LimitN_X.BackColor = Color.Red; } else { LimitN_X.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(5), out di_value); if (di_value == 1) { NHome_X.BackColor = Color.Red; } else { NHome_X.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(6), out di_value); if (di_value == 1) { Home_X.BackColor = Color.Red; } else { Home_X.BackColor = Color.Black; } } private void ScanYDI() { byte di_value = 0; Quicker.UserShare.UserGetCoil(Convert.ToUInt16(7), out di_value); if (di_value == 1) { LimitP_Y.BackColor = Color.Red; } else { LimitP_Y.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(8), out di_value); if (di_value == 1) { LimitN_Y.BackColor = Color.Red; } else { LimitN_Y.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(9), out di_value); if (di_value == 1) { NHome_Y.BackColor = Color.Red; } else { NHome_Y.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(10), out di_value); if (di_value == 1) { Home_Y.BackColor = Color.Red; } else { Home_Y.BackColor = Color.Black; } } private void ScanZDI() { byte di_value = 0; Quicker.UserShare.UserGetCoil(Convert.ToUInt16(11), out di_value); if (di_value == 1) { LimitP_Z.BackColor = Color.Red; } else { LimitP_Z.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(12), out di_value); if (di_value == 1) { LimitN_Z.BackColor = Color.Red; } else { LimitN_Z.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(13), out di_value); if (di_value == 1) { NHome_Z.BackColor = Color.Red; } else { NHome_Z.BackColor = Color.Black; } Quicker.UserShare.UserGetCoil(Convert.ToUInt16(14), out di_value); if (di_value == 1) { Home_Z.BackColor = Color.Red; } else { Home_Z.BackColor = Color.Black; } } private void switch_step(int motion_step) { //show SFC program status from step value byte di_value; switch (motion_step) { case 0: status_bar.Text = "Please press start button to start motion"; lin_pic.Clear(Color.White); draw_mesh(lin_pic, line_pic.Size.Width, line_pic.Size.Height, 10); break; case 1: status_bar.Text = "Detecting X-AXIS Near-home-switch and Home-Switch."; break; case 2: status_bar.Text = "Detecting Y-AXIS Near-home-switch and Home-Switch."; break; case 3: status_bar.Text = "Detecting Z-AXIS Near-home-switch and Home-Switch."; break; case 4: status_bar.Text = "Start Z Axis motion"; break; case 5: status_bar.Text = "Start Line interpolation move"; break; case 6: status_bar.Text = "start arc interpolation move"; break; case 7: status_bar.Text = "Return home motion(X and Y axis)"; break; case 8: status_bar.Text = "Return home motion(Z axis)"; Quicker.UserShare.UserGetCoil(Convert.ToUInt16(2), out di_value); if (di_value == 0) { lin_pic.Clear(Color.White); draw_mesh(lin_pic, line_pic.Size.Width, line_pic.Size.Height, 10); } break; case 9: status_bar.Text = "Please press start button to start motion"; break; case 20: status_bar.Text = "Wait a second"; break; default: status_bar.Text = "error!!! Please check your ISaGRAF I/O connection or Manual."; lin_pic.Clear(Color.White); draw_mesh(lin_pic, line_pic.Size.Width, line_pic.Size.Height, 10); break; } } } }