UploadDataDialog.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace Metis.AutoAnalysis
  15. {
  16. public partial class UploadDataDialog : Form
  17. {
  18. /// <summary>
  19. /// 检查项目 1 : 夹杂物 2 :晶粒度
  20. /// </summary>
  21. public int CheckItem { get; set; }
  22. public string CheckResult
  23. {
  24. set
  25. {
  26. txtCheckItem.Text = value;
  27. }
  28. }
  29. private const string TEST_CODE_INCLUSION = "G1PI";
  30. private const string TEST_CODE_GRAIN = "G1PN";
  31. public string[] InfoList;
  32. public Socket socketclient = null;
  33. public UploadDataDialog(string[] infoList)
  34. {
  35. InitializeComponent();
  36. InfoList = infoList;
  37. txtSAMPLELOTNO.Text = infoList[2];
  38. txtSAMPLEID.Text = infoList[0];
  39. txtFD_SAMPLE_NO.Text = infoList[3];
  40. Connection();
  41. }
  42. private void Connection()
  43. {
  44. string ip = ConfigurationManager.AppSettings["server_ip"];
  45. string portstr = ConfigurationManager.AppSettings["server_port"];
  46. int.TryParse(portstr, out int port);
  47. //定义一个套接字监听
  48. socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  49. //获取IP地址
  50. IPAddress address = IPAddress.Parse(ip);
  51. //将获取的IP地址和端口号绑定在网络节点上
  52. IPEndPoint point = new IPEndPoint(address, port);
  53. try
  54. {
  55. //客户端套接字连接到网络节点上,用的是Connect
  56. socketclient.Connect(point);
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageBox.Show("服务器连接失败" + ex.Message);
  61. }
  62. }
  63. private string AllData(string[] infoList )
  64. {
  65. JObject jsonSent = new JObject();
  66. if(CheckItem == 1)
  67. {
  68. jsonSent.Add("TESTCODE", TEST_CODE_INCLUSION);
  69. }
  70. else if(CheckItem == 2)
  71. {
  72. jsonSent.Add("TESTCODE", TEST_CODE_GRAIN);
  73. }
  74. JArray infoArray = new JArray();
  75. JObject info1 = new JObject();
  76. //string retime = DateTime.Now.ToLocalTime().ToString();
  77. info1.Add("SAMPLELOTNO", infoList[2] ); //检验批号
  78. info1.Add("SAMPLEID", infoList[0]); //试批顺序号
  79. info1.Add("FD_SAMPLE_NO", infoList[3]); //试样号
  80. //info1.Add("RECVTIME", retime); //收样时间
  81. JObject data = new JObject();
  82. if (CheckItem == 1)
  83. {
  84. data = InclusionData();
  85. }
  86. else if (CheckItem == 2)
  87. {
  88. data = GrainData();
  89. }
  90. info1.Add("DATA", data);
  91. infoArray.Add(info1);
  92. jsonSent.Add("INFO", infoArray);
  93. String sentStr = jsonSent.ToString();
  94. Console.WriteLine(sentStr);
  95. return sentStr;
  96. }
  97. /// <summary>
  98. /// 晶粒度json DATA
  99. /// </summary>
  100. /// <returns></returns>
  101. private JObject GrainData()
  102. {
  103. string checkResult = txtResult.Text;
  104. JObject result = new JObject();
  105. float.TryParse(checkResult, out float grade);
  106. result.Add("CRYSTAL_GRAN_VALUE", grade);
  107. return result;
  108. }
  109. /// <summary>
  110. /// 夹杂物json DATA
  111. /// </summary>
  112. /// <returns></returns>
  113. private JObject InclusionData()
  114. {
  115. string checkResult = txtResult.Text;
  116. string [] arr = checkResult.Split(';');
  117. List<string> gradeList = new List<string>();
  118. for (int i = 0; i < arr.Length; i++)
  119. {
  120. string str = arr[i].Trim();
  121. string[] arrItem = str.Split(' ');
  122. for (int j = 0; j < arrItem.Length; j++)
  123. {
  124. string gradeStr = arrItem[j].Trim();
  125. gradeList.Add(gradeStr);
  126. }
  127. }
  128. List<string> AThickList = new List<string>();
  129. List<string> AThinList = new List<string>();
  130. List<string> BThickList = new List<string>();
  131. List<string> BThinList = new List<string>();
  132. List<string> CThickList = new List<string>();
  133. List<string> CThinList = new List<string>();
  134. List<string> DThickList = new List<string>();
  135. List<string> DThinList = new List<string>();
  136. List<string> DSList = new List<string>();
  137. //e是粗系
  138. foreach (var grade in gradeList)
  139. {
  140. string type = grade.Substring(0, 1);
  141. if(type =="A")
  142. {
  143. if(grade.Substring(grade.Length - 1) == "e")
  144. {
  145. string gradeValStr = grade.Substring(1, grade.Length - 2);
  146. AThickList.Add(gradeValStr);
  147. }
  148. else
  149. {
  150. string gradeValStr = grade.Substring(1, grade.Length - 1);
  151. AThinList.Add(gradeValStr);
  152. }
  153. }
  154. if (type == "B")
  155. {
  156. if (grade.Substring(grade.Length - 1) == "e")
  157. {
  158. string gradeValStr = grade.Substring(1, grade.Length - 2);
  159. BThickList.Add(gradeValStr);
  160. }
  161. else
  162. {
  163. string gradeValStr = grade.Substring(1, grade.Length - 1);
  164. BThinList.Add(gradeValStr);
  165. }
  166. }
  167. if (type == "C")
  168. {
  169. if (grade.Substring(grade.Length - 1) == "e")
  170. {
  171. string gradeValStr = grade.Substring(1, grade.Length - 2);
  172. CThickList.Add(gradeValStr);
  173. }
  174. else
  175. {
  176. string gradeValStr = grade.Substring(1, grade.Length - 1);
  177. CThinList.Add(gradeValStr);
  178. }
  179. }
  180. if (type == "D")
  181. {
  182. if(grade.Substring(1, 1) == "S")
  183. {
  184. string gradeValStr = grade.Substring(2, grade.Length - 2);
  185. DSList.Add(gradeValStr);
  186. }
  187. else
  188. {
  189. if (grade.Substring(grade.Length - 1) == "e")
  190. {
  191. string gradeValStr = grade.Substring(1, grade.Length - 2);
  192. DThickList.Add(gradeValStr);
  193. }
  194. else
  195. {
  196. string gradeValStr = grade.Substring(1, grade.Length - 1);
  197. DThinList.Add(gradeValStr);
  198. }
  199. }
  200. }
  201. }
  202. JObject result1 = new JObject();
  203. //=====================================================
  204. //A类
  205. float maxVal = -10000;
  206. foreach (var item in AThickList)
  207. {
  208. float.TryParse(item, out float fVal);
  209. if(maxVal < fVal)
  210. {
  211. maxVal = fVal;
  212. }
  213. }
  214. if (maxVal > 0)
  215. {
  216. result1.Add("A_THICK_VALUE", maxVal);
  217. }
  218. maxVal = -10000;
  219. foreach (var item in AThinList)
  220. {
  221. float.TryParse(item, out float fVal);
  222. if (maxVal < fVal)
  223. {
  224. maxVal = fVal;
  225. }
  226. }
  227. if (maxVal > 0)
  228. {
  229. result1.Add("A_THIN_VALUE", maxVal);
  230. }
  231. //=====================================================
  232. //B类
  233. maxVal = -10000;
  234. foreach (var item in BThickList)
  235. {
  236. float.TryParse(item, out float fVal);
  237. if (maxVal < fVal)
  238. {
  239. maxVal = fVal;
  240. }
  241. }
  242. if (maxVal > 0)
  243. {
  244. result1.Add("B_THICK_VALUE", maxVal);
  245. }
  246. maxVal = -10000;
  247. foreach (var item in BThinList)
  248. {
  249. float.TryParse(item, out float fVal);
  250. if (maxVal < fVal)
  251. {
  252. maxVal = fVal;
  253. }
  254. }
  255. if (maxVal > 0)
  256. {
  257. result1.Add("B_THIN_VALUE", maxVal);
  258. }
  259. //=====================================================
  260. //C类
  261. maxVal = -10000;
  262. foreach (var item in CThickList)
  263. {
  264. float.TryParse(item, out float fVal);
  265. if (maxVal < fVal)
  266. {
  267. maxVal = fVal;
  268. }
  269. }
  270. if (maxVal > 0)
  271. {
  272. result1.Add("C_THICK_VALUE", maxVal);
  273. }
  274. maxVal = -10000;
  275. foreach (var item in CThinList)
  276. {
  277. float.TryParse(item, out float fVal);
  278. if (maxVal < fVal)
  279. {
  280. maxVal = fVal;
  281. }
  282. }
  283. if (maxVal > 0)
  284. {
  285. result1.Add("C_THIN_VALUE", maxVal);
  286. }
  287. //=====================================================
  288. //D类
  289. maxVal = -10000;
  290. foreach (var item in DThickList)
  291. {
  292. float.TryParse(item, out float fVal);
  293. if (maxVal < fVal)
  294. {
  295. maxVal = fVal;
  296. }
  297. }
  298. if (maxVal > 0)
  299. {
  300. result1.Add("D_THICK_VALUE", maxVal);
  301. }
  302. maxVal = -10000;
  303. foreach (var item in DThinList)
  304. {
  305. float.TryParse(item, out float fVal);
  306. if (maxVal < fVal)
  307. {
  308. maxVal = fVal;
  309. }
  310. }
  311. if (maxVal > 0)
  312. {
  313. result1.Add("D_THIN_VALUE", maxVal);
  314. }
  315. //=====================================================
  316. //DS类
  317. maxVal = -10000;
  318. foreach (var item in DSList)
  319. {
  320. float.TryParse(item, out float fVal);
  321. if (maxVal < fVal)
  322. {
  323. maxVal = fVal;
  324. }
  325. }
  326. if (maxVal > 0)
  327. {
  328. result1.Add("DS_VALUE", maxVal);
  329. }
  330. return result1;
  331. }
  332. private void btnSend_Click(object sender, EventArgs e)
  333. {
  334. string strAll = AllData(InfoList);
  335. byte[] arr = System.Text.Encoding.UTF8.GetBytes(strAll);
  336. byte[] sentBytes = new byte[arr.Length + 2];
  337. sentBytes[0] = 0x02;
  338. System.Array.Copy(arr, 0, sentBytes, 1, arr.Length);
  339. sentBytes[sentBytes.Length - 1] = 0x03;
  340. try
  341. {
  342. //调用客户端套接字发送字节数组 .ToHexString()
  343. int suc = socketclient.Send(sentBytes);
  344. }
  345. catch (Exception ex)
  346. {
  347. MessageBox.Show("发送数据失败 " + ex.Message);
  348. }
  349. }
  350. private void btnClose_Click(object sender, EventArgs e)
  351. {
  352. this.Close();
  353. }
  354. }
  355. }