Form1.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace sickTest
  10. {
  11. public partial class Form1 : Form
  12. {
  13. private TcpClient tcpClient;
  14. private UdpClient udpClient;
  15. NetworkStream stream;
  16. private int bufferSize = 0x8000;
  17. private string startCmd = "02 73 45 4E 20 4C 4D 44 73 63 61 6E 64 61 74 61 20 31 03";
  18. private string endCmd = "02 73 45 4E 20 4C 4D 44 73 63 61 6E 64 61 74 61 20 30 03";
  19. int flag = 0;
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. tcpClient = new TcpClient();
  24. tcpClient.Connect(IPAddress.Parse("192.168.1.30"), 2112);
  25. Console.WriteLine("连接成功");
  26. stream = tcpClient.GetStream();
  27. }
  28. //开始
  29. private void button1_Click(object sender, EventArgs e)
  30. {
  31. //byte[] byteStartCmd = HexStringToBinary(startCmd);
  32. //string result = HexStringToASCII(byteStartCmd);
  33. //byte[] b = Encoding.UTF8.GetBytes(result);
  34. //stream.Write(b, 0, b.Length);
  35. //byte[] data = new byte[bufferSize];
  36. //stream.Read(data, 0, bufferSize);
  37. //stream.Close();
  38. //string str = HexStringToASCII(data);
  39. // 初始化一个缓存区
  40. flag = sendCmd(startCmd);
  41. byte[] buffer = new byte[bufferSize];
  42. int read = 0;
  43. int block;
  44. Task.Factory.StartNew(() =>
  45. {
  46. while ((block = stream.Read(buffer, read, buffer.Length - read)) > 0 && flag != 2)
  47. {
  48. // 重新设定读取位置
  49. read += block;
  50. // 检查是否到达了缓存的边界,检查是否还有可以读取的信息
  51. if (read == buffer.Length)
  52. {
  53. // 尝试读取一个字节
  54. int nextByte = stream.ReadByte();
  55. // 读取失败则说明读取完成可以返回结果
  56. if (nextByte == -1)
  57. {
  58. Console.WriteLine("读取失败");
  59. //return buffer;
  60. }
  61. // 调整数组大小准备继续读取
  62. byte[] newBuf = new byte[buffer.Length * 2];
  63. Array.Copy(buffer, newBuf, buffer.Length);
  64. newBuf[read] = (byte)nextByte;
  65. // buffer是一个引用(指针),这里意在重新设定buffer指针指向一个更大的内存
  66. buffer = newBuf;
  67. read++;
  68. }
  69. Thread.Sleep(1);
  70. }
  71. });
  72. Task.Factory.StartNew(() =>
  73. {
  74. string str;
  75. while (buffer != null && flag != 2)
  76. {
  77. str = System.Text.Encoding.Default.GetString(buffer);
  78. Log.WriteLog(LogType.PROCESS, str);
  79. //this.Invoke(new Action(() =>
  80. //{
  81. // str = System.Text.Encoding.Default.GetString(buffer);
  82. // this.richTextBox1.Text = str;
  83. // Thread.Sleep(1000);
  84. // this.richTextBox1.Clear();
  85. // string[] data = str.Split(' ');
  86. // if (data.Length >= 25)
  87. // {
  88. // Console.WriteLine(data[25]);
  89. // }
  90. //}));
  91. Thread.Sleep(1000);
  92. }
  93. });
  94. }
  95. //结束
  96. private void button2_Click(object sender, EventArgs e)
  97. {
  98. flag = sendCmd(endCmd);
  99. }
  100. private int sendCmd(string cmd)
  101. {
  102. if (cmd.Equals(startCmd))
  103. {
  104. byte[] byteStartCmd = HexStringToBinary(startCmd);
  105. string result = HexStringToASCII(byteStartCmd);
  106. byte[] b = Encoding.UTF8.GetBytes(result);
  107. stream.Write(b, 0, b.Length);
  108. return 1;
  109. }
  110. if (cmd.Equals(endCmd))
  111. {
  112. byte[] byteStartCmd = HexStringToBinary(endCmd);
  113. string result = HexStringToASCII(byteStartCmd);
  114. byte[] b = Encoding.UTF8.GetBytes(result);
  115. stream.Write(b, 0, b.Length);
  116. return 2;
  117. }
  118. return 0;
  119. }
  120. /// <summary>
  121. /// 将一条十六进制字符串转换为ASCII
  122. /// </summary>
  123. /// <param name="hexstring">一条十六进制字符串</param>
  124. /// <returns>返回一条ASCII码</returns>
  125. private static string HexStringToASCII(byte[] bt)
  126. {
  127. //byte[] bt = HexStringToBinary(hexstring);
  128. string lin = "";
  129. for (int i = 0; i < bt.Length; i++)
  130. {
  131. lin = lin + bt[i] + " ";
  132. }
  133. string[] ss = lin.Trim().Split(new char[] { ' ' });
  134. char[] c = new char[ss.Length];
  135. int a;
  136. for (int i = 0; i < c.Length; i++)
  137. {
  138. a = Convert.ToInt32(ss[i]);
  139. c[i] = Convert.ToChar(a);
  140. }
  141. string b = new string(c);
  142. return b;
  143. }
  144. /// <summary>
  145. /// 16进制字符串转换为二进制数组
  146. /// </summary>
  147. /// <param name="hexstring">用空格切割字符串</param>
  148. /// <returns>返回一个二进制字符串</returns>
  149. private static byte[] HexStringToBinary(string hexstring)
  150. {
  151. string[] tmpary = hexstring.Trim().Split(' ');
  152. byte[] buff = new byte[tmpary.Length];
  153. for (int i = 0; i < buff.Length; i++)
  154. {
  155. buff[i] = Convert.ToByte(tmpary[i], 16);
  156. }
  157. return buff;
  158. }
  159. }
  160. }