218 lines
8.8 KiB
C#
218 lines
8.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace PLCCommunication
|
|
{
|
|
public partial class FrmOmronPLCCom : Form
|
|
{
|
|
/// <summary>
|
|
/// 定义MelsecPLCUI自定义控件List集合
|
|
/// </summary>
|
|
public List<OmronPLCUI> lstMcUI = new List<OmronPLCUI>();
|
|
/// <summary>
|
|
/// 配置文件实体类
|
|
/// </summary>
|
|
private List<PLCConfig> lstPLCConfig = new List<PLCConfig>();
|
|
/// <summary>
|
|
/// PLC读取寄存器配置文件
|
|
/// </summary>
|
|
private string ConfigPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "Config\\PlcConfig.ini");
|
|
|
|
/// <summary>
|
|
/// 加载lstMcUI窗口次数
|
|
/// </summary>
|
|
private int ComCount;
|
|
|
|
public FrmOmronPLCCom(string path)
|
|
{
|
|
InitializeComponent();
|
|
ConfigPath = path;
|
|
ComCount = int.Parse(new IniHelper(ConfigPath).IniReadValue("SystemConfig", "ComCount"));
|
|
ReadPlcConfig(ComCount);
|
|
LoadUI(ComCount);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 读取配置文件
|
|
/// </summary>
|
|
/// <param name="Count"></param>
|
|
public void ReadPlcConfig(int Count)
|
|
{
|
|
try
|
|
{
|
|
for (int j = 0; j < Count; j++)
|
|
{
|
|
PLCConfig plcConfig = new PLCConfig();
|
|
plcConfig.Index = j + 1;
|
|
plcConfig.IP = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "IP");
|
|
plcConfig.Port = int.Parse(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "Port"));
|
|
plcConfig.JobCount = int.Parse(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "JobCount"));
|
|
plcConfig.HeartBeat = bool.Parse(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "HeartBeat"));
|
|
plcConfig.HeartAddr = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "HeartAddr");
|
|
plcConfig.ScanTime = int.Parse(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "ScanTime"));
|
|
plcConfig.Solt = int.Parse(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", "Solt"));
|
|
List<TriggerParams> lstTgrParamsTemp = new List<TriggerParams>();
|
|
for (int i = 0; i < plcConfig.JobCount; i++)
|
|
{
|
|
TriggerParams trigger = new TriggerParams();
|
|
trigger.Index = i + 1;
|
|
trigger.ThreadName=new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#ThreadName");
|
|
trigger.TriggerAddr = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#RecvAddr");
|
|
string recvType = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#RecvType");
|
|
trigger.TriggerType = (VarType)Enum.Parse(typeof(VarType), recvType, false);
|
|
string bb = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#IsRead");
|
|
|
|
if (bb == "True")
|
|
{
|
|
trigger.IsRead = true;
|
|
}
|
|
else
|
|
{
|
|
trigger.IsRead = false;
|
|
}
|
|
|
|
trigger.ReadAddr = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#ReadAddr");
|
|
trigger.ReadLength =Convert.ToInt16(new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#ReadLength"));
|
|
string readType = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#ReadType");
|
|
trigger.ReadType = (VarType)Enum.Parse(typeof(VarType), readType, false);
|
|
trigger.TriggerCmd = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#TriggerCmd");
|
|
trigger.ResultAddr = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#WriteAddr");
|
|
string writeype = new IniHelper(ConfigPath).IniReadValue(plcConfig.Index + "#PLCParameter", i + 1 + "#WriteType");
|
|
trigger.ResultType = (VarType)Enum.Parse(typeof(VarType), writeype, false);
|
|
lstTgrParamsTemp.Add(trigger);
|
|
}
|
|
plcConfig.lstTgrParams = lstTgrParamsTemp;
|
|
lstPLCConfig.Add(plcConfig);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 加载MelsecPLCUI窗口
|
|
/// </summary>
|
|
/// <param name="Count"></param>
|
|
public void LoadUI(int Count)
|
|
{
|
|
tabCt_MePlcUI.TabPages.Clear();
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
tabCt_MePlcUI.TabPages.Add(i + 1 + "#通讯模块");
|
|
Panel panelUI = new Panel();
|
|
panelUI.Dock = DockStyle.Fill;
|
|
tabCt_MePlcUI.TabPages[i].Controls.Add(panelUI);
|
|
OmronPLCUI plcUI = new OmronPLCUI(lstPLCConfig[i]);
|
|
plcUI.Dock = DockStyle.Fill;
|
|
plcUI.SaveParamsEvent += plcUI_SaveParamsEvent;
|
|
lstMcUI.Add(plcUI);
|
|
panelUI.Controls.Add(plcUI);
|
|
}
|
|
}
|
|
|
|
|
|
#region 参数保存
|
|
/// <summary>
|
|
/// 循环保存参数
|
|
/// </summary>
|
|
/// <param name="plcIndex"></param>
|
|
/// <param name="trgIndex"></param>
|
|
private void plcUI_SaveParamsEvent(int plcIndex, int trgIndex)
|
|
{
|
|
if (trgIndex == 0)
|
|
{
|
|
for (int i = 0; i < lstMcUI[plcIndex - 1].JobCount; i++)
|
|
{
|
|
SaveTrgParam(plcIndex, i + 1, lstMcUI[plcIndex - 1].lstTrgUI[i].TrgParams);
|
|
}
|
|
SavePLCParam(plcIndex);
|
|
}
|
|
else
|
|
{
|
|
SaveTrgParam(plcIndex, trgIndex, lstMcUI[plcIndex - 1].lstTrgUI[trgIndex - 1].TrgParams);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 界面设置参数写入到配置文件
|
|
/// </summary>
|
|
/// <param name="plcIndex"></param>
|
|
/// <param name="trgIndex"></param>
|
|
/// <param name="triggerParams"></param>
|
|
public void SaveTrgParam(int plcIndex, int trgIndex, TriggerParams triggerParams)
|
|
{
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#RecvAddr", triggerParams.TriggerAddr);
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#RecvType", triggerParams.TriggerType.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#WriteAddr", triggerParams.ResultAddr);
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#WriteType", triggerParams.ResultType.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#TriggerCmd", triggerParams.TriggerCmd);
|
|
if (triggerParams.IsRead)
|
|
{
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#IsRead", "True");
|
|
}
|
|
else
|
|
{
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#IsRead", "False");
|
|
}
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#IsRead", triggerParams.IsRead==true?"True":"False");
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#ReadAddr", triggerParams.ReadAddr);
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#ReadType", triggerParams.ReadType.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", trgIndex + "#ReadLength", triggerParams.ReadLength.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="plcIndex"></param>
|
|
public void SavePLCParam(int plcIndex)
|
|
{
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "Index", lstMcUI[plcIndex - 1].PlcConfig.Index.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "IP", lstMcUI[plcIndex - 1].PlcConfig.IP.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "Port", lstMcUI[plcIndex - 1].PlcConfig.Port.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "JobCount", lstMcUI[plcIndex - 1].PlcConfig.JobCount.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "HeartBeat", lstMcUI[plcIndex - 1].PlcConfig.HeartBeat.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "HeartAddr", lstMcUI[plcIndex - 1].PlcConfig.HeartAddr.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "ScanTime", lstMcUI[plcIndex - 1].PlcConfig.ScanTime.ToString());
|
|
new IniHelper(ConfigPath).IniWriteValue(plcIndex + "#PLCParameter", "Solt", lstMcUI[plcIndex - 1].PlcConfig.Solt.ToString());
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 关闭lstMcUI窗口
|
|
/// </summary>
|
|
public void ShutDown()
|
|
{
|
|
for (int i = 0; i < ComCount; i++)
|
|
{
|
|
if (lstMcUI[i] != null)
|
|
{
|
|
lstMcUI[i].Shutdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭本窗口
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void FrmOmronPLCCom_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
this.Visible = false;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
}
|