Exemple #1
0
        public byte[] Send(IDeviceMessage message)
        {
            byte[] buffer = message.GetSendBuffer();

            Connect();
            try {
                for (int i = 0; i < 3; i++)
                {
                    _stream.WriteAsync(buffer, 0, buffer.Length).Wait();

                    var rvalue = _stream.GetTerminalResponseAsync();
                    if (rvalue != null)
                    {
                        byte lrc = rvalue[rvalue.Length - 1]; // Should be the LRC
                        if (lrc != TerminalUtilities.CalculateLRC(rvalue))
                        {
                            SendControlCode(ControlCodes.NAK);
                        }
                        else
                        {
                            SendControlCode(ControlCodes.ACK);
                            return(rvalue);
                        }
                    }
                }
                throw new MessageException("Terminal did not respond in the given timeout.");
            }
            catch (Exception exc) {
                throw new MessageException(exc.Message, exc);
            }
            finally {
                OnMessageSent?.Invoke(message.ToString());
                Disconnect();
            }
        }
        public byte[] Send(IDeviceMessage message)
        {
            byte[] buffer = message.GetSendBuffer();

            var readyReceived = false;

            byte[] responseMessage = null;

            Connect();
            try {
                var task = _stream.WriteAsync(buffer, 0, buffer.Length);

                if (!task.Wait(_settings.Timeout))
                {
                    throw new MessageException("Terminal did not respond in the given timeout.");
                }

                do
                {
                    var rvalue = _stream.GetTerminalResponseAsync();
                    if (rvalue != null)
                    {
                        var msgValue = GetResponseMessageType(rvalue);

                        switch (msgValue)
                        {
                        case UpaMessageType.Ack:
                            break;

                        case UpaMessageType.Nak:
                            break;

                        case UpaMessageType.Ready:
                            readyReceived = true;
                            break;

                        case UpaMessageType.Busy:
                            break;

                        case UpaMessageType.TimeOut:
                            break;

                        case UpaMessageType.Msg:
                            responseMessage = TrimResponse(rvalue);
                            if (IsNonReadyResponse(responseMessage))
                            {
                                readyReceived = true;     // since reboot doesn't return READY
                            }
                            SendAckMessageToDevice();
                            break;

                        default:
                            throw new Exception("Message field value is unknown in API response.");
                        }
                    }
                    else
                    {
                        // Reset the connection before the next attempt
                        Disconnect();
                        Connect();
                    }
                } while (!readyReceived);

                return(responseMessage);
            }
            catch (Exception exc) {
                throw new MessageException(exc.Message, exc);
            }
            finally {
                OnMessageSent?.Invoke(message.ToString());
                Disconnect();
            }
        }