示例#1
0
文件: I8583.cs 项目: a864610877/src
        /// <summary>
        /// 解包
        /// </summary>
        /// <param name="B_Msg">字节报文:11字节报文头+2字节消息类型+8字节位图+n数据元</param>
        public void UnPack8583(byte[] B_Msg)
        {
            if (string.IsNullOrEmpty(_messageType))
            {
                _messageType = GetMessageType(B_Msg);
                InitTab(_messageType);
            }
            //报文不合法
            if (B_Msg.Length < 21)
            {
                tab[0].data = Encoding.GetBytes("Error:Packed lenth<44");
                return;
            }
            Array.Copy(B_Msg, 0, this.TPDU, 0, 5);
            byte[] bitmap   = new byte[8];
            string S_BitMap = "";
            int    maplen   = 8;

            for (int i = 13; i < 21; i++) //取16位位图 8个字节
            {
                bitmap[i - 13] = B_Msg[i];
            }

            for (int i = 0; i < maplen; i++)
            {
                string T_BitMap = I8583.AcceptConvertString(System.Convert.ToString(bitmap[i]), 10, 2);
                S_BitMap = S_BitMap + T_BitMap;
            }

            //取MessageType 2个字节
            byte[] msgType = new byte[2];
            msgType[0] = B_Msg[11];
            msgType[1] = B_Msg[12];
            //_messageType = SendConvertString(msgType[0].ToString(), 10, 16).PadLeft(2, '0') + SendConvertString(msgType[1].ToString(), 10, 16).PadLeft(2, '0');

            //解读报文
            int n   = 0;
            int ptr = 21;

            foreach (char C_BitMap in S_BitMap)
            {
                string flag = Convert.ToString(C_BitMap);
                if (flag == "1")
                {
                    try
                    {
                        List <byte> data  = new List <byte>();
                        int         len   = 0;
                        int         m_len = 0;
                        switch (tab[n].dataType)
                        {
                        case DataType.BCD:
                            #region BCD
                            if (tab[n].len_var > 0)
                            {
                                //变长字段
                                if (tab[n].len_var == 2)
                                {
                                    //两位变长
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    if (tab[n].C)
                                    {
                                        len = len * 2;
                                    }
                                    ptr++;
                                }
                                else if (tab[n].len_var == 3)
                                {
                                    //三位变长
                                    //第一位长度
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    len = len * 100;
                                    ptr++;
                                    //第二位是具体的长度
                                    len = len + int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    if (tab[n].C)
                                    {
                                        len = len * 2;
                                    }
                                    ptr++;
                                }
                                m_len = len;
                                if (len % 2 == 1)
                                {
                                    //如果数据长度为奇数 需要补一位
                                    len = len / 2 + 1;
                                }
                                else
                                {
                                    len = len / 2;
                                }
                            }
                            else
                            {
                                //定长字段
                                m_len = tab[n].len;
                                len   = tab[n].len / 2;
                                if (tab[n].len % 2 == 1)
                                {
                                    len++;
                                }
                            }
                            for (int i = 0; i < len; i++)
                            {
                                var s = SendConvertString(B_Msg[ptr].ToString(), 10, 16).PadLeft(2, '0');
                                data.Add(Convert.ToByte(s[0]));
                                data.Add(Convert.ToByte(s[1]));
                                ptr++;
                            }
                            //去掉自动补充的位(‘0’)
                            data = data.Take(m_len).ToList();
                            this.settabx_data(n, data.ToArray());
                            #endregion
                            break;

                        case DataType.ASCII:
                            #region ASCII
                            if (tab[n].len_var > 0)
                            {
                                //变长字段
                                if (tab[n].len_var == 2)
                                {
                                    //两位变长
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    if (tab[n].C)
                                    {
                                        len = len * 2;
                                    }
                                    ptr++;
                                }
                                else if (tab[n].len_var == 3)
                                {
                                    //三位变长
                                    //第一位长度
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    len = len * 100;
                                    ptr++;
                                    //第二位是具体的长度
                                    len = len + int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    if (tab[n].C)
                                    {
                                        len = len * 2;
                                    }
                                    ptr++;
                                }
                                m_len = len;
                                ////变长字段
                                //if (tab[n].len_var == 2)
                                //{
                                //    //两位变长
                                //    len = int.Parse(SendConvertString(B_Msg[ptr++].ToString()+B_Msg[ptr].ToString(), 10, 16));
                                //    ptr++;
                                //}
                                //else if (tab[n].len_var == 3)
                                //{
                                //    //三位变长
                                //    //第一位是长度
                                //    len = int.Parse(SendConvertString(B_Msg[ptr++].ToString() +B_Msg[ptr++].ToString()+B_Msg[ptr].ToString(), 10, 16));
                                //    len = len * 100;
                                //    ptr++;
                                //    //第二位是具体的长度
                                //    len = len + int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                //    ptr++;
                                //}
                            }
                            else
                            {
                                len = tab[n].len;
                            }
                            for (int i = 0; i < len; i++)
                            {
                                //byte[] m_b = new byte[1];
                                //m_b[0] = B_Msg[ptr];
                                //data += System.Text.Encoding.Default.GetString(m_b);
                                data.Add(B_Msg[ptr]);
                                ptr++;
                            }
                            this.settabx_data(n, data.ToArray());
                            #endregion
                            break;

                        case DataType.BINARY:
                            #region BINARY
                            if (tab[n].len_var > 0)
                            {
                                //变长字段
                                if (tab[n].len_var == 2)
                                {
                                    //两位变长
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    ptr++;
                                }
                                else if (tab[n].len_var == 3)
                                {
                                    //三位变长
                                    //第一位是0x00
                                    len = int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    len = len * 100;
                                    ptr++;
                                    //第二位是具体的长度
                                    len = len + int.Parse(SendConvertString(B_Msg[ptr].ToString(), 10, 16));
                                    ptr++;
                                }
                                m_len = len;
                                if (len % 2 == 1)
                                {
                                    //如果数据长度为奇数 需要补一位
                                    len = len / 2 + 1;
                                }
                                else
                                {
                                    len = len / 2;
                                }
                            }
                            else
                            {
                                //定长字段
                                len = tab[n].len / 2;
                                if (tab[n].len % 2 == 1)
                                {
                                    len++;
                                }
                            }
                            for (int i = 0; i < len; i++)
                            {
                                //data += SendConvertString(B_Msg[ptr].ToString(), 10, 16).PadLeft(2, '0');

                                var s = SendConvertString(B_Msg[ptr].ToString(), 10, 16).PadLeft(2, '0');
                                data.Add(Convert.ToByte(s[0]));
                                data.Add(Convert.ToByte(s[1]));
                                ptr++;
                            }
                            if (m_len > 0)
                            {
                                //data = data.Substring(0, m_len);
                                data = data.Take(m_len).ToList();
                            }
                            this.settabx_data(n, data.ToArray());
                            #endregion
                            break;

                        default:
                            break;
                        }
                    }
                    catch
                    {
                        tab[0].data = Encoding.GetBytes("Error:Packed tab" + n.ToString());
                        return;
                    }
                }
                n++;
            }
        }
