FrmTest.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 
  2. using System;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Curtain.Framework.Utils;
  6. using HslCommunication;
  7. using HslCommunication.Profinet.Melsec;
  8. namespace PCLCommunication
  9. {
  10. public partial class FrmTest : Form
  11. {
  12. public FrmTest()
  13. {
  14. InitializeComponent();
  15. }
  16. private void btnRead_Click(object sender, EventArgs e)
  17. {
  18. MelsecA1EAscii m = null;
  19. try
  20. {
  21. this.txtRText.Text = null;
  22. m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  23. string d = this.txtRAdress.Text.Trim();
  24. byte[] b501 = m.GetByteData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), Convert.ToInt32(this.txtRLenght.Text.Trim()));
  25. if (b501 == null || b501.Length == 0)
  26. {
  27. return;
  28. }
  29. this.txtRText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + Encoding.ASCII.GetString(b501);
  30. }
  31. catch (Exception ex)
  32. {
  33. this.txtRText.Text = m?.Command + "\r\n" + m?.Receive;
  34. LogOut.Error("Text", ex);
  35. MessageBox.Show("读取异常");
  36. }
  37. finally
  38. {
  39. m?.Dispose();
  40. }
  41. }
  42. private void btnConn_Click(object sender, EventArgs e)
  43. {
  44. MelsecA1EAscii m = null;
  45. try
  46. {
  47. m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  48. m.ConnectSocket();
  49. }
  50. catch (Exception ex)
  51. {
  52. string j = JsonUtil.FromObject(m);
  53. LogOut.Error("Text", ex, j);
  54. MessageBox.Show("连接异常");
  55. }
  56. finally
  57. {
  58. m?.Dispose();
  59. }
  60. }
  61. private void btnWrite_Click(object sender, EventArgs e)
  62. {
  63. MelsecA1EAscii m = null;
  64. try
  65. {
  66. this.txtWText.Text = null;
  67. m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  68. string d = this.txtWAdress.Text.Trim();
  69. byte[] b521 = Encoding.ASCII.GetBytes(this.txtWLenght.Text.Trim());
  70. bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521);
  71. this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败");
  72. }
  73. catch (Exception ex)
  74. {
  75. this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  76. LogOut.Error("Text", ex);
  77. MessageBox.Show("写入异常");
  78. }
  79. finally
  80. {
  81. m?.Dispose();
  82. }
  83. }
  84. private void button3_Click(object sender, EventArgs e)
  85. {
  86. MelsecA1EAsciiSocket m = null;
  87. try
  88. {
  89. this.txtWText.Text = null;
  90. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  91. string d = this.txtWAdress.Text.Trim();
  92. byte[] b521 = Encoding.ASCII.GetBytes(this.txtWLenght.Text.Trim());
  93. bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521);
  94. this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败");
  95. }
  96. catch (Exception ex)
  97. {
  98. this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  99. LogOut.Error("Text", ex);
  100. MessageBox.Show("写入异常");
  101. }
  102. finally
  103. {
  104. m?.Dispose();
  105. }
  106. //MelsecA1ENet m = null;
  107. //try
  108. //{
  109. // this.txtWText.Text = null;
  110. // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  111. // string d = this.txtWAdress.Text.Trim();
  112. // OperateResult or = m.Write(d, this.txtWLenght.Text.Trim());
  113. // //if (or is OperateResult<byte[]>)
  114. // //{
  115. // // OperateResult<byte[]> orb = or as OperateResult<byte[]>;
  116. // // string rr = Encoding.ASCII.GetString(orb.Content);
  117. // //}
  118. // this.txtWText.Text = (or.IsSuccess ? "写入成功" : "写入失败") + "\r\n"
  119. // + JsonUtil.FromObject(or);
  120. //}
  121. //catch (Exception ex)
  122. //{
  123. // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  124. // LogOut.Error("Text", ex);
  125. // MessageBox.Show("写入异常");
  126. //}
  127. //finally
  128. //{
  129. // m?.ConnectClose();
  130. // //m?.Dispose();
  131. //}
  132. }
  133. private void button1_Click(object sender, EventArgs e)
  134. {
  135. MelsecA1EAscii m = null;
  136. try
  137. {
  138. this.txtResult.Text = null;
  139. m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  140. m.Do(this.txtCommed.Text.Trim());
  141. this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n";
  142. }
  143. catch (Exception ex)
  144. {
  145. this.txtResult.Text = m?.Command + "\r\n" + m?.Receive;
  146. LogOut.Error("Text", ex);
  147. MessageBox.Show("执行异常");
  148. }
  149. finally
  150. {
  151. m?.Dispose();
  152. }
  153. }
  154. private void button2_Click(object sender, EventArgs e)
  155. {
  156. MelsecA1EAsciiSocket m = null;
  157. try
  158. {
  159. this.txtResult.Text = null;
  160. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  161. byte[] _PLCCommand = new byte[12];
  162. _PLCCommand[0] = 0x00; // 副标题
  163. _PLCCommand[1] = 0xFF; // PLC号
  164. _PLCCommand[2] = 0x0A; // CPU监视定时器(L)这里设置为0x00,0x0A,等待CPU返回的时间为10*250ms=2.5秒
  165. _PLCCommand[3] = 0x00; // CPU监视定时器(H)
  166. _PLCCommand[4] = 0x64; // 起始软元件(开始读取的地址)
  167. _PLCCommand[5] = 0x00;
  168. _PLCCommand[6] = 0x00;
  169. _PLCCommand[7] = 0x00;
  170. _PLCCommand[8] = 0x20; // 软元件代码(L)
  171. _PLCCommand[9] = 0x44; // 软元件代码(H)
  172. _PLCCommand[10] = 0x08; // 软元件点数
  173. _PLCCommand[11] = 0x00;
  174. string ss = Encoding.ASCII.GetString(_PLCCommand);
  175. //m.Do(this.txtCommed.Text.Trim());
  176. m.Do(ss);
  177. this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n";
  178. }
  179. catch (Exception ex)
  180. {
  181. this.txtResult.Text = m?.Command + "\r\n" + m?.Receive;
  182. LogOut.Error("Text", ex);
  183. MessageBox.Show("执行异常");
  184. }
  185. finally
  186. {
  187. m?.Dispose();
  188. }
  189. }
  190. private void button4_Click(object sender, EventArgs e)
  191. {
  192. MelsecA1EAsciiSocket m = null;
  193. try
  194. {
  195. this.txtRText.Text = null;
  196. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  197. string d = this.txtRAdress.Text.Trim();
  198. byte[] b501 = m.GetByteData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), Convert.ToInt32(this.txtRLenght.Text.Trim()));
  199. if (b501 == null || b501.Length == 0)
  200. {
  201. return;
  202. }
  203. this.txtRText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + Encoding.ASCII.GetString(b501);
  204. }
  205. catch (Exception ex)
  206. {
  207. this.txtRText.Text = m?.Command + "\r\n" + m?.Receive;
  208. LogOut.Error("Text", ex);
  209. MessageBox.Show("读取异常");
  210. }
  211. finally
  212. {
  213. m?.Dispose();
  214. }
  215. //MelsecA1ENet m = null;
  216. //try
  217. //{
  218. // this.txtRText.Text = null;
  219. // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  220. // string d = this.txtRAdress.Text.Trim();
  221. // OperateResult<string> or = m.ReadString(d, ushort.Parse( this.txtRLenght.Text.Trim()));
  222. // this.txtRText.Text = or.Content + "\r\n" + (or.IsSuccess ? "读取成功" : "读取失败") + "\r\n"
  223. // + JsonUtil.FromObject( or);
  224. //}
  225. //catch (Exception ex)
  226. //{
  227. // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  228. // LogOut.Error("Text", ex);
  229. // MessageBox.Show("读取异常");
  230. //}
  231. //finally
  232. //{
  233. // m?.ConnectClose();
  234. // //m?.Dispose();
  235. //}
  236. }
  237. }
  238. }