Files
1440WGJ/JY.Inspection/Frm/FrmChangeModel.cs
T
2026-07-14 13:55:17 +08:00

310 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
/// <summary>
/// 数据库访问接口
/// </summary>
private IDbHelper dbHelper = new OpSqlDataBase();
/// <summary>
/// 定义DataGridView数据源
/// </summary>
private BindingList<PLCConfigPara> blPLCConfigParaList = new BindingList<PLCConfigPara>();
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);
}
/// <summary>
/// 加载产品型号下拉
/// </summary>
private void setCombOrg()
{
var list = dbHelper.GetProductModelList();
cmbProductModel.Items.Clear();
cmbProductModel.DataSource = list;
cmbProductModel.DisplayMember = "ModelName";
cmbProductModel.ValueMember = "ModelName";
}
/// <summary>
/// 一键保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 一键换型
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
/// <summary>
/// 打开轴参数配置页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConfigBaseSet_Click(object sender, EventArgs e)
{
FrmConfigBaseSet frmConfigBaseSet = new FrmConfigBaseSet();
frmConfigBaseSet.ShowDialog();
}
/// <summary>
/// 选择产品机型事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbProductModel_SelectedIndexChanged(object sender, EventArgs e)
{
GetPLCConfigPara(cmbProductModel.Text);
}
/// <summary>
/// 查询当前产品型号轴参数信息
/// </summary>
/// <param name="str"></param>
private void GetPLCConfigPara(string str)
{
try
{
var list = dbHelper.GetPLCConfigPara(str);
blPLCConfigParaList = new BindingList<PLCConfigPara>(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);
}
}
}