Example #1
0
        private byte[] CreateRequest(String terminalNo, int messageType,
                                     Command cmd, byte[] data, int dataLen)
        {
            /*          General Message for Request
             *  XX      Length
             *  ASCII   Terminal Serial
             *  XX      Message Type
             *  BCD     SequenceNum
             *  BCD     Date
             *  BCD     Time
             *          [Detail]
             *  XX      LRC
             */

            Encoding encode = Encoding.GetEncoding(1254);

            List <byte> reqPacket = new List <byte>();
            List <byte> msgPacket = new List <byte>();
            int         allLen    = dataLen;

            //SEQUNCE
            reqPacket.AddRange(MessageBuilder.HexToByteArray(GMPCommonTags.TAG_SEQUNCE));
            reqPacket.Add((byte)GMPConstants.LEN_SEQUENCE);
            byte[] arrVal = MessageBuilder.ConvertIntToBCD(seqNum, GMPConstants.LEN_SEQUENCE);
            reqPacket.AddRange(arrVal);
            //DATE and TIME
            reqPacket.AddRange(MessageBuilder.GetDateTimeInBytes(DateTime.Now));
            // Fiscal Command
            if (cmd != Command.NULL && cmd != Command.RAW_MESSAGE)
            {
                reqPacket.AddRange(MessageBuilder.HexToByteArray(FPUCommonTags.FPU_FISCAL_COMMAND));
                reqPacket.Add((byte)GMPConstants.LEN_FISCAL_COMMAND);
                reqPacket.Add((byte)cmd);
            }

            if (dataLen > 0)
            {
                reqPacket.AddRange(data);
            }

            if (FiscalPrinter.TripleKey != null && messageType == REQUEST_MSG_ID)
            {
                // End of message Tag
                reqPacket.AddRange(MessageBuilder.HexToByteArray(FPUDataTags.ENDOFMSG));
                reqPacket.Add(1);
                reqPacket.Add(1);

                byte[] desData = MessageBuilder.EncryptTriple(reqPacket.ToArray(),
                                                              reqPacket.Count,
                                                              FiscalPrinter.TripleKey);
                if (desData.Length > 0)
                {
                    reqPacket.Clear();
                    //ENCRYPTION DATA
                    reqPacket.AddRange(desData);
                }
            }

            // Terminal ID
            msgPacket.AddRange(encode.GetBytes(terminalNo.PadLeft(12, ' ')));
            // Message TAG
            msgPacket.AddRange(MessageBuilder.HexToByteArray(messageType));
            // Message length
            int msgLen = reqPacket.Count;

            msgPacket.AddRange(MessageBuilder.AddLength(msgLen));
            // Message Data
            msgPacket.AddRange(reqPacket);
            // CRC
            msgPacket.Add(MessageBuilder.CalculateLRC(msgPacket));
            // All Length
            allLen = msgPacket.Count;
            msgPacket.Insert(0, (byte)(allLen % 256));
            msgPacket.Insert(0, (byte)(allLen / 256));

            return(msgPacket.ToArray());
        }