Files
CleanMachine/CleanMachineHMI/frmChild/frmEditPlcItem.cs
2022-03-18 19:06:26 +08:00

227 lines
10 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 CleanMachineHMI.FrmUserControl;
using CleanMachineHMI.Utils;
using Krypton.Toolkit;
using LeadDataBaseUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CleanMachineHMI.frmChild
{
public partial class frmEditPlcItem : KryptonForm
{
public frmEditPlcItem()
{
InitializeComponent();
_ComboBoxLanguageTextList = this.GetInfoLanguageText(GlobalHelper.LanguageCode, "KryptonComboBox");
_InfoLanguageTextList = this.GetInfoLanguageText(GlobalHelper.LanguageCode);
}
private DataTable _ComboBoxLanguageTextList;
private DataTable _InfoLanguageTextList;
public string PlcItemId { get; set; }
int modbusMinIndex = 0;
private void txt_GotFocus(object sender, EventArgs e)
{
KeyboardAutoPopUp.ShowInputPanel();
}
private void txt_LostFocus(object sender, EventArgs e)
{
KeyboardAutoPopUp.HideInputPanel();
}
private void InitControlEvent()
{
ktb_plcItemCode.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
ktb_plcItemCode.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
ktb_plcItemName.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
ktb_plcItemName.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
knud_plcItemPriAddress.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
knud_plcItemPriAddress.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
knud_plcItemDeviationAddress.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
knud_plcItemDeviationAddress.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
}
private void frmEditPlcItem_Load(object sender, EventArgs e)
{
try
{
InitControlEvent();
this.SetFormControlText();
if (GlobalHelper.LanguageCode == "English")
{
kcmb_plcItemType.Items.Clear();
foreach (DataRow item in _ComboBoxLanguageTextList.GetComBoboxItemArr(kcmb_plcItemType.Name, "KryptonComboBox"))
{
kcmb_plcItemType.Items.Add(item["CONTROL_NAME"].ToString());
}
kcmb_plcItemDataType.Items.Clear();
foreach (DataRow item in _ComboBoxLanguageTextList.GetComBoboxItemArr(kcmb_plcItemDataType.Name, "KryptonComboBox"))
{
kcmb_plcItemDataType.Items.Add(item["CONTROL_NAME"].ToString());
}
}
kcmb_plcItemType.SelectedIndex = 0;
kcmb_plcItemDataType.SelectedIndex = 0;
string sql = $@"SELECT * FROM BAS_PLC_ITEM WHERE PLC_ITEM_ID = '{PlcItemId}'";
DataTable dt = LeadDbHelperSQLite.Query(sql).Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
ktb_plcItemCode.Text = dt.Rows[0]["PLC_ITEM_CODE"].ToString();
ktb_plcItemName.Text = dt.Rows[0]["PLC_ITEM_NAME"].ToString();
kcmb_plcItemType.SelectedIndex = Convert.ToInt32(dt.Rows[0]["PLC_ITEM_TYPE"]);
kcmb_plcItemDataType.SelectedIndex = Convert.ToInt32(dt.Rows[0]["PLC_ITEM_DATA_TYPE"]);
knud_plcItemPriAddress.Value = Convert.ToInt32(dt.Rows[0]["PLC_ITEM_PRI_ADDRESS"]);
knud_plcItemDeviationAddress.Value = Convert.ToInt32(dt.Rows[0]["PLC_ITEM_DEVIATION_ADDRESS"]);
ktb_plcItemCode.Enabled = false;
kcmb_plcItemDataType_SelectionChangeCommitted(sender, e);
}
kryptonPanel1.Focus();
}
catch (Exception ex)
{
KryptonMessageBox.Show(this.GetInfoText("编辑PLC变量数据界面加载异常", "LoadEditFrmExp") + $":【{ex.Message}】!");
}
}
/// <summary>
/// 判断是否八进制格式字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public bool IsOctal(string str)
{
string PATTERN = @"[0-7]+$";
return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN);
}
private void kryptonButton1_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(ktb_plcItemCode.Text))
{
//KryptonMessageBox.Show("请输入PLC编码");
KryptonMessageBox.Show(this.GetInfoText("请输入PLC变量编码!", "EnterPlcItemCodeInfo"));
return;
}
if (string.IsNullOrEmpty(ktb_plcItemName.Text))
{
//KryptonMessageBox.Show("请输入PLC名称");
KryptonMessageBox.Show(this.GetInfoText("请输入PLC变量名称!", "EnterPlcItemNameInfo"));
return;
}
if (string.IsNullOrEmpty(kcmb_plcItemType.Text))
{
//KryptonMessageBox.Show("请选择PLC类型");
KryptonMessageBox.Show(this.GetInfoText("请选择PLC变量类型!", "SelectPlcItemTypeInfo"));
return;
}
if (string.IsNullOrEmpty(kcmb_plcItemDataType.Text))
{
//KryptonMessageBox.Show("请选择PLC数据类型");
KryptonMessageBox.Show(this.GetInfoText("请选择PLC变量数据类型!", "SelectPlcItemDataTypeInfo"));
return;
}
int addPriIndex = Convert.ToInt32(knud_plcItemPriAddress.Value);
if (kcmb_plcItemDataType.SelectedIndex == 0)
{
if (addPriIndex <= 1535)
{
modbusMinIndex = 2049;
}
else
{
modbusMinIndex = 5057;
}
}
else if (kcmb_plcItemDataType.SelectedIndex == 1)
{
modbusMinIndex = 4097;
}
else if (kcmb_plcItemDataType.SelectedIndex == 2)
{
modbusMinIndex = 1025;
if (!IsOctal(addPriIndex.ToString()))
{
//KryptonMessageBox.Show("当PLC数据类型为:【X】,输入的PLC地址只能是八进制的数字!");
KryptonMessageBox.Show("When PLC item data type is【X】, input PLC address can only be octal number!");
return;
}
}
else if (kcmb_plcItemDataType.SelectedIndex == 3)
{
modbusMinIndex = 1281;
if (!IsOctal(addPriIndex.ToString()))
{
//KryptonMessageBox.Show("当PLC数据类型为:【Y】,输入的PLC地址只能是八进制的数字!");
KryptonMessageBox.Show("When PLC item data type is【Y】, input PLC address can only be octal number!");
return;
}
}
int plcModbusAdd = modbusMinIndex + addPriIndex - 1;
string sql = $@"
UPDATE BAS_PLC_ITEM
SET PLC_ITEM_NAME = '{ktb_plcItemName.Text}',
PLC_ITEM_TYPE = '{kcmb_plcItemType.SelectedIndex}',
PLC_ITEM_DATA_TYPE = '{kcmb_plcItemDataType.SelectedIndex}',
PLC_ITEM_MODBUS_ADDRESS = '{plcModbusAdd}',
PLC_ITEM_DEVIATION_ADDRESS = '{Convert.ToInt32(knud_plcItemDeviationAddress.Value)}',
PLC_ITEM_PRI_ADDRESS = '{addPriIndex}',
UPDATE_BY = '{GlobalHelper.UserInfo.UserId}',
UPDATE_TIME = datetime('now', 'localtime'),
DATA_VERSION = DATA_VERSION + 1
WHERE
PLC_ITEM_ID = '{PlcItemId}'
";
int row = LeadDbHelperSQLite.ExecuteSql(sql);
if (row > 0)
{
//KryptonMessageBox.Show("编辑PLC变量数据成功!");
KryptonMessageBox.Show(this.GetInfoText("编辑PLC变量数据成功!", "EditSuccessInfo"));
frmPlcItemConfig.RefreshFrmHost();
this.Close();
}
else
{
//KryptonMessageBox.Show("编辑PLC变量数据失败!");
KryptonMessageBox.Show(this.GetInfoText("编辑PLC变量数据失败!", "EditFailInfo"));
}
}
catch (Exception ex)
{
KryptonMessageBox.Show(this.GetInfoText("编辑PLC变量数据发生异常", "EditPlcItemDataExp") + $":【{ex.Message}】!");
}
}
private void kcmb_plcItemDataType_SelectionChangeCommitted(object sender, EventArgs e)
{
if (kcmb_plcItemDataType.SelectedIndex == 0)
{
knud_plcItemPriAddress.Maximum = 4095;
}
else if (kcmb_plcItemDataType.SelectedIndex == 1)
{
knud_plcItemPriAddress.Maximum = 9999;
}
else if (kcmb_plcItemDataType.SelectedIndex == 2)
{
knud_plcItemPriAddress.Maximum = 377;
}
else if (kcmb_plcItemDataType.SelectedIndex == 3)
{
knud_plcItemPriAddress.Maximum = 377;
}
}
private void kryptonButton2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}