| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
-
- 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());
- //13.ToString("X4")
- 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<byte[]>)
- // //{
- // // OperateResult<byte[]> orb = or as OperateResult<byte[]>;
- // // 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();
- string b501 = m.GetHexData(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" + 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<string> 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();
- //}
- }
- }
- }
|