Files
CleanMachine/CleanMachineHMI/frmChild/frmAddPlcItem.cs

247 lines
10 KiB
C#
Raw Normal View History

2021-06-05 14:17:30 +08:00
using CleanMachineHMI.FrmUserControl;
using CleanMachineHMI.Utils;
2021-12-26 21:30:02 +08:00
using Krypton.Toolkit;
2021-06-05 14:17:30 +08:00
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();
2022-03-18 21:25:34 +08:00
this.SetFormControlText();
2021-06-05 14:17:30 +08:00
{
kcmb_plcItemType.Items.Clear();
2022-03-19 16:14:32 +08:00
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemType.Name))
2021-06-05 14:17:30 +08:00
{
2022-03-18 21:25:34 +08:00
kcmb_plcItemType.Items.Add(item);
2021-06-05 14:17:30 +08:00
}
kcmb_plcItemDataType.Items.Clear();
2022-03-19 16:14:32 +08:00
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemDataType.Name))
2021-06-05 14:17:30 +08:00
{
2022-03-18 21:25:34 +08:00
kcmb_plcItemDataType.Items.Add(item);
2021-06-05 14:17:30 +08:00
}
}
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编码");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请输入PLC变量编码!", "EnterPlcItemCodeInfo"));
2021-06-05 14:17:30 +08:00
return;
}
if (string.IsNullOrEmpty(ktb_plcItemName.Text))
{
//KryptonMessageBox.Show("请输入PLC名称");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请输入PLC变量名称!", "EnterPlcItemNameInfo"));
2021-06-05 14:17:30 +08:00
return;
}
if (string.IsNullOrEmpty(kcmb_plcItemType.Text))
{
//KryptonMessageBox.Show("请选择PLC类型");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请选择PLC变量类型!", "SelectPlcItemTypeInfo"));
2021-06-05 14:17:30 +08:00
return;
}
if (string.IsNullOrEmpty(kcmb_plcItemDataType.Text))
{
//KryptonMessageBox.Show("请选择PLC数据类型");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请选择PLC变量数据类型!", "SelectPlcItemDataTypeInfo"));
2021-06-05 14:17:30 +08:00
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,
2021-12-31 16:27:14 +08:00
'{GlobalHelper.UserInfo.UserId}',
2021-06-05 14:17:30 +08:00
datetime('now', 'localtime'),
2021-12-31 16:27:14 +08:00
'{GlobalHelper.UserInfo.UserId}',
2021-06-05 14:17:30 +08:00
datetime('now', 'localtime'),
0,
NULL,
NULL,
NULL,
NULL,
NULL
);
";
int row = LeadDbHelperSQLite.ExecuteSql(sql);
if (row > 0)
{
//KryptonMessageBox.Show("添加PLC变量数据成功!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("新增PLC变量数据成功!", "AddSuccessInfo"));
2021-06-05 14:17:30 +08:00
frmPlcItemConfig.RefreshFrmHost();
this.Close();
}
else
{
//KryptonMessageBox.Show("添加PLC变量数据失败!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("新增PLC变量数据失败!", "AddFailInfo"));
2021-06-05 14:17:30 +08:00
}
}
catch (Exception ex)
{
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("新增PLC变量数据发生异常", "AddPlcItemDataExp") + $":【{ex.Message}】!");
2021-06-05 14:17:30 +08:00
}
}
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();
}
}
}