Example #1
0
        /// <summary>
        /// Xmode数据包初始化
        /// </summary>
        /// <param name="startHeader">起始字符,决定Xmode类型</param>
        /// <param name="packetNum">包序号</param>
        /// <param name="validData">有效数据,不足长度将自动填充,超过部分丢弃</param>
        /// <param name="len">有效数据数据长度</param>
        public XmodePacket(XmodeStartHeader startHeader, byte packetNum, byte[] validData, int len)
        {
            try
            {
                if (validData.Length < len)
                {
                    throw new Exception("XmodePacket:有效数据实际长度小于所定义长度");
                }


                if (XmodeStartHeader.SOH == startHeader)
                {
                    validDataLen = 128;
                }
                else
                {
                    validDataLen = 1024;
                }

                packetData = new byte[PacketLen];

                packetData[0] = (byte)startHeader;
                packetData[1] = packetNum;
                packetData[2] = (byte)(packetNum ^ 0xFF); //取反

                if (len >= validDataLen)                  //大于等于则直接填充有效长度
                {
                    Array.Copy(validData, 0, packetData, 3, validDataLen);
                }
                else
                {
                    //小于部分填充数据包
                    if (len == 0)
                    {
                        throw new Exception("XmodePacket:有效数据长度不能为0");
                    }
                    Array.Copy(validData, 0, packetData, 3, len);

                    for (int i = len; i < validDataLen; i++)
                    {
                        packetData[3 + i] = (byte)XmodeDefine.EOF;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        /// <summary>
        /// 数据包管理器初始化
        /// </summary>
        /// <param name="data">需要打包的数据</param>
        /// <param name="len">数据长度</param>
        /// <param name="startHeader">开始字符</param>
        public XmodePacketManager(byte[] data, int len, XmodeStartHeader startHeader)
        {
            int validDataLen = 128;

            if (XmodeStartHeader.SOH == startHeader)
            {
                validDataLen = 128;
            }
            else if (XmodeStartHeader.STX == startHeader)
            {
                validDataLen = 1024;
            }
            else
            {
                throw new Exception("XmodePacketManager:错误头长度");
            }



            if (data.Length < len)
            {
                throw new Exception("XmodePacketManager:数据长度小于所需长度");
            }

            if (len > 255 * validDataLen)
            {
                throw new Exception("XmodePacketManager:数据长度超过一次传输的最大长度");
            }
            packetList = new List <XmodePacket>();

            int fullLen   = len / validDataLen;
            int remainLen = len % validDataLen;

            for (int i = 0; i < fullLen; i++)
            {
                var array = new byte[validDataLen];
                Array.Copy(data, i * validDataLen, array, 0, validDataLen);
                var packet = new XmodePacket(startHeader, (byte)(i + 1), data, validDataLen);
                packetList.Add(packet);
            }
            if (remainLen != 0)
            {
                var dataArray = new byte[remainLen];
                Array.Copy(data, fullLen * validDataLen, dataArray, 0, remainLen);
                var packet = new XmodePacket(startHeader, (byte)(fullLen + 1), data, remainLen);
                packetList.Add(packet);
            }
        }