MainWindow.cs 2.8 KB

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