Form1.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace FileAnalysis
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. TCPClient client;
  17. private const int bufferSize = 8000;
  18. private void Analysis_Click(object sender, EventArgs e)
  19. {
  20. //client.Send("A110S001H01B1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
  21. //client.Send("41 31 31 30 53 30 30 31 48 30 31 42 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 AF E4");
  22. //string sss= BinTohex("A110S001H10R1011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
  23. string sss = BinTohex("B008S001GM");
  24. client.Send(sss);
  25. }
  26. private string GETMD5(string password)
  27. {
  28. //初始化MD5对象
  29. MD5 md5 = MD5.Create();
  30. //将源字符串转化为byte数组
  31. Byte[] soucebyte = Encoding.Default.GetBytes(password);
  32. //soucebyte转化为mf5的byte数组
  33. Byte[] md5bytes = md5.ComputeHash(soucebyte);
  34. //将md5的byte数组再转化为MD5数组
  35. StringBuilder sb = new StringBuilder();
  36. foreach (Byte b in md5bytes)
  37. {
  38. //x表示16进制,2表示2位
  39. sb.Append(b.ToString("x2"));
  40. }
  41. return sb.ToString();
  42. }
  43. public static ushort CalculateCRC(byte[] data)
  44. {
  45. ushort crc = 0xFFFF;
  46. foreach (byte b in data)
  47. {
  48. crc ^= b;
  49. for (int i = 0; i < 8; i++)
  50. {
  51. if ((crc & 0x0001) != 0)
  52. {
  53. crc >>= 1;
  54. crc ^= 0xA001;
  55. }
  56. else
  57. {
  58. crc >>= 1;
  59. }
  60. }
  61. }
  62. // Swap bytes to get the correct Modbus CRC16 format
  63. return (ushort)((crc << 8) | (crc >> 8));
  64. }
  65. public static string BinTohex(string mBin)
  66. {
  67. char[] values = mBin.ToCharArray();
  68. int tempI;
  69. StringBuilder WRSCodeSB = new StringBuilder();
  70. foreach (char value in values)
  71. {
  72. tempI = Convert.ToInt32(value);
  73. WRSCodeSB.Append(Convert.ToString(tempI, 16).PadLeft(2, '0'));
  74. }
  75. return WRSCodeSB.ToString();
  76. }
  77. public static int[] GetDecimalData(string HexStr, int[] DataSize, int[] NotShow)
  78. {
  79. List<int> ReturnData = new List<int>();
  80. try
  81. {
  82. //去除前面的指令字符和数据长度字符
  83. HexStr = HexStr.Replace(":", "");
  84. if (HexStr != "")
  85. {
  86. int ValueDataSize = Convert.ToInt32("0x" + (HexStr.Substring(4, 2)), 16) * 2;
  87. string RealData = HexStr.Substring(6);
  88. RealData = RealData.Substring(0, ValueDataSize);
  89. int SubIndex = 0;
  90. for (int i = 0; i < DataSize.Length; i = i + 1)
  91. {
  92. if (SubIndex + DataSize[i] < RealData.Length)
  93. {
  94. bool notshow = false;
  95. for (int j = 0; j < NotShow.Length; j++)
  96. {
  97. if (i == NotShow[j])
  98. notshow = true;
  99. }
  100. if (notshow)
  101. {
  102. SubIndex += DataSize[i];
  103. continue;
  104. }
  105. if (DataSize[i] == 8)
  106. {
  107. Console.WriteLine("0x" + (RealData.Substring(SubIndex, DataSize[i]).Substring(4, 4)) + (RealData.Substring(SubIndex, DataSize[i]).Substring(0, 4)));
  108. ReturnData.Add(Convert.ToInt32("0x" + (RealData.Substring(SubIndex, DataSize[i]).Substring(4, 4)) + (RealData.Substring(SubIndex, DataSize[i]).Substring(0, 4)), 16));
  109. }
  110. else if (DataSize[i] == 4)
  111. {
  112. Console.WriteLine("0x" + (RealData.Substring(SubIndex, DataSize[i])));
  113. ReturnData.Add(Convert.ToInt32("0x" + (RealData.Substring(SubIndex, DataSize[i])), 16));
  114. }
  115. }
  116. SubIndex += DataSize[i];
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. Console.WriteLine(ex.Message);
  123. }
  124. return ReturnData.ToArray();
  125. }
  126. string nextLine;
  127. private void Form1_Load(object sender, EventArgs e)
  128. {
  129. client = new TCPClient("172.16.15.10", "1030", "", "");
  130. //label1.BackColor = Color.GreenYellow;
  131. //string json = "";
  132. //string json1 = "";
  133. //MESHelper mes = new MESHelper();
  134. //mes.GetMobileAllInfo("Q32024040002", out json1, out json);
  135. //////mes.GetMobileAllInfo("F45112235030001", out json1, out json);
  136. //Console.WriteLine(json1);
  137. //Console.WriteLine(json);
  138. //StreamReader sR = new StreamReader(@"C:\Users\callm\Desktop\Android\036653943B010A11-C.txt", Encoding.Default);
  139. //while ((nextLine = sR.ReadLine()) != null)
  140. //{
  141. // ReturnData.AppendText(nextLine.ToString() + "\n");
  142. //}
  143. }
  144. }
  145. }