private TssMsg Msg2TssMsg(byte ctrl, string datatype, string functype, string canshu, byte[] shuju) { TssMsg tm = new TssMsg(); tm.deviceID = "0"; tm.flag = "$RADIOINF"; tm.ctrl = ctrl; tm.datatype = datatype; tm.functype = functype; tm.canshuquchangdu = 0; tm.shujuquchangdu = 0; int numofmsg = 102; if (canshu != "") { tm.canshuqu = canshu; byte[] b = Encoding.Default.GetBytes(tm.canshuqu); tm.canshuquchangdu = (Int16)b.Length; numofmsg = numofmsg + b.Count(); } if (shuju != null) { tm.shujuqu = new byte[shuju.Length]; Array.Copy(shuju, tm.shujuqu, shuju.Count()); tm.shujuquchangdu = shuju.Count(); numofmsg = numofmsg + shuju.Count(); } tm.lenofmsg = numofmsg; return(tm); }
private TssMsg Byte2tssmsg(byte[] by) { TssMsg tm = new TssMsg(); try { tm.flag = Encoding.Default.GetString(by, 0, 9).TrimEnd('\0'); tm.crc = by[9].ToString(); tm.lenofmsg = BitConverter.ToInt32(by, 10); tm.ctrl = by[14]; tm.xieyibanbenhao = by[15]; tm.datatype = Encoding.Default.GetString(by, 16, 16).TrimEnd('\0'); tm.functype = Encoding.Default.GetString(by, 32, 16).TrimEnd('\0'); tm.baowenxuhao = BitConverter.ToInt32(by, 48); tm.baotouchangdu = BitConverter.ToInt16(by, 52); tm.canshuquchangdu = BitConverter.ToInt16(by, 54); tm.shujuquchangdu = BitConverter.ToInt32(by, 56); try { string shijian = ""; shijian = "20" + by[60] + "-" + by[61] + "-" + by[62] + " " + by[63] + ":" + by[64] + ":" + by[65]; tm.shibiao = DateTime.Parse(shijian).ToString("yyyy-MM-dd HH:mm:ss"); } catch (Exception ex) { } tm.deviceID = Encoding.Default.GetString(by, 72, 10).TrimEnd('\0'); tm.source = Encoding.Default.GetString(by, 82, 10).TrimEnd('\0'); tm.destination = Encoding.Default.GetString(by, 82, 10).TrimEnd('\0'); if (tm.canshuquchangdu > 0) { tm.canshuqu = Encoding.Default.GetString(by, 102, tm.canshuquchangdu).TrimEnd('\0'); } if (tm.shujuquchangdu > 0) { tm.shujuqu = new byte[tm.shujuquchangdu]; Array.Copy(by, 102 + tm.canshuquchangdu, tm.shujuqu, 0, tm.shujuquchangdu); } return(tm); } catch (Exception ex) { } return(tm); }
private void StartUdpClient() { while (true) { try { using (UdpClient udpClient = new UdpClient(localPort)) { TssMsg tm = Msg2TssMsg(0, "task", "getheartbeat", "", null); byte[] buffer = Tssmsg2byte(tm); Log("<Send>" + buffer.Length); udpClient.Send(buffer, buffer.Length, remoteIpEndPoint); } Thread.Sleep(sleepTimes); } catch (Exception e) { Log("<Send Err>" + e.ToString()); } } }
private void StartUdpServer() { while (true) { try { using (UdpClient udpClient = new UdpClient(localPort)) { TssMsg tm = Msg2TssMsg(0, "task", "getheartbeat", "", null); byte[] buffer = Tssmsg2byte(tm); if (buffer == null) { Log("<Send> buffer is null"); } Log("<Send>" + buffer.Length); udpClient.Send(buffer, buffer.Length, remoteIpEndPoint); IPEndPoint rp = new IPEndPoint(IPAddress.Any, 0); buffer = null; buffer = udpClient.Receive(ref rp); string thisRemoteIp = rp.Address.ToString(); if (thisRemoteIp == remoteIp) { tm = Byte2tssmsg(buffer); Log(tm.datatype + "," + tm.functype + "," + tm.canshuqu); if (tm.datatype == "Data" && tm.functype == "heartbeat") { string msg = tm.canshuqu; string str = msg.Replace("<", "").Replace(">", ""); string headFunc = str.Split(':')[0]; if (headFunc == "heartbeat") { OnUdpHeartBeat(tm.canshuqu); string[] paras = str.Split(';'); GateWayStatusInfo gateWayStatusInfo = new GateWayStatusInfo(); foreach (string kv in paras) { string key = kv.Split('=')[0]; string value = kv.Split('=')[1]; if (key == "swnet") { gateWayStatusInfo.net = value; } if (key == "swpow") { gateWayStatusInfo.power = value; } if (key == "longitude") { gateWayStatusInfo.lon = double.Parse(value); } if (key == "latitude") { gateWayStatusInfo.lat = double.Parse(value); } if (key == "voltage") { OnUdpVoltageInfo(value); gateWayStatusInfo.vlotage = double.Parse(value); } } OnGateWayStatusInfo(gateWayStatusInfo); } } } } } catch (Exception e) { Log("<Send Err>" + e.ToString()); } Thread.Sleep(sleepTimes); } }
private byte[] Tssmsg2byte(TssMsg tm) { byte[] by = new byte[102]; Array.Copy(tobyte(tm.flag), by, tobyte(tm.flag).Length); Array.Copy(BitConverter.GetBytes(tm.lenofmsg + 1), 0, by, 10, 4); Array.Copy(new byte[] { tm.ctrl }, 0, by, 14, 1); Array.Copy(new byte[] { tm.xieyibanbenhao }, 0, by, 15, 1); Array.Copy(tobyte(tm.datatype), 0, by, 16, tobyte(tm.datatype).Length); Array.Copy(tobyte(tm.functype), 0, by, 32, tobyte(tm.functype).Length); Array.Copy(BitConverter.GetBytes(tm.baowenxuhao), 0, by, 48, 4); Array.Copy(BitConverter.GetBytes(tm.baotouchangdu), 0, by, 52, 2); Array.Copy(BitConverter.GetBytes(tm.canshuquchangdu), 0, by, 54, 2); Array.Copy(BitConverter.GetBytes(tm.shujuquchangdu), 0, by, 56, 4); // 时标 DateTime de = DateTime.Now; int yy = int.Parse(de.ToString("yy")); int MM = int.Parse(de.ToString("MM")); int dd = int.Parse(de.ToString("dd")); int HH = int.Parse(de.ToString("HH")); int m = int.Parse(de.ToString("mm")); int ss = int.Parse(de.ToString("ss")); Array.Copy(new byte[] { (byte)yy }, 0, by, 60, 1); Array.Copy(new byte[] { (byte)MM }, 0, by, 61, 1); Array.Copy(new byte[] { (byte)dd }, 0, by, 62, 1); Array.Copy(new byte[] { (byte)HH }, 0, by, 63, 1); Array.Copy(new byte[] { (byte)m }, 0, by, 64, 1); Array.Copy(new byte[] { (byte)ss }, 0, by, 65, 1); short ms = 0; Array.Copy(BitConverter.GetBytes(ms), 0, by, 66, 2); Array.Copy(BitConverter.GetBytes(ms), 0, by, 68, 2); Array.Copy(BitConverter.GetBytes(ms), 0, by, 70, 2); Array.Copy(tobyte(tm.deviceID), 0, by, 72, tobyte(tm.deviceID).Length); if (tm.source != null) { Array.Copy(tobyte(tm.source), 0, by, 82, tobyte(tm.source).Length); } if (tm.destination != null) { Array.Copy(tobyte(tm.destination), 0, by, 92, tobyte(tm.destination).Length); } // CRC byte crcInt = getHeadcrc(by); Array.Copy(new byte[] { crcInt }, 0, by, 9, 1); byte[] bu = null; if (tm.canshuquchangdu > 0) { bu = tobyte(tm.canshuqu); } byte[] bk = null; if (tm.shujuquchangdu > 0) { bk = tm.shujuqu; } int k1 = 0; int k2 = 0; if (bu != null) { k1 = bu.Count(); } if (bk != null) { k2 = bk.Count(); } byte[] bbb = new byte[101 + k1 + k2 + 1]; Array.Copy(by, 0, bbb, 0, 102); if (bu != null) { Array.Copy(bu, 0, bbb, 102, k1); } if (bk != null) { Array.Copy(bk, 0, bbb, 102 + k1, k2); } int t = getcrc(bbb); byte[] bkk; byte[] bt = new byte[1]; bt[0] = (byte)t; bkk = bbb.Concat(bt).ToArray(); return(bkk); }