public static entity.Trank getDataSource(String str) { //首先对字符串进行处理去掉头尾的空格 //str.TrimStart(' ').TrimEnd(' '); entity.Trank tk = new entity.Trank(); str = Regex.Replace(str, @"\s+", " ").Trim(); if (str.StartsWith("7E") && str.EndsWith("7E")) { str = trimFirEndAndChange(str); if (checkBcc(str)) { string[] sArray = Regex.Split(str, " "); String trimId = ""; for (int i = 0; i < 20; i++) { trimId += sArray[i + 4].Remove(0, 1); } trimId = trimId.Remove(0, 4); String infoNum = ConvertHelper.ConvertBase(sArray[24] + sArray[25], 16, 10); //设备类型sArray[26] //设备上传的时间 String trimTime = (int.Parse(sArray[27]) + 2000) + "/" + sArray[28] + "/" + sArray[29] + " " + sArray[30] + ":" + sArray[31] + ":" + sArray[32]; //电压 String voltage = (int.Parse(ConvertHelper.ConvertBase(sArray[33] + sArray[34], 16, 10)) * 0.1) + ""; String pith = getPRY(sArray[35] + sArray[36]); String roll = getPRY(sArray[37] + sArray[38]); String yaw = getPRY(sArray[39] + sArray[40]); //经度 String longitude = getLonLat(sArray[41] + sArray[42] + sArray[43] + sArray[44]); //纬度 String latitude = getLonLat(sArray[45] + sArray[46] + sArray[47] + sArray[48]); //距离 String distance = getTempDis(sArray[49] + sArray[50]); //温度 String temp = getTempDis(sArray[51] + sArray[52]); String statValue = getStatValue(sArray[53] + sArray[54]); String lotrpu = ConvertHelper.ConvertBase(statValue, 16, 2); lotrpu = ConvertHelper.RepairZero(lotrpu, 16); String loca = ConvertHelper.ConvertBase(lotrpu.Substring(8, 8), 2, 10); String trno = ConvertHelper.ConvertBase(lotrpu.Substring(5, 3), 2, 10); String putsta = ConvertHelper.ConvertBase(lotrpu.Substring(3, 2), 2, 10); //人力制动状态码 String brakestatusCode = sArray[55] + sArray[56]; //电流 String electricity = "" + (int.Parse(ConvertHelper.ConvertBase(sArray[57] + sArray[58], 16, 10)) * 0.1); String Gprs = gteGPRS(sArray[57], sArray[58]); tk.TrimId = trimId; tk.InfoNum = infoNum; tk.TrimTime = trimTime; tk.Voltage = voltage; tk.Pith = pith; tk.Roll = roll; tk.Yaw = yaw; tk.Longitude = longitude; tk.Latitude = latitude; tk.Distance = distance; tk.Temp = temp; tk.StatValue = statValue; tk.Loca = loca; tk.Trno = trno; tk.Putsta = putsta; tk.BrakestatusCode = brakestatusCode; tk.Electricity = electricity; tk.GPRS1 = Gprs; str = "trimId:" + trimId + "infoNum:" + infoNum + "trimTime:" + trimTime + "voltage:" + voltage + "pith:" + pith + "roll:" + roll + "yaw:" + yaw + "longitude:" + longitude + "latitude:" + latitude + "distance:" + distance + "temp:" + temp + "statValue:" + statValue + "loca:" + loca + "trno:" + trno + "putsta:" + putsta + "brakestatusCode:" + brakestatusCode + "electricity:" + electricity; Console.WriteLine("str:" + str); return(tk); } // } else { str = "7E数据不匹配"; } return(tk); }
public Datavive() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false;//设置该属性 为false client.Completed += new Action <System.Net.Sockets.TcpClient, EnSocketAction>((c, enAction) => { IPEndPoint iep = c.Client.RemoteEndPoint as IPEndPoint; string key = string.Format("{0}:{1}", iep.Address.ToString(), iep.Port); switch (enAction) { case EnSocketAction.Connect: Console.WriteLine("已经与{0}建立连接", key); break; case EnSocketAction.SendMsg: Console.WriteLine("{0}:向{1}发送了一条消息", DateTime.Now, key); break; case EnSocketAction.Close: Console.WriteLine("服务端连接关闭"); break; default: break; } }); //接收 try { client.Received += new Action <string, string>((key, msg) => { entity.Trank tk = DataSource.getDataSource(msg); label41.Text = tk.TrimId; label4.Text = tk.InfoNum; label39.Text = tk.TrimTime; label37.Text = tk.Voltage; label35.Text = tk.Pith; label33.Text = tk.Roll; label31.Text = tk.Yaw; label7.Text = tk.Longitude; label5.Text = tk.Latitude; label11.Text = tk.Distance; label9.Text = tk.Temp; string[] sTrings = tk.StatValue.Split(','); label15.Text = sTrings[0]; label27.Text = sTrings[1]; label13.Text = sTrings[2]; label25.Text = sTrings[3]; label23.Text = sTrings[4]; label19.Text = sTrings[5]; label21.Text = sTrings[6]; label17.Text = sTrings[7]; label44.Text = sTrings[8]; label29.Text = tk.Loca; label49.Text = tk.Trno; label47.Text = tk.Putsta; label52.Text = tk.BrakestatusCode; label45.Text = tk.Electricity; string[] sTrings2 = tk.GPRS1.Split(','); label55.Text = sTrings2[0]; label51.Text = sTrings2[1]; Console.WriteLine(tk.TrimId); }); } catch (Exception ex) { } client.ConnectAsync("127.0.0.1", 6873); }