public static ResponseData SetFromByte(byte[] _arr) { ResponseData rst = new ResponseData(); try { string dataBady = Encoding.Default.GetString(_arr, 6, _arr.Length - 8); string[] datas = dataBady.Split('/'); int i = 0; rst.RecDateTime = DateTime.Parse(datas[i++]); rst.Organi = uint.Parse(datas[i++]); rst.PKID = uint.Parse(datas[i++]); rst.RespResult = uint.Parse(datas[i++]); rst.SPK1_Status = (datas[i++] == "1") ? true : false; rst.SPK2_Status = (datas[i++] == "1") ? true : false; rst.SPK3_Status = (datas[i++] == "1") ? true : false; rst.SPK4_Status = (datas[i++] == "1") ? true : false; rst.SPK5_Status = (datas[i++] == "1") ? true : false; rst.SPK6_Status = (datas[i++] == "1") ? true : false; rst.SPK7_Status = (datas[i++] == "1") ? true : false; rst.SPK8_Status = (datas[i++] == "1") ? true : false; rst.Disaster = datas[i++]; rst.DeviceVer = datas[i++]; rst.Region = datas[i++]; rst.SwcStatus = byte.Parse(datas[i++]); rst.RcvMedia = byte.Parse(datas[i]); } catch { Console.Write(string.Format("\n[{0}] ResponseData SetFromByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); rst = null; } return(rst); }
public static byte[] GetToByte(ResponseData _data) { byte[] arr = null; try { byte[] soh = new byte[] { 0x01, 0x02 }; byte[] len = new byte[] { 0x30, 0x30, 0x30 }; byte cmd = (byte)'R'; byte[] b1 = Encoding.Default.GetBytes(_data.RecDateTime.ToString("yyyy-MM-dd HH:mm:ss")); byte[] b2 = Encoding.Default.GetBytes(string.Format("/{0}", _data.Organi)); byte[] b3 = Encoding.Default.GetBytes(string.Format("/{0}", _data.PKID)); byte[] b4 = Encoding.Default.GetBytes(string.Format("/{0}", _data.RespResult)); byte[] b5 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK1_Status) ? "1" : "0")); byte[] b6 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK2_Status) ? "1" : "0")); byte[] b7 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK3_Status) ? "1" : "0")); byte[] b8 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK4_Status) ? "1" : "0")); byte[] b9 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK5_Status) ? "1" : "0")); byte[] b10 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK6_Status) ? "1" : "0")); byte[] b11 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK7_Status) ? "1" : "0")); byte[] b12 = Encoding.Default.GetBytes(string.Format("/{0}", (_data.SPK8_Status) ? "1" : "0")); byte[] b13 = Encoding.Default.GetBytes(string.Format("/{0}", _data.Disaster)); byte[] b14 = Encoding.Default.GetBytes(string.Format("/{0}", _data.DeviceVer)); byte[] b15 = Encoding.Default.GetBytes(string.Format("/{0}", _data.Region)); byte[] b16 = Encoding.Default.GetBytes(string.Format("/{0}", _data.SwcStatus)); byte[] b17 = Encoding.Default.GetBytes(string.Format("/{0}", _data.RcvMedia)); byte[] eoh = new byte[] { 0x03, 0x04 }; int dataLen = b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length + b14.Length + b15.Length + b16.Length + b17.Length; len = Encoding.Default.GetBytes(string.Format("{0:000}", dataLen)); arr = new byte[8 + dataLen]; Buffer.BlockCopy(soh, 0, arr, 0, soh.Length); Buffer.BlockCopy(len, 0, arr, soh.Length, len.Length); arr[soh.Length + len.Length] = cmd; Buffer.BlockCopy(b1, 0, arr, soh.Length + len.Length + 1, b1.Length); Buffer.BlockCopy(b2, 0, arr, soh.Length + len.Length + 1 + b1.Length, b2.Length); Buffer.BlockCopy(b3, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length, b3.Length); Buffer.BlockCopy(b4, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length, b4.Length); Buffer.BlockCopy(b5, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length, b5.Length); Buffer.BlockCopy(b6, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length, b6.Length); Buffer.BlockCopy(b7, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length, b7.Length); Buffer.BlockCopy(b8, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length, b8.Length); Buffer.BlockCopy(b9, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length, b9.Length); Buffer.BlockCopy(b10, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length, b10.Length); Buffer.BlockCopy(b11, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length, b11.Length); Buffer.BlockCopy(b12, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length, b12.Length); Buffer.BlockCopy(b13, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length, b13.Length); Buffer.BlockCopy(b14, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length, b14.Length); Buffer.BlockCopy(b15, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length + b14.Length, b15.Length); Buffer.BlockCopy(b16, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length + b14.Length + b15.Length, b16.Length); Buffer.BlockCopy(b17, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length + b14.Length + b15.Length + b16.Length, b17.Length); Buffer.BlockCopy(eoh, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length + b11.Length + b12.Length + b13.Length + b14.Length + b15.Length + b16.Length + b17.Length, eoh.Length); } catch (Exception ex) { Console.Write(string.Format("\n[{0}] ResponseData GetToByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); arr = null; } return(arr); }