239 lines
14 KiB
C#
239 lines
14 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 SimpleCommunication
|
||
|
|
{
|
||
|
|
public partial class FrmCommunication : Form
|
||
|
|
{
|
||
|
|
public List<TabPage> lstComPage = new List<TabPage>();
|
||
|
|
private int ComCount;
|
||
|
|
/// <summary>
|
||
|
|
/// 定义MelsecPLCUI自定义控件List集合
|
||
|
|
/// </summary>
|
||
|
|
public List<CommunUI> lstUI = new List<CommunUI>();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 配置文件实体类
|
||
|
|
/// </summary>
|
||
|
|
private List<ComConfig> lstComConfig = new List<ComConfig>();
|
||
|
|
/// <summary>
|
||
|
|
/// PLC读取寄存器配置文件
|
||
|
|
/// </summary>
|
||
|
|
private string ConfigPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "Config\\ComConfig.ini");
|
||
|
|
|
||
|
|
|
||
|
|
public FrmCommunication()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
//ConfigPath = path;
|
||
|
|
//ComCount = int.Parse(new IniHelper(ConfigPath).IniReadValue("SystemConfig", "ComCount"));
|
||
|
|
ComCount = 1;
|
||
|
|
ReadPlcConfig(ComCount);
|
||
|
|
LoadUI(ComCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public void ReadPlcConfig(int Count)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
for (int j = 0; j < Count; j++)
|
||
|
|
{
|
||
|
|
ComConfig comConfig = new ComConfig();
|
||
|
|
comConfig.Index = j + 1;
|
||
|
|
comConfig.Auto_Connect = bool.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "Auto_Connect"));
|
||
|
|
comConfig.Connect_Typt = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
|
||
|
|
comConfig.TCP_IP = new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "TCP_IP");
|
||
|
|
comConfig.TCP_Port = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "TCP_Port"));
|
||
|
|
comConfig.HeartBeat = bool.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "HeartBeat"));
|
||
|
|
comConfig.HeartText = new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "HeartText");
|
||
|
|
comConfig.HeartTime = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "HeartTime"));
|
||
|
|
|
||
|
|
comConfig.COM_Port = new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "COM_Port");
|
||
|
|
comConfig.COM_BaudRate = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "COM_BaudRate"));
|
||
|
|
comConfig.COM_Parity = new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "COM_Parity");
|
||
|
|
comConfig.COM_DataBit = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "COM_DataBit"));
|
||
|
|
comConfig.COM_StopBit = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "COM_StopBit"));
|
||
|
|
|
||
|
|
comConfig.Endsymbol = int.Parse(new IniHelper(ConfigPath).IniReadValue(comConfig.Index + "#COMMUNICATION_SETTING", "Endsymbol"));
|
||
|
|
lstComConfig.Add(comConfig);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
MessageBox.Show(ex.Message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 加载MelsecPLCUI窗口
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="Count"></param>
|
||
|
|
public void LoadUI(int Count)
|
||
|
|
{
|
||
|
|
tabCt_ComUI.TabPages.Clear();
|
||
|
|
for (int i = 0; i < Count; i++)
|
||
|
|
{
|
||
|
|
tabCt_ComUI.TabPages.Add(i + 1 + "#通讯模块");
|
||
|
|
Panel panelUI = new Panel();
|
||
|
|
panelUI.Dock = DockStyle.Fill;
|
||
|
|
tabCt_ComUI.TabPages[i].Controls.Add(panelUI);
|
||
|
|
CommunUI plcUI = new CommunUI(lstComConfig[i]);
|
||
|
|
plcUI.Dock = DockStyle.Fill;
|
||
|
|
plcUI.SaveParamsEvent += plcUI_SaveParamsEvent;
|
||
|
|
lstUI.Add(plcUI);
|
||
|
|
panelUI.Controls.Add(plcUI);
|
||
|
|
LoadIni(i);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 循环保存参数
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="comIndex"></param>
|
||
|
|
/// <param name="trgIndex"></param>
|
||
|
|
private void plcUI_SaveParamsEvent(int comIndex, int trgIndex)
|
||
|
|
{
|
||
|
|
if (trgIndex == 0)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < comIndex; i++)
|
||
|
|
{
|
||
|
|
SaveComParam(comIndex);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
///
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="plcIndex"></param> TCP_Typt
|
||
|
|
public void SaveComParam(int comIndex)
|
||
|
|
{
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "Tgr_Count", lstUI[comIndex - 1].ComConfig.Index.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "Auto_Connect", lstUI[comIndex - 1].ComConfig.Auto_Connect.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "Connect_Typt", lstUI[comIndex - 1].ComConfig.Connect_Typt.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "TCP_IP", lstUI[comIndex - 1].ComConfig.TCP_IP.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "TCP_Port", lstUI[comIndex - 1].ComConfig.TCP_Port.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "HeartBeat", lstUI[comIndex - 1].ComConfig.HeartBeat.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "HeartText", lstUI[comIndex - 1].ComConfig.HeartText.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "HeartTime", lstUI[comIndex - 1].ComConfig.HeartTime.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "COM_Port", lstUI[comIndex - 1].ComConfig.COM_Port.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "COM_StopBit", lstUI[comIndex - 1].ComConfig.COM_StopBit.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "COM_BaudRate", lstUI[comIndex - 1].ComConfig.COM_BaudRate.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "COM_Parity", lstUI[comIndex - 1].ComConfig.COM_Parity.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "COM_DataBit", lstUI[comIndex - 1].ComConfig.COM_DataBit.ToString());
|
||
|
|
new IniHelper(ConfigPath).IniWriteValue(comIndex + "#COMMUNICATION_SETTING", "Endsymbol", lstUI[comIndex - 1].ComConfig.Endsymbol.ToString());
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Form1_Load(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
private void LoadIni(int comIndex)
|
||
|
|
{
|
||
|
|
|
||
|
|
lstUI[comIndex].txtHeartData.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "HeartText");
|
||
|
|
lstUI[comIndex].txtIntervalTime.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "HeartTime");
|
||
|
|
lstUI[comIndex].pgeAuto_CheckBox.Checked = ((new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "HeartBeat") == "True") ? true : false);
|
||
|
|
lstUI[comIndex].cmbEndSymbol.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Endsymbol"));
|
||
|
|
if (new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Auto_Connect") == "True")
|
||
|
|
{
|
||
|
|
lstUI[comIndex].Auto_CheckBox.Checked = true;
|
||
|
|
//串口
|
||
|
|
if (Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt")) == 0)
|
||
|
|
{
|
||
|
|
lstUI[comIndex].Server_GroupBox.Visible = false;
|
||
|
|
lstUI[comIndex].info_GroupBox.Dock = DockStyle.Fill;
|
||
|
|
lstUI[comIndex].tcpType_Box.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
lstUI[comIndex].comPort_Box.Items.Clear();
|
||
|
|
lstUI[comIndex].comPort_Box.Items.Add(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_Port"));
|
||
|
|
lstUI[comIndex].comPort_Box.Text = lstUI[comIndex].comPort_Box.Items[0].ToString();
|
||
|
|
lstUI[comIndex].baudRate_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_BaudRate");
|
||
|
|
lstUI[comIndex].parity_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_Parity");
|
||
|
|
lstUI[comIndex].dataBits_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_DataBit");
|
||
|
|
lstUI[comIndex].stopBits_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_StopBit");
|
||
|
|
lstUI[comIndex].COMOpen();
|
||
|
|
}
|
||
|
|
//服务端
|
||
|
|
else if (Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt")) == 1)
|
||
|
|
{
|
||
|
|
lstUI[comIndex].tcpType_Box.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
lstUI[comIndex].tbIP.Items.Clear();
|
||
|
|
lstUI[comIndex].tbIP.Items.Add(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_IP"));
|
||
|
|
lstUI[comIndex].tbIP.Text = lstUI[comIndex].tbIP.Items[0].ToString();
|
||
|
|
lstUI[comIndex].tbPort.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_Port");
|
||
|
|
//lstUI[comIndex].TCPOpen();
|
||
|
|
}
|
||
|
|
//客户端
|
||
|
|
else if (Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt")) == 2)
|
||
|
|
{
|
||
|
|
lstUI[comIndex].Server_GroupBox.Visible = false;
|
||
|
|
lstUI[comIndex].info_GroupBox.Dock = DockStyle.Fill;
|
||
|
|
lstUI[comIndex].tcpType_Box.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
lstUI[comIndex].tbIP.Items.Clear();
|
||
|
|
lstUI[comIndex].tbIP.Items.Add(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_IP"));
|
||
|
|
lstUI[comIndex].tbIP.Text = lstUI[comIndex].tbIP.Items[0].ToString();
|
||
|
|
lstUI[comIndex].tbPort.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_Port");
|
||
|
|
//lstUI[comIndex].TCPOpen();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
//串口
|
||
|
|
lstUI[comIndex].Server_GroupBox.Visible = false;
|
||
|
|
lstUI[comIndex].info_GroupBox.Dock = DockStyle.Fill;
|
||
|
|
lstUI[comIndex].tcpType_Box.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
lstUI[comIndex].comPort_Box.Items.Clear();
|
||
|
|
lstUI[comIndex].comPort_Box.Items.Add(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_Port"));
|
||
|
|
lstUI[comIndex].comPort_Box.Text = lstUI[comIndex].comPort_Box.Items[0].ToString();
|
||
|
|
lstUI[comIndex].baudRate_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_BaudRate");
|
||
|
|
lstUI[comIndex].parity_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_Parity");
|
||
|
|
lstUI[comIndex].dataBits_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_DataBit");
|
||
|
|
lstUI[comIndex].stopBits_Box.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "COM_StopBit");
|
||
|
|
lstUI[comIndex].COMOpen();
|
||
|
|
|
||
|
|
//客户端
|
||
|
|
lstUI[comIndex].Server_GroupBox.Visible = false;
|
||
|
|
lstUI[comIndex].info_GroupBox.Dock = DockStyle.Fill;
|
||
|
|
lstUI[comIndex].tcpType_Box.SelectedIndex = Convert.ToInt32(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "Connect_Typt"));
|
||
|
|
lstUI[comIndex].tbIP.Items.Clear();
|
||
|
|
lstUI[comIndex].tbIP.Items.Add(new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_IP"));
|
||
|
|
lstUI[comIndex].tbIP.Text = lstUI[comIndex].tbIP.Items[0].ToString();
|
||
|
|
lstUI[comIndex].tbPort.Text = new IniHelper(ConfigPath).IniReadValue((comIndex + 1) + "#COMMUNICATION_SETTING", "TCP_Port");
|
||
|
|
//lstUI[comIndex].TCPOpen();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
lstUI[comIndex].Auto_CheckBox.Checked = false;
|
||
|
|
lstUI[comIndex].comSerial.LoadComSerial(lstUI[comIndex].comPort_Box);
|
||
|
|
lstUI[comIndex].LocalIP(lstUI[comIndex].tbIP);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
private void FrmCommunication_FormClosing(object sender, FormClosingEventArgs e)
|
||
|
|
{
|
||
|
|
this.Visible = false;
|
||
|
|
e.Cancel = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|