Exemplo n.º 1
0
        internal static void InitTable(I8583 i8583, string messageType)
        {
            if (Current == null)
            {
                throw new Exception("please init I8583Initializer::Current first");
            }

            Current.InitTab(i8583, messageType);
        }
Exemplo n.º 2
0
        protected override void InitTab(I8583 i8583, string messageType)
        {
            for (int i = 0; i < 64; i++)
            {
                i8583.settabx(i, "0", "", 0, 0, 0);
            }
            i8583.settabx_len(1, 19, 2, DataType.BCD);
            i8583.settabx_len(2, 6, DataType.BCD);
            i8583.settabx_len(3, 12, DataType.BCD);
            i8583.settabx_len(10, 6, DataType.BCD);
            i8583.settabx_len(11, 6, DataType.BCD);
            i8583.settabx_len(12, 4, DataType.BCD);
            i8583.settabx_len(13, 4, DataType.BCD);
            i8583.settabx_len(14, 4, DataType.BCD);
            i8583.settabx_len(21, 3, DataType.BCD);
            i8583.settabx_len(22, 3, DataType.BCD);
            i8583.settabx_len(24, 2, DataType.BCD);
            i8583.settabx_len(25, 2, DataType.BCD);
            i8583.settabx_len(31, 11, 2, DataType.BCD, true);
            i8583.settabx_len(34, 37, 2, DataType.BCD);
            i8583.settabx_len(35, 104, 3, DataType.BCD);
            i8583.settabx_len(36, 12, DataType.ASCII);
            i8583.settabx_len(37, 6, DataType.ASCII);
            i8583.settabx_len(38, 2, DataType.ASCII);
            i8583.settabx_len(40, 8, DataType.ASCII);
            i8583.settabx_len(41, 15, DataType.ASCII);
            i8583.settabx_len(43, 25, 2, DataType.ASCII);
            i8583.settabx_len(47, 62, 3, DataType.ASCII);
            i8583.settabx_len(48, 3, DataType.ASCII);
            i8583.settabx_len(51, 16, DataType.BINARY);
            i8583.settabx_len(52, 16, DataType.BCD);
            i8583.settabx_len(53, 20, 3, DataType.ASCII);
            i8583.settabx_len(59, 22, 3, DataType.BCD);
            i8583.settabx_len(60, 29, 3, DataType.BCD);

            switch (messageType)
            {
            case "0800":
                i8583.settabx_len(61, 72, 3, DataType.BINARY, true);
                break;

            default:
                i8583.settabx_len(61, 60, 3, DataType.ASCII);
                break;
            }
            //i8583.settabx_len(61, 48, 3, DataType.BINARY, true);
            i8583.settabx_len(62, 63, 3, DataType.ASCII);
            i8583.settabx_len(63, 16, DataType.BINARY);
        }
Exemplo n.º 3
0
 public void CopyTo(I8583 i8583, params int[] idxs)
 {
     i8583.TPDU = this.TPDU;
     for (int i = 0; i < 64; i++)
     {
         if (Array.IndexOf(idxs, i) < 0)
         {
             continue;
         }
         if (this.Gettabx_flag(i) == "1")
         {
             i8583.settabx_data(i, this.tab[i].data.ToList().ToArray());
         }
     }
 }
Exemplo n.º 4
0
        public I8583 Clone()
        {
            I8583 i8583 = new I8583(_messageType);

            i8583.TPDU = new byte[this.TPDU.Length];
            Array.Copy(this.TPDU, i8583.TPDU, this.TPDU.Length);
            for (int i = 0; i < 64; i++)
            {
                if (this.Gettabx_flag(i) == "1")
                {
                    i8583.settabx_data(i, this.tab[i].data.ToList().ToArray());
                }
            }
            return(i8583);
        }
Exemplo n.º 5
0
        /// <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++;
            }
        }
Exemplo n.º 6
0
 protected abstract void InitTab(I8583 i8583, string messageType);
Exemplo n.º 7
0
        /// <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);
        }