//struct in datablock to read . private void timer1_Tick_1(object sender, EventArgs e) { //call funtion read data try { if (plcstation.IsConnected) { double m1 = (double)plcstation.Read("DB10.DBD0", VarType.Real); double m2 = (double)plcstation.Read("DB10.DBD4", VarType.Real); double m3 = (double)plcstation.Read("DB10.DBD8", VarType.Real); double m4 = (double)plcstation.Read("DB10.DBD12", VarType.Real); double m5 = (double)plcstation.Read("DB10.DBD16", VarType.Real); //insert sql ; ////////////////////////// StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"INSERT INTO MachineRuning(TagName,Valuee,TimeStamp) VALUES"); double[] data = { m1, m2, m3, m4, m5 }; for (int i = 0; i < data.Length; i++) { string values = string.Format("({0},{1},{2}),", new string[] { "@TagName" + i.ToString(), "@Valuee" + i.ToString(), "@TimeStamp" + i.ToString() }); sbSql.Append(values); } string sql = sbSql.ToString(); sql = sql.TrimEnd(','); // remove the trailing comma ',' string conectionstring = @" Data Source = DESKTOP-TF7PT8I\WINCC; Initial Catalog = S71200; User ID = sa; Password = gcsvn"; SqlConnection conect = new SqlConnection(conectionstring); SqlCommand cmd = new SqlCommand(sql, conect);//create command conect.Open(); for (int j = 0; j < data.Length; j++) { cmd.Parameters.Add("@TagName" + j.ToString(), System.Data.SqlDbType.VarChar).Value = "ToTalTimeM_" + (j + 1).ToString(); cmd.Parameters.Add("@Valuee" + j.ToString(), System.Data.SqlDbType.Float).Value = data[j]; cmd.Parameters.Add("@TimeStamp" + j.ToString(), System.Data.SqlDbType.DateTime).Value = DateTime.Now; } //insert into bang vlues(register1,3,datetime.now),(register2,5,datetime.now),() cmd.ExecuteNonQuery(); conect.Close(); ///////////////display ////////////////////// textBox1.Text = string.Format("{0:N3}", m1); textBox2.Text = string.Format("{0:N3}", m2); textBox3.Text = string.Format("{0:N3}", m3); textBox4.Text = string.Format("{0:N3}", m4); textBox5.Text = string.Format("{0:N3}", m5); } else { textBox6.Text = "Disconnect with PLC"; } } catch (Exception ess) { MessageBox.Show(this, ess.Message); plcstation.Open(); } }
public Form1() { InitializeComponent(); timer1.Interval = 1000 * 3 * 1;// plcstation.Open(); timer1.Enabled = true; textBox6.Text = "Connected with PLC"; timer1.Start(); RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("ApplicationName", Application.ExecutablePath); //conect.Open(); }
public double ReadDouble(string variable, int decimalNumbers) { PLC.Open(); double value = Math.Round(Convert.ToDouble((uint)PLC.Read(variable)), decimalNumbers); PLC.Close(); return(value); }
public bool ReadBool(DataType dataType, int db, int startByteAdr) { PLC.Open(); bool value = Convert.ToBoolean(PLC.ReadBytes(dataType, db, startByteAdr, 1)[0]); PLC.Close(); return(value); }
public bool ReadBool(string variable) { PLC.Open(); bool value = (bool)(PLC.Read(variable)); PLC.Close(); return(value); }
public ErrorCode Connect() { ErrorCode connect_result = plc.Open(); if (connect_result == ErrorCode.NoError) { isConnect = true; } return(connect_result); }
public bool Open() { try { PLC.Open(); return(true); } catch (Exception) { return(false); } }
private void FRMCOLLECTION_Load(object sender, EventArgs e) { try { plc = new PLC(CPU_Type.S7400, "192.168.3.1", 0, 3); errCode = plc.Open(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public int ReadInt(string variable) { //int test = Int.FromByteArray((byte[])PLC.Read(variable)); PLC.Open(); var value = PLC.Read(variable); PLC.Close(); if (Convert.ToInt32(value) == 0) { return(0); } return((ushort)value); }
public bool Connect() { try { PLC.Open(); SetErrorMessageOk(); return(true); } catch (Exception ex) { SetErrorMessageBad(ex.Message); return(false); } }
// Create private void btnconnect_Click(object sender, EventArgs e) { try { plc = new PLC(CPU_Type.S71200, txtID.Text, 0, 1); if (!plc.IsAvailable) { throw new Exception("Không tìm thấy PLC cần kết nối!"); } errorState = plc.Open(); if (errorState != ExceptionCode.ExceptionNo) { throw new Exception(errorState.ToString()); } btnconnect.Enabled = false; } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); //throw ex; } }
private void btnConnection_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(txtIPaddress.Text)) { throw new Exception("Xin vui lòng nhập địa chỉ IP"); } int selectionIndex = cboxPLCs.SelectedIndex; CPU_Type cpuType = CPU_Type.S7200; string ipAddress = txtIPaddress.Text; switch (selectionIndex) { case 0: cpuType = CPU_Type.S7200; break; case 1: cpuType = CPU_Type.S7300; break; case 2: cpuType = CPU_Type.S7400; break; case 3: cpuType = CPU_Type.S71200; break; default: cboxPLCs.SelectedIndex = 3; cpuType = CPU_Type.S71200; break; } plc = new PLC(cpuType, ipAddress, (short)numericUpDownRack.Value, (short)numericUpDownSlot.Value); if (!plc.IsAvailable) { throw new Exception("Không tìm thấy PLC cần kết nối!"); } errCode = plc.Open(); if (errCode != ExceptionCode.ExceptionNo) { throw new Exception(plc.lastErrorString); } this.SetEnabledBotton(false); //BackgroundWorker START worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); //worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); //worker.DoWork += (obj, ea) => worker_DoWork(); worker.RunWorkerAsync(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error); } }