Exemple #1
0
        private IFiscalResponse _SendMessage(IWrappedMessage msg, Func <byte[], IFiscalResponse> responseFactory)
        {
            IFiscalResponse response = null;

            byte[] lastStatusBytes = null;
            var    packet          = msg.GetBytes(_sequence);

            for (var r = 0; r < 3; r++)
            {
                try
                {
                    _port.Write(packet, 0, packet.Length);
                    var list = new List <byte>();

                    while (ReadByte())
                    {
                        var b = _queue.Dequeue();
                        if (b == 22)
                        {
                            continue;
                        }
                        if (b == 21)
                        {
                            throw new IOException("Invalid packet checksum or form of messsage.");
                        }
                        list.Add(b);
                    }

                    list.Add(_queue.Dequeue());
                    var buffer = list.ToArray();
                    response        = responseFactory(buffer);
                    lastStatusBytes = list.Skip(list.IndexOf(0x04) + 1).Take(6).ToArray();
                    break;
                }
                catch (Exception)
                {
                    if (r >= 2)
                    {
                        throw;
                    }
                }
            }
            _sequence += 1;
            if (_sequence > 254)
            {
                _sequence = 32;
            }
            if (msg.Command != 74)
            {
                checkStatusOnErrors(lastStatusBytes);
            }
            return(response);
        }
Exemple #2
0
        public IFiscalResponse SendMessage(IWrappedMessage msg, Func <byte[], IFiscalResponse> responseFactory, int sequence)
        {
            IFiscalResponse response = null;

            byte[] lastStatusBytes = null;
            var    packet          = msg.GetBytes(sequence);

            for (var r = 0; r < 3; r++)
            {
                try
                {
                    _serialPort.Write(packet, 0, packet.Length);
                    var list = new List <byte>();

                    while (ReadByte())
                    {
                        var b = _queue.Dequeue();
                        if (b == 22)
                        {
                            continue;
                        }
                        if (b == 21)
                        {
                            throw new IOException("Invalid packet checksum or form of messsage.");
                        }
                        list.Add(b);
                    }

                    list.Add(_queue.Dequeue());
                    var buffer = list.ToArray();
                    response        = responseFactory(buffer);
                    lastStatusBytes = list.Skip(list.IndexOf(0x04) + 1).Take(6).ToArray();
                    break;
                }
                catch (Exception)
                {
                    if (r >= 2)
                    {
                        throw;
                    }
                    _queue.Clear();
                }
            }
            if (msg.Command != 74)
            {
                Helpers.ThrowErrorOnBadStatus.Check(lastStatusBytes);
            }
            return(response);
        }