Form1.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12. namespace DataGathering
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private TcpClient tcpClient;
  17. NetworkStream stream;
  18. private int bufferSize = 4096;
  19. private string startCmd = "02 73 52 4E 20 4C 4D 44 73 63 61 6E 64 61 74 61 03";
  20. int flag = 0;
  21. private bool isok = false;
  22. private static readonly object Lock = new object();
  23. List<string> validdatalist;
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. tcpClient = new TcpClient();
  28. tcpClient.Connect(IPAddress.Parse("192.168.1.31"), 2111);
  29. Console.WriteLine("连接成功");
  30. stream = tcpClient.GetStream();
  31. validdatalist = new List<string>();
  32. }
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. isok = false;
  36. List<string> datalist = new List<string>();
  37. Task.Factory.StartNew(() =>
  38. {
  39. while (!isok)
  40. {
  41. lock (Lock)
  42. {
  43. datalist.Clear();
  44. flag = sendCmd(startCmd);
  45. byte[] buffer = new byte[bufferSize];
  46. stream.Read(buffer, 0, buffer.Length);
  47. string str = HexStringToASCII(buffer);
  48. datalist.Add(str);
  49. }
  50. Thread.Sleep(200);
  51. }
  52. });
  53. //string testimg = "";
  54. Task.Factory.StartNew(() =>
  55. {
  56. while (!isok)
  57. {
  58. lock (Lock)
  59. {
  60. for (int i = 0; i < datalist.Count; i++)
  61. {
  62. string ss = validStr(datalist[i], "sRA LMDscandata 1 ", " not defined 0 0 0");
  63. string[] data = ss.Split(' ');
  64. string number = data[22];
  65. int a = Convert.ToInt32(number, 16);
  66. int[] validNum = new int[a];
  67. string aws = "";
  68. for (int j = 0; j < a; j++)
  69. {
  70. validNum[j] = Convert.ToInt32(data[23 + j], 16);
  71. aws = aws + validNum[j] + " ";
  72. }
  73. validdatalist.Add(aws);
  74. this.Invoke(new Action(() =>
  75. {
  76. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  77. sw.Start();
  78. aws.TrimEnd(' ');
  79. List<double> listy = new List<double>();
  80. List<double> listx = new List<double>();
  81. string[] origindata = aws.Split(' ');
  82. origindata = origindata.Where(s => !string.IsNullOrEmpty(s)).ToArray();
  83. int[] origindataTransformed = Array.ConvertAll<string, int>(origindata, int.Parse);
  84. //int startDegree = (Convert.ToInt32(this.textBox1.Text) + 45) * 3;
  85. //int endDegree = (Convert.ToInt32(this.textBox2.Text) + 45) * 3 + 1;
  86. for (int k = 0; k < origindataTransformed.Length; k++)
  87. {
  88. double x = origindataTransformed[k] * Math.Cos((45 + 0.333 * k) * Math.PI / 180);
  89. double y = origindataTransformed[k] * Math.Sin((45 + 0.333 * k) * Math.PI / 180);
  90. Log.WriteCredence(x + " " + y);
  91. listy.Add(y);
  92. listx.Add(x);
  93. }
  94. this.chart1.Series[0].Points.Clear();
  95. sw.Stop();
  96. Console.WriteLine(sw.ElapsedMilliseconds + "ms");
  97. }));
  98. }
  99. }
  100. Thread.Sleep(200);
  101. }
  102. });
  103. }
  104. private void InitChart(List<double> listx, List<double> listy)
  105. {
  106. //定义图表区域
  107. this.chart1.ChartAreas.Clear();
  108. ChartArea chartArea1 = new ChartArea("C1");
  109. this.chart1.ChartAreas.Add(chartArea1);
  110. //定义存储和显示点的容器
  111. this.chart1.Series.Clear();
  112. Series series1 = new Series("S1");
  113. series1.ChartArea = "C1";
  114. this.chart1.Series.Add(series1);
  115. //设置图表显示样式
  116. //this.chart1.ChartAreas[0].AxisY.Minimum = 0;
  117. //this.chart1.ChartAreas[0].AxisY.Maximum = 100;
  118. //this.chart1.ChartAreas[0].AxisX.Interval = 5;
  119. this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
  120. this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
  121. //设置标题
  122. this.chart1.Titles.Clear();
  123. this.chart1.Titles.Add("S01");
  124. this.chart1.Titles[0].Text = "雷达显示";
  125. this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
  126. this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
  127. //设置图表显示样式
  128. this.chart1.Series[0].Color = Color.Red;
  129. this.chart1.Series[0].ChartType = SeriesChartType.Point;
  130. for (int i = 0; i < listx.Count; i++)
  131. {
  132. this.chart1.Series[0].Points.AddXY(listx[i], listy[i]);
  133. }
  134. }
  135. private void button2_Click(object sender, EventArgs e)
  136. {
  137. isok = true;
  138. for (int i = 0; i < validdatalist.Count; i++)
  139. {
  140. string temp = validdatalist[i];
  141. string[] tempdata = temp.Split(' ');
  142. for (int j = 0; j < tempdata.Length; j++)
  143. {
  144. Log.WriteData("data" + i, tempdata[j]);
  145. }
  146. }
  147. Environment.Exit(0);
  148. this.Close();
  149. }
  150. private int sendCmd(string cmd)
  151. {
  152. if (cmd.Equals(startCmd))
  153. {
  154. byte[] byteStartCmd = HexStringToBinary(startCmd);
  155. string result = HexStringToASCII(byteStartCmd);
  156. byte[] b = Encoding.UTF8.GetBytes(result);
  157. stream.Write(b, 0, b.Length);
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. private static string HexStringToASCII(byte[] bt)
  163. {
  164. //byte[] bt = HexStringToBinary(hexstring);
  165. string lin = "";
  166. for (int i = 0; i < bt.Length; i++)
  167. {
  168. lin = lin + bt[i] + " ";
  169. }
  170. string[] ss = lin.Trim().Split(new char[] { ' ' });
  171. char[] c = new char[ss.Length];
  172. int a;
  173. for (int i = 0; i < c.Length; i++)
  174. {
  175. a = Convert.ToInt32(ss[i]);
  176. c[i] = Convert.ToChar(a);
  177. }
  178. string b = new string(c);
  179. return b;
  180. }
  181. private static byte[] HexStringToBinary(string hexstring)
  182. {
  183. string[] tmpary = hexstring.Trim().Split(' ');
  184. byte[] buff = new byte[tmpary.Length];
  185. for (int i = 0; i < buff.Length; i++)
  186. {
  187. buff[i] = Convert.ToByte(tmpary[i], 16);
  188. }
  189. return buff;
  190. }
  191. private static string validStr(string sourse, string startstr, string endstr)
  192. {
  193. string result = string.Empty;
  194. int startindex, endindex;
  195. try
  196. {
  197. startindex = sourse.IndexOf(startstr);
  198. if (startindex == -1)
  199. {
  200. return result;
  201. }
  202. string tmpstr = sourse.Substring(startindex + startstr.Length);
  203. endindex = tmpstr.IndexOf(endstr);
  204. if (endindex == -1)
  205. {
  206. return result;
  207. }
  208. result = tmpstr.Remove(endindex);
  209. }
  210. catch (Exception) { }
  211. return result;
  212. }
  213. private void button3_Click(object sender, EventArgs e)
  214. {
  215. List<string> datalist = new List<string>();
  216. datalist.Clear();
  217. flag = sendCmd(startCmd);
  218. byte[] buffer = new byte[bufferSize];
  219. stream.Read(buffer, 0, buffer.Length);
  220. string str = HexStringToASCII(buffer);
  221. datalist.Add(str);
  222. string ss = validStr(datalist[0], "sRA LMDscandata 1 ", " not defined 0 0 0");
  223. string[] data = ss.Split(' ');
  224. string number = data[22];
  225. int a = Convert.ToInt32(number, 16);
  226. int[] validNum = new int[a];
  227. string aws = "";
  228. for (int j = 0; j < a; j++)
  229. {
  230. validNum[j] = Convert.ToInt32(data[23 + j], 16);
  231. aws = aws + validNum[j] + " ";
  232. }
  233. aws.TrimEnd(' ');
  234. List<double> listy = new List<double>();
  235. List<double> listx = new List<double>();
  236. string[] origindata = aws.Split(' ');
  237. origindata = origindata.Where(s => !string.IsNullOrEmpty(s)).ToArray();
  238. int[] origindataTransformed = Array.ConvertAll<string, int>(origindata, int.Parse);
  239. //int startDegree = (Convert.ToInt32(this.textBox1.Text) + 45) * 3;
  240. //int endDegree = (Convert.ToInt32(this.textBox2.Text) + 45) * 3 + 1;
  241. for (int k = 0; k < origindataTransformed.Length; k++)
  242. {
  243. double x = origindataTransformed[k] * Math.Cos((45 + 0.333 * k) * Math.PI / 180);
  244. double y = origindataTransformed[k] * Math.Sin((45 + 0.333 * k) * Math.PI / 180);
  245. Log.WriteCredence(x + " " + y);
  246. //listy.Add(y);
  247. //listx.Add(x);
  248. }
  249. }
  250. }
  251. }