private BindingList <OPCItems> initPLCData() { BindingList <OPCItems> plcData = new BindingList <OPCItems>(); setEnabled(this.Stop, false); setEnabled(this.panelStart, false); setEnabled(this.Start, false); { string sqlConnection = "Server='" + this.DBAddr.Text + "'; Database='OPC2DBMS'; User Id='" + this.DBUser.Text + "'; Password='******'; MultipleActiveResultSets=true; Connect Timeout = 1"; var sqlQuery = "select Tag, Register, Description, PLCName from PLC2DB_Tags where type = 'VAR' and OPCName = '" + OPCName.Text + "' and PLCName = '" + PLCName.Text + "'"; try { using (SqlConnection connection = new SqlConnection(sqlConnection)) { setEnabled(this.Start, false); PlcAutoLog.Enqueue(messages["RETRIVE"]); connection.Open(); SqlCommand cmd = new SqlCommand(sqlQuery, connection); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { OPCItems oPCItems = new OPCItems(); oPCItems.Tag = sdr[0].ToString().Trim(); oPCItems.Address = sdr[1].ToString().Trim(); oPCItems.Description = sdr[2].ToString().Trim(); oPCItems.PLCName = sdr[3].ToString().Trim(); plcData.Add(oPCItems); } sdr.Close(); } PlcAutoLog.Enqueue(messages["DBSUCCESS"]); } catch (Exception ex) { PlcAutoLog.Enqueue(messages["DBFAIL"]); toggleStartButtons(false); Console.WriteLine(ex); } } return(plcData); }
private async void Start_Click(object sender, EventArgs e) { this.ControlBox = false; progressBar1.Visible = true; _isStarting = true; GlobalLogStart(); progressBar1.Visible = true; progressBar1.Style = ProgressBarStyle.Marquee; PlcAutoLog.Enqueue(messages["OPENCONN"]); _plcAuto.PLCData = await Task.Run(() => initPLCData()); var t = Task.Run(() => { while (_isStarting) { for (int i = 1; i <= 16; i++) { if (!busyLeds[i]) { refreshIndicators(i); } } Thread.Sleep(1000); } }); IntData.InitializeData(); var source = new BindingSource(_plcAuto.PLCData, null); PLCDataGrid.DataSource = source; _connOPC = "Data Source='" + this.DBAddr.Text + "'; Initial Catalog='OPC2DBMS';User id='" + this.DBUser.Text + "'; Password='******';"; _connMSS = "Data Source='" + this.DBAddr.Text + "'; Initial Catalog='MSS';User id='" + this.DBUser.Text + "'; Password='******';"; if (_ws == null) { _ws = new OPC2Queue(_connOPC, _connMSS); } if (_q2DB == null) { _q2DB = new Queue2DB(_connMSS); } toggleStartButtons(true); var t1 = Task.Run(() => { while (_isStarting) { if (PLCSimulatorSwitch.fetch) { if (PLCSimulatorSwitch.isSwitch) { ConnectButton_Click(sender, e); PLCSimulatorSwitch.fetch = false; } else { DisconnectButton_Click(sender, e); PLCSimulatorSwitch.fetch = false; } } if (OPC2MQSwitch.fetch) { if (OPC2MQSwitch.isSwitch) { OPCClientStart_Click(sender, e); } OPC2MQSwitch.fetch = false; _ws.isConnecting = OPC2MQSwitch.isSwitch; } if (MQ2DBSwitch.fetch) { if (!_q2DB.isConnecting) { Queue2DBStart_Click(sender, e); } MQ2DBSwitch.fetch = false; _q2DB.isConnecting = MQ2DBSwitch.isSwitch; } if (DSPbatchSwitch.fetch) { if (!batch.isConnecting) { DSPBatchStart_Click(sender, e); } DSPbatchSwitch.fetch = false; batch.isConnecting = DSPbatchSwitch.isSwitch; } if (PLCSimulatorSwitch.isSwitch != _plcAuto.isConnecting || PLCSimulatorSwitch.Pending) { PLCSimulatorSwitch.value = _plcAuto.isConnecting ? 1 : 0; setEnabled(PLCDashboard, _plcAuto.isConnecting); setEnabled(ConnectButton, !_plcAuto.isConnecting); setEnabled(DisconnectButton, _plcAuto.isConnecting); } if (OPC2MQSwitch.isSwitch != _ws.isConnecting || OPC2MQSwitch.Pending) { OPC2MQSwitch.value = _ws.isConnecting ? 1 : 0; setEnabled(OPCClientStop, _ws.isConnecting); setEnabled(OPCClientStart, !_ws.isConnecting); } if (MQ2DBSwitch.isSwitch != _q2DB.isConnecting || MQ2DBSwitch.Pending) { MQ2DBSwitch.value = _q2DB.isConnecting ? 1 : 0; setEnabled(Queue2DBStop, _q2DB.isConnecting); setEnabled(Queue2DBStart, !_q2DB.isConnecting); } if (DSPbatchSwitch.isSwitch != batch.isConnecting || DSPbatchSwitch.Pending) { DSPbatchSwitch.value = batch.isConnecting ? 1 : 0; setEnabled(DSPBatchStart, !batch.isConnecting); setEnabled(DSPBatchStop, batch.isConnecting); setEnabled(DSPBatchControl, !batch.isConnecting); } Thread.Sleep(200); } }); progressBar1.Visible = false; await t1; this.ControlBox = true; }
private async void refreshControls() { var t = Task.Run(() => { bool commitNow = false; while (_plcAuto.isConnecting && _isStarting) { foreach (SwitchControl item in this.PLCDashboard.Controls.OfType <SwitchControl>()) { if (!item.fetch) { if (item.value != _plcAuto.getTagValue(item.Name) || item.Pending) { item.value = _plcAuto.getTagValue(item.Name); item.quality = _plcAuto.getTagItem(item.Name) == null ? 0 : (_plcAuto.getTagItem(item.Name).Quality / 192.0); } } else { _plcAuto.queuePLCWrites(item.value, item.Name); Thread.Sleep(200); item.fetch = false; commitNow = true; } if (item.Text != _plcAuto.getTagValue(item.Name).ToString()) { this.setText(item, _plcAuto.getTagValue(item.Name).ToString()); } } foreach (TextBox item in this.PLCDashboard.Controls.OfType <TextBox>()) { if (item.Text != _plcAuto.getTagValue(item.Name).ToString()) { this.setText(item, _plcAuto.getTagValue(item.Name).ToString()); } } TimeSpan countdown = new TimeSpan(0); TimeSpan countup = new TimeSpan(0); if (_plcAuto.isCountDown) { CountDown.BackColor = Color.RoyalBlue; CountDown.ForeColor = Color.WhiteSmoke; CountUp.BackColor = Color.WhiteSmoke; CountUp.ForeColor = Color.Black; countdown = _plcAuto.countingDownTime - DateTime.Now; } else if (_plcAuto.WaitEST) { CountDown.BackColor = Color.RoyalBlue; CountDown.ForeColor = Color.WhiteSmoke; CountUp.BackColor = Color.WhiteSmoke; CountUp.ForeColor = Color.Black; } else if (_plcAuto.isCountUp) { CountUp.BackColor = Color.RoyalBlue; CountUp.ForeColor = Color.WhiteSmoke; CountDown.BackColor = Color.WhiteSmoke; CountDown.ForeColor = Color.Black; countup = DateTime.Now - _plcAuto.countingUpTime; } else { CountUp.BackColor = Color.WhiteSmoke; CountUp.ForeColor = Color.Black; CountDown.BackColor = Color.WhiteSmoke; CountDown.ForeColor = Color.Black; } if (this.CountDown.Text != countdown.ToString(@"hh\:mm\:ss")) { this.setText(this.CountDown, countdown.ToString(@"hh\:mm\:ss")); } if (this.CountUp.Text != countup.ToString(@"hh\:mm\:ss")) { this.setText(this.CountUp, countup.ToString(@"hh\:mm\:ss")); } if (commitNow) { _plcAuto.Write(); commitNow = false; } else { Thread.Sleep(500); } } }); await t; foreach (SwitchControl item in this.PLCDashboard.Controls.OfType <SwitchControl>()) { item.isSwitch = false; item.value = 0; } //after stop connections for (int i = 0; i < _plcAuto.PLCData.Count; i++) { _plcAuto.PLCData[i].Value = 0; } PlcAutoLog.Enqueue(messages["DISCONN"]); setText(Quality, ""); setEnabled(ConnectButton, true); setEnabled(DisconnectButton, false); }