示例#2
0
文件: I8583.cs 项目: a864610877/src
        /// <summary>
        /// 计算报文长度
        /// 暂时不用 POS设备传过来的报文包含两个字节的长度
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int GetDatagramLength(byte[] data)
        {
            //报文头11字节+消息类型2字节+位图8字节
            int length = 21;

            I8583 c = new I8583(GetMessageType(data));

            byte[] bitmap   = new byte[8];
            string S_BitMap = "";
            int    maplen   = 8;

            for (int i = 13; i < 21; i++) //取16位位图
            {
                bitmap[i - 13] = data[i];
            }

            for (int i = 0; i < maplen; i++)
            {
                string T_BitMap = I8583.AcceptConvertString(System.Convert.ToString(bitmap[i]), 10, 2);
                S_BitMap = S_BitMap + T_BitMap;
            }

            int idx = 0;

            foreach (char bit in S_BitMap)
            {
                string tab = bit.ToString();
                int    len = 0;
                if (length > data.Length)
                {
                    return(-1);
                }

                if (tab == "1")
                {
                    switch (c.tab[idx].dataType)
                    {
                    case DataType.BCD:
                        #region BCD
                        if (c.tab[idx].len_var > 0)
                        {
                            if (c.tab[idx].len_var == 2)
                            {
                                //两位变长
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                            else if (c.tab[idx].len_var == 3)
                            {
                                //三位变长
                                length++;
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                            if (len % 2 == 1)
                            {
                                //如果数据长度为奇数 需要补一位
                                len = len / 2 + 1;
                            }
                            else
                            {
                                len = len / 2;
                            }
                        }
                        else
                        {
                            //定长字段
                            len = c.tab[idx].len / 2;
                            if (c.tab[idx].len % 2 == 1)
                            {
                                len++;
                            }
                        }
                        length += len;
                        #endregion
                        break;

                    case DataType.ASCII:
                        #region ASCII
                        if (c.tab[idx].len_var > 0)
                        {
                            if (c.tab[idx].len_var == 2)
                            {
                                //两位变长
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                            else if (c.tab[idx].len_var == 3)
                            {
                                //三位变长
                                length++;
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                        }
                        else
                        {
                            len = c.tab[idx].len;
                        }
                        length += len;
                        #endregion
                        break;

                    case DataType.BINARY:
                        #region BINARY
                        if (c.tab[idx].len_var > 0)
                        {
                            if (c.tab[idx].len_var == 2)
                            {
                                //两位变长
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                            else if (c.tab[idx].len_var == 3)
                            {
                                //三位变长
                                length++;
                                len = int.Parse(I8583.SendConvertString(data[length].ToString(), 10, 16));
                                length++;
                            }
                            if (len % 2 == 1)
                            {
                                //如果数据长度为奇数 需要补一位
                                len = len / 2 + 1;
                            }
                            else
                            {
                                len = len / 2;
                            }
                        }
                        else
                        {
                            //定长字段
                            len = c.tab[idx].len / 2;
                            if (c.tab[idx].len % 2 == 1)
                            {
                                len++;
                            }
                        }
                        length += len;
                        #endregion
                        break;

                    default:
                        break;
                    }
                }
                idx++;
            }

            return(length);
        }