using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Metis.AutoAnalysis { public partial class UploadDataDialog : Form { /// /// 检查项目 1 : 夹杂物 2 :晶粒度 /// public int CheckItem { get; set; } public string CheckResult { set { txtCheckItem.Text = value; } } private const string TEST_CODE_INCLUSION = "G1PI"; private const string TEST_CODE_GRAIN = "G1PN"; public string[] InfoList; public Socket socketclient = null; public UploadDataDialog(string[] infoList) { InitializeComponent(); InfoList = infoList; txtSAMPLELOTNO.Text = infoList[2]; txtSAMPLEID.Text = infoList[0]; txtFD_SAMPLE_NO.Text = infoList[3]; Connection(); } private void Connection() { string ip = ConfigurationManager.AppSettings["server_ip"]; string portstr = ConfigurationManager.AppSettings["server_port"]; int.TryParse(portstr, out int port); //定义一个套接字监听 socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //获取IP地址 IPAddress address = IPAddress.Parse(ip); //将获取的IP地址和端口号绑定在网络节点上 IPEndPoint point = new IPEndPoint(address, port); try { //客户端套接字连接到网络节点上,用的是Connect socketclient.Connect(point); } catch (Exception ex) { MessageBox.Show("服务器连接失败" + ex.Message); } } private string AllData(string[] infoList ) { JObject jsonSent = new JObject(); if(CheckItem == 1) { jsonSent.Add("TESTCODE", TEST_CODE_INCLUSION); } else if(CheckItem == 2) { jsonSent.Add("TESTCODE", TEST_CODE_GRAIN); } JArray infoArray = new JArray(); JObject info1 = new JObject(); //string retime = DateTime.Now.ToLocalTime().ToString(); info1.Add("SAMPLELOTNO", infoList[2] ); //检验批号 info1.Add("SAMPLEID", infoList[0]); //试批顺序号 info1.Add("FD_SAMPLE_NO", infoList[3]); //试样号 //info1.Add("RECVTIME", retime); //收样时间 JObject data = new JObject(); if (CheckItem == 1) { data = InclusionData(); } else if (CheckItem == 2) { data = GrainData(); } info1.Add("DATA", data); infoArray.Add(info1); jsonSent.Add("INFO", infoArray); String sentStr = jsonSent.ToString(); Console.WriteLine(sentStr); return sentStr; } /// /// 晶粒度json DATA /// /// private JObject GrainData() { string checkResult = txtResult.Text; JObject result = new JObject(); float.TryParse(checkResult, out float grade); result.Add("CRYSTAL_GRAN_VALUE", grade); return result; } /// /// 夹杂物json DATA /// /// private JObject InclusionData() { string checkResult = txtResult.Text; string [] arr = checkResult.Split(';'); List gradeList = new List(); for (int i = 0; i < arr.Length; i++) { string str = arr[i].Trim(); string[] arrItem = str.Split(' '); for (int j = 0; j < arrItem.Length; j++) { string gradeStr = arrItem[j].Trim(); gradeList.Add(gradeStr); } } List AThickList = new List(); List AThinList = new List(); List BThickList = new List(); List BThinList = new List(); List CThickList = new List(); List CThinList = new List(); List DThickList = new List(); List DThinList = new List(); List DSList = new List(); //e是粗系 foreach (var grade in gradeList) { string type = grade.Substring(0, 1); if(type =="A") { if(grade.Substring(grade.Length - 1) == "e") { string gradeValStr = grade.Substring(1, grade.Length - 2); AThickList.Add(gradeValStr); } else { string gradeValStr = grade.Substring(1, grade.Length - 1); AThinList.Add(gradeValStr); } } if (type == "B") { if (grade.Substring(grade.Length - 1) == "e") { string gradeValStr = grade.Substring(1, grade.Length - 2); BThickList.Add(gradeValStr); } else { string gradeValStr = grade.Substring(1, grade.Length - 1); BThinList.Add(gradeValStr); } } if (type == "C") { if (grade.Substring(grade.Length - 1) == "e") { string gradeValStr = grade.Substring(1, grade.Length - 2); CThickList.Add(gradeValStr); } else { string gradeValStr = grade.Substring(1, grade.Length - 1); CThinList.Add(gradeValStr); } } if (type == "D") { if(grade.Substring(1, 1) == "S") { string gradeValStr = grade.Substring(2, grade.Length - 2); DSList.Add(gradeValStr); } else { if (grade.Substring(grade.Length - 1) == "e") { string gradeValStr = grade.Substring(1, grade.Length - 2); DThickList.Add(gradeValStr); } else { string gradeValStr = grade.Substring(1, grade.Length - 1); DThinList.Add(gradeValStr); } } } } JObject result1 = new JObject(); //===================================================== //A类 float maxVal = -10000; foreach (var item in AThickList) { float.TryParse(item, out float fVal); if(maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("A_THICK_VALUE", maxVal); } maxVal = -10000; foreach (var item in AThinList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("A_THIN_VALUE", maxVal); } //===================================================== //B类 maxVal = -10000; foreach (var item in BThickList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("B_THICK_VALUE", maxVal); } maxVal = -10000; foreach (var item in BThinList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("B_THIN_VALUE", maxVal); } //===================================================== //C类 maxVal = -10000; foreach (var item in CThickList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("C_THICK_VALUE", maxVal); } maxVal = -10000; foreach (var item in CThinList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("C_THIN_VALUE", maxVal); } //===================================================== //D类 maxVal = -10000; foreach (var item in DThickList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("D_THICK_VALUE", maxVal); } maxVal = -10000; foreach (var item in DThinList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("D_THIN_VALUE", maxVal); } //===================================================== //DS类 maxVal = -10000; foreach (var item in DSList) { float.TryParse(item, out float fVal); if (maxVal < fVal) { maxVal = fVal; } } if (maxVal > 0) { result1.Add("DS_VALUE", maxVal); } return result1; } private void btnSend_Click(object sender, EventArgs e) { string strAll = AllData(InfoList); byte[] arr = System.Text.Encoding.UTF8.GetBytes(strAll); byte[] sentBytes = new byte[arr.Length + 2]; sentBytes[0] = 0x02; System.Array.Copy(arr, 0, sentBytes, 1, arr.Length); sentBytes[sentBytes.Length - 1] = 0x03; try { //调用客户端套接字发送字节数组 .ToHexString() int suc = socketclient.Send(sentBytes); } catch (Exception ex) { MessageBox.Show("发送数据失败 " + ex.Message); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }