FrmTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. //13.ToString("X4")
  71. bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521);
  72. this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败");
  73. }
  74. catch (Exception ex)
  75. {
  76. this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  77. LogOut.Error("Text", ex);
  78. MessageBox.Show("写入异常");
  79. }
  80. finally
  81. {
  82. m?.Dispose();
  83. }
  84. }
  85. private void button3_Click(object sender, EventArgs e)
  86. {
  87. MelsecA1EAsciiSocket m = null;
  88. try
  89. {
  90. this.txtWText.Text = null;
  91. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  92. string d = this.txtWAdress.Text.Trim();
  93. byte[] b521 = Encoding.ASCII.GetBytes(this.txtWLenght.Text.Trim());
  94. bool r = m.SetData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), b521);
  95. this.txtWText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + (r ? "写入成功" : "写入失败");
  96. }
  97. catch (Exception ex)
  98. {
  99. this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  100. LogOut.Error("Text", ex);
  101. MessageBox.Show("写入异常");
  102. }
  103. finally
  104. {
  105. m?.Dispose();
  106. }
  107. //MelsecA1ENet m = null;
  108. //try
  109. //{
  110. // this.txtWText.Text = null;
  111. // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  112. // string d = this.txtWAdress.Text.Trim();
  113. // OperateResult or = m.Write(d, this.txtWLenght.Text.Trim());
  114. // //if (or is OperateResult<byte[]>)
  115. // //{
  116. // // OperateResult<byte[]> orb = or as OperateResult<byte[]>;
  117. // // string rr = Encoding.ASCII.GetString(orb.Content);
  118. // //}
  119. // this.txtWText.Text = (or.IsSuccess ? "写入成功" : "写入失败") + "\r\n"
  120. // + JsonUtil.FromObject(or);
  121. //}
  122. //catch (Exception ex)
  123. //{
  124. // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  125. // LogOut.Error("Text", ex);
  126. // MessageBox.Show("写入异常");
  127. //}
  128. //finally
  129. //{
  130. // m?.ConnectClose();
  131. // //m?.Dispose();
  132. //}
  133. }
  134. private void button1_Click(object sender, EventArgs e)
  135. {
  136. MelsecA1EAscii m = null;
  137. try
  138. {
  139. this.txtResult.Text = null;
  140. m = new MelsecA1EAscii(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  141. m.Do(this.txtCommed.Text.Trim());
  142. this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n";
  143. }
  144. catch (Exception ex)
  145. {
  146. this.txtResult.Text = m?.Command + "\r\n" + m?.Receive;
  147. LogOut.Error("Text", ex);
  148. MessageBox.Show("执行异常");
  149. }
  150. finally
  151. {
  152. m?.Dispose();
  153. }
  154. }
  155. private void button2_Click(object sender, EventArgs e)
  156. {
  157. MelsecA1EAsciiSocket m = null;
  158. try
  159. {
  160. this.txtResult.Text = null;
  161. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  162. byte[] _PLCCommand = new byte[12];
  163. _PLCCommand[0] = 0x00; // 副标题
  164. _PLCCommand[1] = 0xFF; // PLC号
  165. _PLCCommand[2] = 0x0A; // CPU监视定时器(L)这里设置为0x00,0x0A,等待CPU返回的时间为10*250ms=2.5秒
  166. _PLCCommand[3] = 0x00; // CPU监视定时器(H)
  167. _PLCCommand[4] = 0x64; // 起始软元件(开始读取的地址)
  168. _PLCCommand[5] = 0x00;
  169. _PLCCommand[6] = 0x00;
  170. _PLCCommand[7] = 0x00;
  171. _PLCCommand[8] = 0x20; // 软元件代码(L)
  172. _PLCCommand[9] = 0x44; // 软元件代码(H)
  173. _PLCCommand[10] = 0x08; // 软元件点数
  174. _PLCCommand[11] = 0x00;
  175. string ss = Encoding.ASCII.GetString(_PLCCommand);
  176. //m.Do(this.txtCommed.Text.Trim());
  177. m.Do(ss);
  178. this.txtResult.Text = m.Command + "\r\n" + m.Receive + "\r\n";
  179. }
  180. catch (Exception ex)
  181. {
  182. this.txtResult.Text = m?.Command + "\r\n" + m?.Receive;
  183. LogOut.Error("Text", ex);
  184. MessageBox.Show("执行异常");
  185. }
  186. finally
  187. {
  188. m?.Dispose();
  189. }
  190. }
  191. private void button4_Click(object sender, EventArgs e)
  192. {
  193. MelsecA1EAsciiSocket m = null;
  194. try
  195. {
  196. this.txtRText.Text = null;
  197. m = new MelsecA1EAsciiSocket(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  198. string d = this.txtRAdress.Text.Trim();
  199. string b501 = m.GetHexData(d[0].ToString(), Convert.ToInt32(d.Substring(1)), Convert.ToInt32(this.txtRLenght.Text.Trim()));
  200. if (b501 == null || b501.Length == 0)
  201. {
  202. return;
  203. }
  204. this.txtRText.Text = m.Command + "\r\n" + m.Receive + "\r\n" + b501;
  205. }
  206. catch (Exception ex)
  207. {
  208. this.txtRText.Text = m?.Command + "\r\n" + m?.Receive;
  209. LogOut.Error("Text", ex);
  210. MessageBox.Show("读取异常");
  211. }
  212. finally
  213. {
  214. m?.Dispose();
  215. }
  216. //MelsecA1ENet m = null;
  217. //try
  218. //{
  219. // this.txtRText.Text = null;
  220. // m = new MelsecA1ENet(this.txtIP.Text.Trim(), Convert.ToInt32(this.txtPort.Text.Trim()));
  221. // string d = this.txtRAdress.Text.Trim();
  222. // OperateResult<string> or = m.ReadString(d, ushort.Parse( this.txtRLenght.Text.Trim()));
  223. // this.txtRText.Text = or.Content + "\r\n" + (or.IsSuccess ? "读取成功" : "读取失败") + "\r\n"
  224. // + JsonUtil.FromObject( or);
  225. //}
  226. //catch (Exception ex)
  227. //{
  228. // //this.txtWText.Text = m?.Command + "\r\n" + m?.Receive;
  229. // LogOut.Error("Text", ex);
  230. // MessageBox.Show("读取异常");
  231. //}
  232. //finally
  233. //{
  234. // m?.ConnectClose();
  235. // //m?.Dispose();
  236. //}
  237. }
  238. }
  239. }