/// <summary> /// 构建终端(平台)通用应答 /// </summary> /// <param name="SerialNumByte">对应的终端消息的流水号</param> /// <param name="IdType">对应的终端消息的ID</param> /// <param name="result">结果</param> /// <returns></returns> public TMLOrPFCommonRsp GetTMLOrPFCommonRsp(byte[] SerialNumByte, BuBiaoMsgIdType IdType, byte result) { TMLOrPFCommonRsp TPCRsp = new TMLOrPFCommonRsp(); TPCRsp.SerialNumByte = SerialNumByte; TPCRsp.RspID = IdType; TPCRsp.Result = result; TPCRsp.WriteBuffer(); return TPCRsp; }
/// <summary> /// 字节数组转结构体 /// </summary> public void ReadBuffer() { if (dataBuffer != null) { //应答流水号 SerialNumByte = new byte[2]; int Index = 0; Array.Copy(dataBuffer, Index, SerialNumByte, 0, 2); SerialNum = BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(SerialNumByte), 0); //应答ID Index += 2; RspIDByte = new byte[2]; Array.Copy(dataBuffer, Index, RspIDByte, 0, 2); RspID = (BuBiaoMsgIdType)Enum.ToObject(typeof(BuBiaoMsgIdType), BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(RspIDByte), 0)); //结果 Index += 2; Result = dataBuffer[Index]; } }
//public GPRSFrame(BuBiaoMsgIdType MsgID, bool IsMutiPackage, EncryptType IsEncrypt, UInt16 DataLength // , byte[] TMLPhoneNum, byte[] SerialNum, UInt16 PackageCount, UInt16 PackageIndex, byte[] Data) public GPRSFrame(BuBiaoMsgIdType MsgID, bool IsMutiPackage, EncryptType IsEncrypt, UInt16 DataLength , string FullGpsCode, UInt16 SerialNum, UInt16 PackageCount, UInt16 PackageIndex, byte[] Data) { byte[] PhoneNumberByt; this.MsgIDByte = null; this.MsgID = MsgID; this.MspProperty = null; this.IsMutiPackage = IsMutiPackage; this.IsEncrypt = IsEncrypt; this.DataLength = DataLength; this.TMLPhoneNum = string.Empty; this.GPSCode = string.Empty; string strTmpSimNum = FullGpsCode.Substring(4); string strPhoneNumber = ""; if (strTmpSimNum.Length == 11) strPhoneNumber = "0" + strTmpSimNum; else strPhoneNumber = "0" + strTmpSimNum.PadLeft(11, '1'); PhoneNumberByt = new byte[strPhoneNumber.Length]; for (int i = 0; i < strPhoneNumber.Length; i++) { PhoneNumberByt[i] = Convert.ToByte(strPhoneNumber.Substring(i, 1)); } this.TMLPhoneNumByte = BuBiaoData.IntToBCD(PhoneNumberByt); this.SerialNum = new byte[2]; byte[] tmpWriteBuffer = new byte[2]; tmpWriteBuffer = BitConverter.GetBytes(SerialNum); Array.Copy(BuBiaoData.LocalByteToNetByteOrder(tmpWriteBuffer), 0, this.SerialNum, 0, 2); //this.SerialNum = SerialNum; this.PackageCountByte = null; this.PackageCount = PackageCount; this.PackageIndexByte = null; this.PackageIndex = PackageIndex; this.Data = Data; this.CheckSum = 0; this.IsValidData = false; this.LastPackUpdateTime = DateTime.Now; this.dicChildPacket = new Dictionary<ushort, byte[]>(); }
/// <summary> /// 将接收到的字节数组转换为结构体 /// </summary> /// <param name="receivedBuffer">还原后的字节数组</param> public void ReadBuffer(byte[] receivedBuffer) { //检验是否有帧头帧尾 int dataLen = receivedBuffer.Length; if (receivedBuffer[0] == 0x7E && receivedBuffer[dataLen - 1] == 0x7E) { //去掉帧头帧尾的数据帧 byte[] dataBuffer = new byte[dataLen - 2]; Array.Copy(receivedBuffer, 1, dataBuffer, 0, dataBuffer.Length); //消息ID MsgIDByte = new byte[] { dataBuffer[0], dataBuffer[1] }; MsgID = (BuBiaoMsgIdType)Enum.ToObject(typeof(BuBiaoMsgIdType), BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(MsgIDByte), 0)); //消息体属性 MspProperty = new byte[] { dataBuffer[2], dataBuffer[3] }; //消息体属性的内容 IsMutiPackage = BuBiaoData.getBit(MspProperty[0], 5) == 1 ? true : false; //如果第10位(相当于高字节第二位)为1,则表示RSA加密 if (BuBiaoData.getBit(MspProperty[0], 2) == 1) { IsEncrypt = EncryptType.RSA; } else { IsEncrypt = EncryptType.NoEncrypt; } //取得消息体长度的前两位 byte Length1 = BuBiaoData.DoByteAND(MspProperty[0], 0x03); //取得消息体长度 byte[] LengthBytes = { Length1, MspProperty[1] }; //消息体长度 DataLength = BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(LengthBytes), 0); //终端手机号 TMLPhoneNumByte = new byte[] { dataBuffer[4], dataBuffer[5], dataBuffer[6], dataBuffer[7], dataBuffer[8], dataBuffer[9] }; //标准部标协议 byte[] TMLPhoneNumInt = BuBiaoData.BCDToInt(TMLPhoneNumByte); StringBuilder sb = new StringBuilder(); for (int i = 0, i_Count = TMLPhoneNumInt.Length; i < i_Count; i++) { sb.Append(TMLPhoneNumInt[i].ToString()); } TMLPhoneNum = sb.ToString(); GPSCode = "0037" + TMLPhoneNum; //消息流水号 SerialNum = new byte[] { dataBuffer[10], dataBuffer[11] }; //消息体的起始位置 int DataStartIndex = 12; if (IsMutiPackage) { //消息总包数 PackageCountByte = new byte[] { dataBuffer[12], dataBuffer[13] }; PackageCount = BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(PackageCountByte), 0); //包序号 PackageIndexByte = new byte[] { dataBuffer[14], dataBuffer[15] }; PackageIndex = BitConverter.ToUInt16(BuBiaoData.LocalByteToNetByteOrder(PackageIndexByte), 0); DataStartIndex = 16; } //消息体 if (DataLength > 0) { Data = new byte[DataLength]; Array.Copy(dataBuffer, DataStartIndex, Data, 0, DataLength); } //校验码 CheckSum = dataBuffer[dataBuffer.Length - 1]; //计算校验和 if (BuBiaoData.GetCheckSum(receivedBuffer) == CheckSum) { //标为有效数据 IsValidData = true; } } }
/// <summary> /// 带参数构造方法 /// </summary> /// <param name="MsgID"></param> /// <param name="IsMutiPackage"></param> /// <param name="IsEncrypt"></param> /// <param name="DataLength"></param> /// <param name="TMLPhoneNum"></param> /// <param name="SerialNum"></param> /// <param name="PackageCount"></param> /// <param name="PackageIndex"></param> /// <param name="Data"></param> public GPRSFrame(BuBiaoMsgIdType MsgID, bool IsMutiPackage, EncryptType IsEncrypt, UInt16 DataLength , byte[] TMLPhoneNumbyte, byte[] SerialNum, UInt16 PackageCount, UInt16 PackageIndex, byte[] Data) { this.MsgIDByte = null; this.MsgID = MsgID; this.MspProperty = null; this.IsMutiPackage = IsMutiPackage; this.IsEncrypt = IsEncrypt; this.DataLength = DataLength; this.TMLPhoneNum = string.Empty; this.TMLPhoneNumByte = TMLPhoneNumbyte; this.GPSCode = string.Empty; this.SerialNum = SerialNum; this.PackageCountByte = null; this.PackageCount = PackageCount; this.PackageIndexByte = null; this.PackageIndex = PackageIndex; this.Data = Data; this.CheckSum = 0; this.IsValidData = false; this.LastPackUpdateTime = DateTime.Now; this.dicChildPacket = new Dictionary<ushort, byte[]>(); }