示例#1
0
        public override SecsMessageBase ToSecsMessage(byte[] data)
        {
            using (MemoryStream reader = new MemoryStream(data))
            {
                reader.Position = 0;

                byte[] lengthBytes = new byte[4];

                //get length byte
                reader.Read(lengthBytes, 0, lengthBytes.Length);
                Array.Reverse(lengthBytes);

                int dataLength = BitConverter.ToInt32(lengthBytes, 0);
                if (data.Length != dataLength + 4)
                {
                    //invalid data length
                    throw new Exception("Invalid data lenght");
                }

                //get header
                byte[] header = new byte[10];
                reader.Read(header, 0, header.Length);

                //get device id from header
                byte[] deviceIdBytes = new byte[2];
                Array.Copy(header, 0, deviceIdBytes, 0, 2);
                Array.Reverse(deviceIdBytes);
                ushort deviceId = BitConverter.ToUInt16(deviceIdBytes, 0);

                //get stream
                byte stream   = (byte)(header[2] & 0x7F);
                byte function = header[3];

                bool needReply = ((header[2] & 0x80) == 0x80);

                //transactionId.
                byte[] transactionIdBytes = new byte[4];
                Array.Copy(header, 6, transactionIdBytes, 0, transactionIdBytes.Length);
                Array.Reverse(transactionIdBytes);
                uint transactionId = BitConverter.ToUInt32(transactionIdBytes, 0);

                SecsMessageBase msg = GetSecsMessageInstance(stream, function, needReply);

                msg.ReadItems(reader);                          //Sec2 data read
                msg.NeedReply     = needReply;
                msg.TransactionId = transactionId;
                msg.DeviceId      = deviceId;

                return(msg);
            }
        }
        public override SecsMessageBase ToSecsMessage(byte[] data)
        {
            byte[] header = new byte[10];
            Array.Copy(data, 0, header, 0, header.Length);
            //header
            //[0][1][2][3][4][5][6][7][8][9] ...
            //[0][1]
            byte[] tmp2bytes = new byte[2];
            Array.Copy(header, 0, tmp2bytes, 0, tmp2bytes.Length);
            Array.Reverse(tmp2bytes);
            ushort deviceId = BitConverter.ToUInt16(tmp2bytes, 0);
            //       [2]
            byte stream    = (byte)(header[2] & 0x7F); //0111 1111
            bool needReply = (0x80 == (byte)(header[2] & 0x80));
            //          [3]
            byte function  = header[3];
            bool isPrimary = (function % 2 == 1);

            //             [4][5]
            Array.Copy(header, 4, tmp2bytes, 0, tmp2bytes.Length);
            bool isLastBlock = (0x80 == (tmp2bytes[0] & 0x80));

            Array.Reverse(tmp2bytes); //reverse for convert by BitConvertor
            tmp2bytes[0] = (byte)(tmp2bytes[0] & 0x7F);
            ushort blockNo = BitConverter.ToUInt16(tmp2bytes, 0);

            //                   [6][7][8][9]
            byte[] temp4Bytes = new byte[4];
            Array.Copy(header, 6, temp4Bytes, 0, temp4Bytes.Length);
            Array.Reverse(temp4Bytes);
            uint transId = BitConverter.ToUInt32(temp4Bytes, 0);

            SecsMessageBase msg = GetSecsMessageInstance(stream, function, needReply);

            if (data != null && data.Length > 10)
            {
                using (MemoryStream reader = new MemoryStream(data))
                {
                    reader.Position = header.Length;
                    msg.ReadItems(reader);
                }
            }

            msg.TransactionId = transId;
            msg.DeviceId      = deviceId;

            return(msg);
        }