using System; using System.Text; using System.Windows.Forms; using Curtain.Framework.Utils; using HslCommunication; using HslCommunication.Profinet.Melsec; namespace PCLCommunication { public partial class FrmTest : Form { public FrmTest() { InitializeComponent(); } private void btnRead_Click(object sender, EventArgs e) { MelsecA1EAscii m = null; try { this.txtRText.Text = null; m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); string d = this.txtRAdress.Text.Trim(); byte[] b501 = m.GetByteData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), Convert.ToInt32(this.txtRLenght.Text.Trim())); if (b501 == null || b501.Length == 0) { return; } this.txtRText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + Encoding.ASCII.GetString(b501); } catch (Exception ex) { this.txtRText.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("读取异常"); } finally { m?.Dispose(); } } private void btnConn_Click(object sender, EventArgs e) { MelsecA1EAscii m = null; try { m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); m.ConnectSocket(); } catch (Exception ex) { string j = JsonUtil.FromObject(m); LogOut.Error("Text", ex, j); MessageBox.Show("连接异常"); } finally { m?.Dispose(); } } private void btnWrite_Click(object sender, EventArgs e) { MelsecA1EAscii m = null; try { this.txtWText.Text = null; m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); string d = this.txtWAdress.Text.Trim(); byte[] b521 = Encoding.ASCII.GetBytes(this.txtWLenght.Text.Trim()); bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521); this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败"); } catch (Exception ex) { this.txtWText.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("写入异常"); } finally { m?.Dispose(); } } private void button3_Click(object sender, EventArgs e) { MelsecA1EAsciiSocket m = null; try { this.txtWText.Text = null; m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); string d = this.txtWAdress.Text.Trim(); byte[] b521 = Encoding.ASCII.GetBytes(this.txtWLenght.Text.Trim()); bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521); this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败"); } catch (Exception ex) { this.txtWText.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("写入异常"); } finally { m?.Dispose(); } //MelsecA1ENet m = null; //try //{ // this.txtWText.Text = null; // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); // string d = this.txtWAdress.Text.Trim(); // OperateResult or = m.Write(d, this.txtWLenght.Text.Trim()); // //if (or is OperateResult) // //{ // // OperateResult orb = or as OperateResult; // // string rr = Encoding.ASCII.GetString(orb.Content); // //} // this.txtWText.Text = (or.IsSuccess ? "写入成功" : "写入失败") + "\r\n" // + JsonUtil.FromObject(or); //} //catch (Exception ex) //{ // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive; // LogOut.Error("Text", ex); // MessageBox.Show("写入异常"); //} //finally //{ // m?.ConnectClose(); // //m?.Dispose(); //} } private void button1_Click(object sender, EventArgs e) { MelsecA1EAscii m = null; try { this.txtResult.Text = null; m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); m.Do(this.txtCommed.Text.Trim()); this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n"; } catch (Exception ex) { this.txtResult.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("执行异常"); } finally { m?.Dispose(); } } private void button2_Click(object sender, EventArgs e) { MelsecA1EAsciiSocket m = null; try { this.txtResult.Text = null; m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); byte[] _PLCCommand = new byte[12]; _PLCCommand[0] = 0x00; // 副标题 _PLCCommand[1] = 0xFF; // PLC号 _PLCCommand[2] = 0x0A; // CPU监视定时器(L)这里设置为0x00,0x0A,等待CPU返回的时间为10*250ms=2.5秒 _PLCCommand[3] = 0x00; // CPU监视定时器(H) _PLCCommand[4] = 0x64; // 起始软元件(开始读取的地址) _PLCCommand[5] = 0x00; _PLCCommand[6] = 0x00; _PLCCommand[7] = 0x00; _PLCCommand[8] = 0x20; // 软元件代码(L) _PLCCommand[9] = 0x44; // 软元件代码(H) _PLCCommand[10] = 0x08; // 软元件点数 _PLCCommand[11] = 0x00; string ss = Encoding.ASCII.GetString(_PLCCommand); //m.Do(this.txtCommed.Text.Trim()); m.Do(ss); this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n"; } catch (Exception ex) { this.txtResult.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("执行异常"); } finally { m?.Dispose(); } } private void button4_Click(object sender, EventArgs e) { MelsecA1EAsciiSocket m = null; try { this.txtRText.Text = null; m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); string d = this.txtRAdress.Text.Trim(); byte[] b501 = m.GetByteData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), Convert.ToInt32(this.txtRLenght.Text.Trim())); if (b501 == null || b501.Length == 0) { return; } this.txtRText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + Encoding.ASCII.GetString(b501); } catch (Exception ex) { this.txtRText.Text = m?.Command + "\r\n" + m?.Receive; LogOut.Error("Text", ex); MessageBox.Show("读取异常"); } finally { m?.Dispose(); } //MelsecA1ENet m = null; //try //{ // this.txtRText.Text = null; // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim())); // string d = this.txtRAdress.Text.Trim(); // OperateResult or = m.ReadString(d, ushort.Parse( this.txtRLenght.Text.Trim())); // this.txtRText.Text = or.Content + "\r\n" + (or.IsSuccess ? "读取成功" : "读取失败") + "\r\n" // + JsonUtil.FromObject( or); //} //catch (Exception ex) //{ // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive; // LogOut.Error("Text", ex); // MessageBox.Show("读取异常"); //} //finally //{ // m?.ConnectClose(); // //m?.Dispose(); //} } } }