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_3 { public partial class Form1 : Form { /*initial graphic varible*/ Graphics g; Pen mesh; Pen line; Bitmap img; int step = 0; int speed_value;//get current speed //get currnet x,y axis encoder pulse int cur_x; int cur_y; //next position int next_x; int next_y; public Form1() { InitializeComponent(); // mention message MessageBox.Show("Please start your ISaGRAF application before press start button.", "Button Clicked", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); /*setting timer tick*/ timer1.Interval = 250; timer1.Enabled = true; /*initial position*/ cur_x=0; cur_y=240; /*create a bitmap canvas*/ img = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height); g = Graphics.FromImage(img); pictureBox1.Image = img; g.Clear(Color.White); draw_mesh();/*drawing mesh */ } private void timer1_Tick(object sender, EventArgs e) { ScanProperties();//scan all integer properties. Scan_XDI();//scan x axis D/I status in ISaGRAF Scan_YDI();//scan y axis D/I status in ISaGRAF } private void ScanProperties() { int Value = 0; string position; byte tmp=0; //Get current x and y axis logic pulse. Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(12), out next_x); position = "( "+next_x.ToString()+" , "; next_x = next_x / 5000; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(14), out next_y); position += next_y.ToString()+" )"; cur_pos.Text = position.ToString(); next_y = 240 - (next_y / 5000);//orignal point transition if (((cur_x - next_x) * (cur_x - next_x) > 0) || ((cur_y - next_y) * (cur_y - next_y) > 0)) { line_draw(); cur_x = next_x; cur_y = next_y; } /*swap position*/ /*read speed*/ Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(16), out speed_value); speed.Text = speed_value.ToString(); /*show status bar*/ Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(18), out step); Quicker.UserShare.UserGetCoil(Convert.ToUInt16(28), out tmp); //check emergency status if (tmp == 0) { emg.BackColor = Color.Black; showstep(step); } else { emg.BackColor = Color.Red; step = -1; showstep(step); } /*get next position to move*/ Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(20), out Value); position = "( "+Value.ToString()+" , "; Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(22), out Value); position += Value.ToString()+" )"; nxt_pos.Text = position.ToString(); /*get total of point*/ Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(26), out Value); Total_p.Text = (Value / 4).ToString(); } private void draw_mesh() { /*draw mesh by canvas size*/ mesh = new Pen(Color.Silver); for (int i = 1; i < 9; i++) { g.DrawLine(mesh, i * 40, 0, i * 40, 240); } for (int j = 1; j < 7; j++) { g.DrawLine(mesh, 0, j * 40, 320, j * 40); } mesh.Dispose(); } //draw line to display private void line_draw()//drawing one line { line = new Pen(Color.Blue); g.DrawLine(line, cur_x, cur_y, next_x, next_y); pictureBox1.Refresh(); line.Dispose(); } private void Scan_XDI() { byte status; //load limit+ status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(2), out status); if (status == 1) Limit_XP.BackColor = Color.Red; else Limit_XP.BackColor = Color.Black; //load limit- status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(3), out status); if (status == 1) Limit_XN.BackColor = Color.Red; else Limit_XN.BackColor = Color.Black; //load home status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(6), out status); if (status == 1) Home_X.BackColor = Color.Red; else Home_X.BackColor = Color.Black; //load near home status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(7), out status); if (status == 1) NHome_X.BackColor = Color.Red; else NHome_X.BackColor = Color.Black; } private void Scan_YDI() { byte status; //load limit+ status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(4), out status); if (status == 1) Limit_YP.BackColor = Color.Red; else Limit_YP.BackColor = Color.Black; //load limit- status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(5), out status); if (status == 1) Limit_YN.BackColor = Color.Red; else Limit_YN.BackColor = Color.Black; //load home status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(8), out status); if (status == 1) Home_Y.BackColor = Color.Red; else Home_Y.BackColor = Color.Black; //load near home status Quicker.UserShare.UserGetCoil(Convert.ToUInt16(9), out status); if (status == 1) NHome_Y.BackColor = Color.Red; else NHome_Y.BackColor = Color.Black; } private void button1_Click(object sender, EventArgs e) { //click to start int tmp; //if no file be selected. if (comboBox1.Text == "") { MessageBox.Show("Please select File ", "Button Clicked", MessageBoxButtons.OK,MessageBoxIcon.None,MessageBoxDefaultButton.Button1); return; } //select item and set value to ISaGRAF else if (comboBox1.Text == "File 1") { tmp = 1; Quicker.UserShare.UserSetReg_Long(Convert.ToUInt16(10),out tmp); Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 1); } else { tmp = 2; Quicker.UserShare.UserSetReg_Long(Convert.ToUInt16(10),out tmp); Quicker.UserShare.UserSetCoil(Convert.ToUInt16(1), 1); } pictureBox1.Refresh(); } private void showstep(int motion_step) { int Value; //show status in every status switch (motion_step) { case 0: status_bar.Text = "please press start button to start motion"; g.Clear(Color.White); draw_mesh(); break; case 1: status_bar.Text = "Detecting X-axis Near-Home-Switch and Home-Switch."; break; case 2: status_bar.Text = "Detecting X-axis Near-Home-Switch and Home-Switch."; break; case 3: Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(24), out Value); status_bar.Text = "start motion " + comboBox1.Text+" NO. "+ ((Value/4)+1).ToString()+" point."; break; case 4: status_bar.Text = "now working"; break; case 5: status_bar.Text = "auto back to home"; break; case 20: status_bar.Text = "wait 10 seconds"; break; default: status_bar.Text = "error !!! Please check your ISaGRAF I/O connection or Manual."; g.Clear(Color.White); draw_mesh(); break; } } } }