MainWindow.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using UAS_PLCDataReader.PublicMethod;
  6. using HslCommunication.Profinet;
  7. using HslCommunication.Profinet.Melsec;
  8. using HslCommunication;
  9. using System.Text;
  10. namespace UAS_PLCDataReader
  11. {
  12. public partial class MainWindow : Form
  13. {
  14. ModeBusTCPServer md = new ModeBusTCPServer();
  15. public MainWindow()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. CheckForIllegalCrossThreadCalls = false;
  22. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  23. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  24. {
  25. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  26. if(IpEntry.AddressList[i].ToString()=="192.168.127.20")
  27. IP.Text = IpEntry.AddressList[i].ToString();
  28. }
  29. Encoding1.Text = Encoding1.Items[0].ToString();
  30. }
  31. private void OpenServer_Click(object sender, EventArgs e)
  32. {
  33. md.IP = IP.Text;
  34. md.Port = Port.Text;
  35. md.Open();
  36. if (md.IsOpen)
  37. {
  38. OpenServer.Enabled = false;
  39. Port.Enabled = false;
  40. }
  41. }
  42. private void CloseServer_Click(object sender, EventArgs e)
  43. {
  44. md.Close();
  45. if (!md.IsOpen)
  46. {
  47. OpenServer.Enabled = true;
  48. Port.Enabled = true;
  49. }
  50. }
  51. private void SEND_Click(object sender, EventArgs e)
  52. {
  53. //Console.WriteLine(BaseUtil.ASCIIToString("3A30313033343032314330303030303231374130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303430DA"));
  54. MelsecMcNet melsec_net = new MelsecMcNet("192.168.1.120", 8880);
  55. OperateResult<byte[]> read = melsec_net.Read("D200", 5);
  56. if (read.IsSuccess)
  57. {
  58. Console.WriteLine(Encoding.ASCII.GetString(read.Content));
  59. }
  60. else
  61. {
  62. //失败读取
  63. MessageBox.Show(read.ToMessageShowString());
  64. }
  65. }
  66. public static string HexStringToASCII(string hexstring)
  67. {
  68. byte[] bt = HexStringToBinary(hexstring);
  69. string lin = "";
  70. for (int i = 0; i < bt.Length; i++)
  71. {
  72. lin = lin + bt[i] + " ";
  73. }
  74. string[] ss = lin.Trim().Split(new char[] { ' ' });
  75. char[] c = new char[ss.Length];
  76. int a;
  77. for (int i = 0; i < c.Length; i++)
  78. {
  79. a = Convert.ToInt32(ss[i]);
  80. c[i] = Convert.ToChar(a);
  81. }
  82. string b = new string(c);
  83. return b;
  84. }
  85. public static byte[] HexStringToBinary(string hexstring)
  86. {
  87. string[] tmpary = hexstring.Trim().Split(' ');
  88. byte[] buff = new byte[tmpary.Length];
  89. for (int i = 0; i < buff.Length; i++)
  90. {
  91. buff[i] = Convert.ToByte(tmpary[i], 16);
  92. }
  93. return buff;
  94. }
  95. }
  96. }