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 } }