123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- 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 HRT_Measure
- {
- public partial class FrmCommunication : Form
- {
- public FrmCommunication()
- {
- InitializeComponent();
- }
- private void FrmCommunication_Load(object sender, EventArgs e)
- {
- try
- {
- foreach (var item in ProgramHelp.Instance.dictCommPLC)
- {
- cobPLC.Items.Add(item.Key);
- }
- if(cobPLC.Items.Count > 0) cobPLC.SelectedIndex = 0;
- rabPLCDataType.SelectedIndex = 0;
-
- foreach (var item in ProgramHelp.Instance.dictClient)
- {
- cobTcpClient.Items.Add(item.Key);
- }
- if(cobTcpClient.Items.Count > 0) cobTcpClient.SelectedIndex = 0;
- foreach (var item in ProgramHelp.Instance.dictServer)
- {
- cobTcpServer.Items.Add(item.Key);
- }
- if (cobTcpServer.Items.Count > 0) cobTcpServer.SelectedIndex = 0;
- timer1.Enabled = true;
- }
- catch (Exception)
- {
- }
- }
- private void CobPLC_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- }
- private void BtnPLCConnect_Click(object sender, EventArgs e)
- {
- ProgramHelp.Instance.dictCommPLC[cobPLC.Text].Connect();
- }
-
- private void Timer1_Tick(object sender, EventArgs e)
- {
- try
- {
- if(ProgramHelp.Instance.dictCommPLC.Count > 0)
- {
- btnPLCConnect.FillColor = ProgramHelp.Instance.dictCommPLC[cobPLC.Text].isPLCConnected ? Color.Green : Color.Gray;
- }
- if(ProgramHelp.Instance.dictClient.Count > 0)
- {
- btnClientConnect.FillColor = ProgramHelp.Instance.dictClient[cobTcpClient.Text].isClientConnected ? Color.Green : Color.Gray;
- txtClientRecv.Text = ProgramHelp.Instance.dictClient[cobTcpClient.Text].strClientRecv;
- }
- if(ProgramHelp.Instance.dictServer.Count > 0)
- {
- btnTcpServerStart.FillColor = ProgramHelp.Instance.dictServer[cobTcpServer.Text].isServerConnected ? Color.Green : Color.Gray;
- txtTCPServerRecv.Text = ProgramHelp.Instance.dictServer[cobTcpServer.Text].strRecv;
- }
- }
- catch (Exception)
- {
- }
- }
- #region PLC
- private void BtnPLCRead_Click(object sender, EventArgs e)
- {
- if(txtPLCAdd.Text.Trim() == "")
- {
- MessageBox.Show("请写入地址!");
- }
- switch (rabPLCDataType.SelectedIndex)
- {
- case 0:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadInt(txtPLCAdd.Text, out int idata, out string strContent))
- {
- txtPLCReturn.Text = $"读取到{idata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strContent}";
- }
- break;
- case 1:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadFloat(txtPLCAdd.Text, out float fdata, out string strfContent))
- {
- txtPLCReturn.Text = $"读取到{fdata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strfContent}";
- }
- break;
- case 2:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadDouble(txtPLCAdd.Text, out double ddata, out string strdContent))
- {
- txtPLCReturn.Text = $"读取到{ddata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strdContent}";
- }
- break;
- case 3:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadShort(txtPLCAdd.Text, out short sdata, out string strsContent))
- {
- txtPLCReturn.Text = $"读取到{sdata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strsContent}";
- }
- break;
- case 4:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadCoin(txtPLCAdd.Text, out bool bdata, out string strbContent))
- {
- txtPLCReturn.Text = $"读取到{bdata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strbContent}";
- }
- break;
- case 5:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadUshort(txtPLCAdd.Text, out ushort ushortdata, out string strushortContent))
- {
- txtPLCReturn.Text = $"读取到{ushortdata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strushortContent}";
- }
- break;
- case 6:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadString(txtPLCAdd.Text, out string strdata, out string strdContent1))
- {
- txtPLCReturn.Text = $"读取到{strdata}";
- }
- else
- {
- txtPLCReturn.Text = $"读取失败:{strdContent1}";
- }
- break;
- }
- }
- private void BtnPLCWrite_Click(object sender, EventArgs e)
- {
- if (txtPLCAdd.Text.Trim() == "")
- {
- MessageBox.Show("请写入地址!");
- }
- if(txtPLCValue.Text.Trim() == "")
- {
- MessageBox.Show("请写入要写入的值!");
- }
- switch (rabPLCDataType.SelectedIndex)
- {
- case 0:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteInt(txtPLCAdd.Text, int.Parse(txtPLCValue.Text), out string striContent))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{striContent}";
- }
- break;
- case 1:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteFloat(txtPLCAdd.Text, float.Parse(txtPLCValue.Text), out string strfContent))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{strfContent}";
- }
- break;
- case 2:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteDouble(txtPLCAdd.Text, double.Parse(txtPLCValue.Text), out string strdContent))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{strdContent}";
- }
- break;
- case 3:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteShort(txtPLCAdd.Text, short.Parse(txtPLCValue.Text), out string striContent1))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{striContent1}";
- }
- break;
- case 4:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteCoin(txtPLCAdd.Text, Convert.ToBoolean(txtPLCValue.Text), out string striContent2))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{striContent2}";
- }
- break;
- case 5:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteUShort(txtPLCAdd.Text, Convert.ToUInt16(txtPLCValue.Text), out string striContent12))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{striContent12}";
- }
- break;
- case 6:
- if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteString(txtPLCAdd.Text, txtPLCValue.Text, out string strdContent1))
- {
- txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
- }
- else
- {
- txtPLCReturn.Text = $"写入失败:{strdContent1}";
- }
- break;
- }
- }
- #endregion
- #region Client
- private void BtnClientConnect_Click(object sender, EventArgs e)
- {
- ProgramHelp.Instance.dictClient[cobTcpClient.Text].TcpClientStart();
- }
- private void BtnClientSend_Click(object sender, EventArgs e)
- {
- ProgramHelp.Instance.dictClient[cobTcpClient.Text].TcpClientSend(txtClientSend.Text.Trim());
- }
- #endregion
- #region Server
- private void BtnTcpServerStart_Click(object sender, EventArgs e)
- {
- ProgramHelp.Instance.dictServer[cobTcpServer.Text].server.Start();
- }
- private void BtnTCPServerSend_Click(object sender, EventArgs e)
- {
- ProgramHelp.Instance.dictServer[cobTcpServer.Text].Send(txtTCPServerSend.Text.Trim());
- }
- #endregion
- }
- }
|