using JY.DAL; using JY.Model; using MetroFramework.Forms; using PLCCommunication; using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace JY.Inspection.Frm { public partial class FrmChangeModel : MetroForm { /// /// 数据库访问接口 /// private IDbHelper dbHelper = new OpSqlDataBase(); /// /// 定义DataGridView数据源 /// private BindingList blPLCConfigParaList = new BindingList(); private delegate void UpdateBar(int value); private FrmOmronPLCCom MelsecPLCCom; public string strModelType; //bool IsNoOrg = false; int AxisCount = 0; public FrmChangeModel(FrmOmronPLCCom melsecPLCCom, string ModelType) { InitializeComponent(); MelsecPLCCom = melsecPLCCom; strModelType = ModelType; this.dgvParaPLC.AutoGenerateColumns = false; dgvParaPLC.DataSource = blPLCConfigParaList; } private void FrmChangeModel_Load(object sender, EventArgs e) { progressBar1.Maximum = 100;//进度条 progressBar1.Step = 1; setCombOrg(); cmbProductModel.Text = strModelType; GetPLCConfigPara(strModelType); } /// /// 加载产品型号下拉 /// private void setCombOrg() { var list = dbHelper.GetProductModelList(); cmbProductModel.Items.Clear(); cmbProductModel.DataSource = list; cmbProductModel.DisplayMember = "ModelName"; cmbProductModel.ValueMember = "ModelName"; } /// /// 一键保存 /// /// /// private void btnReadPLC_Click(object sender, EventArgs e) { try { if (!HomeForm.startup) { MessageBox.Show("请先开启监控连接PLC!"); return; } if (cmbProductModel.Text == "") { MessageBox.Show("请先选择型号!"); return; } ChangeButtonStatus(false); string model = cmbProductModel.Text; if (blPLCConfigParaList.Count <= 0) { MessageBox.Show("轴参数为空不能保存", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { AxisCount = blPLCConfigParaList.Count; Task.Run(() => { ClearPos(true); for (int i = 1; i < AxisCount; i++) { try { blPLCConfigParaList[i].PLCValue = MelsecPLCCom.lstMcUI[0].ReadIntDReg(blPLCConfigParaList[i].PLCAddress); blPLCConfigParaList[i].UpdateData = DateTime.Now;//更新数据时间 } catch (Exception ex) { blPLCConfigParaList[i].PLCValue = 0;//没有值的话,给予0值 blPLCConfigParaList[i].UpdateData = DateTime.Now;//更新数据时间 } Thread.Sleep(100); ChangeBar(i); } Thread.Sleep(100); if (this.InvokeRequired) { Action w = colosbar; this.Invoke(w); } else { progressBar1.Visible = false; metroLabel2.Text = string.Empty; metroLabel2.Visible = false; } var result = dbHelper.InsertPLCConfigParam(blPLCConfigParaList.ToList()); MessageBox.Show("保存成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); ChangeButtonStatus(true); ClearPos(false); }); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// /// 一键换型 /// /// /// private void btnChangeConfig_Click(object sender, EventArgs e) { if (!HomeForm.startup) { MessageBox.Show("请先开启监控连接PLC!"); return; } if (MessageBox.Show("确定要更改" + cmbProductModel.Text + "型号吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { return; } try { if (blPLCConfigParaList.Count > 0) { ChangeButtonStatus(false); AxisCount = blPLCConfigParaList.Count; Task.Run(() => { ClearPos(true); for (int i = 0; i < blPLCConfigParaList.Count; i++) { try { MelsecPLCCom.lstMcUI[0].WriteDReg(blPLCConfigParaList[i].PLCAddress, blPLCConfigParaList[i].PLCValue); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } Thread.Sleep(100); ChangeBar(i); } Thread.Sleep(100); if (this.InvokeRequired) { Action w = colosbar; this.Invoke(w); } else { progressBar1.Visible = false; metroLabel2.Text = string.Empty; metroLabel2.Visible = false; } }); //melsec.Write("R500", ProdTypeNum_All);//写回到PLC。型号序号下发到PLC。R500是跟黄晓宇工确定好的PLC地址,固定不变。 MessageBox.Show("一键换型成功!"); } else { MessageBox.Show("没有参数换型!"); } ChangeButtonStatus(true); ClearPos(false); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } } /// /// 打开轴参数配置页面 /// /// /// private void btnConfigBaseSet_Click(object sender, EventArgs e) { FrmConfigBaseSet frmConfigBaseSet = new FrmConfigBaseSet(); frmConfigBaseSet.ShowDialog(); } /// /// 选择产品机型事件 /// /// /// private void cmbProductModel_SelectedIndexChanged(object sender, EventArgs e) { GetPLCConfigPara(cmbProductModel.Text); } /// /// 查询当前产品型号轴参数信息 /// /// private void GetPLCConfigPara(string str) { try { var list = dbHelper.GetPLCConfigPara(str); blPLCConfigParaList = new BindingList(list); dgvParaPLC.DataSource = blPLCConfigParaList; } catch (Exception ex) { throw ex; } } #region 进度条码设置 public void colosbar() { progressBar1.Visible = false; metroLabel2.Visible = false; } public void ClearPos(bool b) { this.Invoke(new Action(() => { progressBar1.Visible = b; metroLabel2.Visible = b; })); } public void ChangeBar(int value) { if (progressBar1.InvokeRequired) { UpdateBar c = new UpdateBar(ChangeBar); this.Invoke(c, new object[] { value }); } else { if (value < AxisCount) { progressBar1.Value = Convert.ToInt16(((double)value / AxisCount) * 100); progressBar1.PerformStep(); Thread.Sleep(100); metroLabel2.Text = "已完成" + progressBar1.Value + "%"; Application.DoEvents(); } } } public void ChangeButtonStatus(bool b) { this.Invoke(new Action(() => { btnSavePLCConfig.Enabled = b; btnChangeConfig.Enabled = b; btnConfigBaseSet.Enabled = b; })); } #endregion private void dgvParaPLC_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvParaPLC.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvParaPLC.RowHeadersDefaultCellStyle.Font, rectangle, dgvParaPLC.RowHeadersDefaultCellStyle.ForeColor=Color.Gray, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } } }