247 lines
10 KiB
C#
247 lines
10 KiB
C#
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 frmAddPlcItem : KryptonForm
|
||
{
|
||
public frmAddPlcItem()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
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); //失去焦点后发生事件
|
||
}
|
||
int modbusMinIndex = 0;
|
||
private void frmAddPlcItem_Load(object sender, EventArgs e)
|
||
{
|
||
InitControlEvent();
|
||
this.SetFormControlText();
|
||
|
||
{
|
||
kcmb_plcItemType.Items.Clear();
|
||
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemType.Name, "KryptonComboBox"))
|
||
{
|
||
kcmb_plcItemType.Items.Add(item);
|
||
}
|
||
kcmb_plcItemDataType.Items.Clear();
|
||
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemDataType.Name, "KryptonComboBox"))
|
||
{
|
||
kcmb_plcItemDataType.Items.Add(item);
|
||
}
|
||
}
|
||
kcmb_plcItemType.SelectedIndex = 0;
|
||
kcmb_plcItemDataType.SelectedIndex = 0;
|
||
kryptonPanel1.Focus();
|
||
|
||
}
|
||
/// <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 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 data type is【Y】, input PLC address can only be octal number!");
|
||
return;
|
||
}
|
||
}
|
||
int plcModbusAdd = modbusMinIndex + addPriIndex - 1;
|
||
string sqlCheck = $@"SELECT 1 FROM BAS_PLC_ITEM WHERE PLC_ITEM_CODE = '{ktb_plcItemCode.Text}'";
|
||
DataTable dtCheck = LeadDbHelperSQLite.Query(sqlCheck).Tables[0];
|
||
if (dtCheck != null && dtCheck.Rows.Count > 0)
|
||
{
|
||
//KryptonMessageBox.Show($"PLC编码:【{ktb_plcItemCode.Text}】已经被使用,请重新输入!");
|
||
KryptonMessageBox.Show($"PLC item code 【{0}】already exist, please re-enter");
|
||
return;
|
||
}
|
||
string sql = $@"
|
||
INSERT INTO [BAS_PLC_ITEM] (
|
||
[PLC_ITEM_CODE],
|
||
[PLC_ITEM_NAME],
|
||
[PLC_ITEM_TYPE],
|
||
[PLC_ITEM_DATA_TYPE],
|
||
[PLC_ITEM_DATA_LENGTH],
|
||
[PLC_ITEM_PRI_ADDRESS],
|
||
[PLC_ITEM_MODBUS_ADDRESS],
|
||
[PLC_ITEM_DEVIATION_ADDRESS],
|
||
[PLC_ITEM_VALUE],
|
||
[USED_FLAG],
|
||
[DEL_FLAG],
|
||
[CREATE_BY],
|
||
[CREATE_TIME],
|
||
[UPDATE_BY],
|
||
[UPDATE_TIME],
|
||
[DATA_VERSION],
|
||
[REMARKS1],
|
||
[REMARKS2],
|
||
[REMARKS3],
|
||
[REMARKS4],
|
||
[REMARKS5]
|
||
)
|
||
VALUES
|
||
(
|
||
'{ktb_plcItemCode.Text}',
|
||
'{ktb_plcItemName.Text}',
|
||
'{kcmb_plcItemType.SelectedIndex}',
|
||
'{kcmb_plcItemDataType.SelectedIndex}',
|
||
'{0}',
|
||
'{addPriIndex}',
|
||
'{plcModbusAdd}',
|
||
'{Convert.ToInt32(knud_plcItemDeviationAddress.Value)}',
|
||
NULL,
|
||
1,
|
||
0,
|
||
'{GlobalHelper.UserInfo.UserId}',
|
||
datetime('now', 'localtime'),
|
||
'{GlobalHelper.UserInfo.UserId}',
|
||
datetime('now', 'localtime'),
|
||
0,
|
||
NULL,
|
||
NULL,
|
||
NULL,
|
||
NULL,
|
||
NULL
|
||
);
|
||
";
|
||
int row = LeadDbHelperSQLite.ExecuteSql(sql);
|
||
if (row > 0)
|
||
{
|
||
//KryptonMessageBox.Show("添加PLC变量数据成功!");
|
||
KryptonMessageBox.Show(this.GetInfoText("新增PLC变量数据成功!", "AddSuccessInfo"));
|
||
frmPlcItemConfig.RefreshFrmHost();
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
//KryptonMessageBox.Show("添加PLC变量数据失败!");
|
||
KryptonMessageBox.Show(this.GetInfoText("新增PLC变量数据失败!", "AddFailInfo"));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
KryptonMessageBox.Show(this.GetInfoText("新增PLC变量数据发生异常", "AddPlcItemDataExp") + $":【{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();
|
||
}
|
||
}
|
||
}
|