예제 #1
0
        public override async Task TransmitDataAsync(byte[] data, CancellationToken cancellationToken,
                                                     bool enableAck = true)
        {
            if (Address == null)
            {
                throw new InvalidOperationException("Can't send data to local device.");
            }

            if (_maxPayloadLength == null)
            {
                _maxPayloadLength = await GetMaximumTransmitPayloadLengthAsync();
            }

            var dataStream = new MemoryStream(data);

            int read;

            // ReSharper disable once PossibleInvalidOperationException
            byte[] block = new byte[_maxPayloadLength.Value];
            while ((read = dataStream.Read(block, 0, block.Length)) > 0)
            {
                var readBlock = new byte[read];
                Array.Copy(block, readBlock, readBlock.Length);
                var transmitRequest = new TxRequestExtFrame(Address.LongAddress, Address.ShortAddress, data);

                if (!enableAck)
                {
                    transmitRequest.Options = TransmitOptionsExt.DisableAck;
                    await Controller.ExecuteAsync(transmitRequest).ConfigureAwait(false);
                }
                else
                {
                    var response = await Controller
                                   .ExecuteQueryAsync <TxStatusExtFrame>(transmitRequest, cancellationToken)
                                   .ConfigureAwait(false);

                    if (response.DeliveryStatus != DeliveryStatusExt.Success)
                    {
                        // per documentation, set short address to unknown on failure
                        Address.ShortAddress = ShortAddress.Disabled;

                        throw new XBeeException($"Delivery failed with status code '{response.DeliveryStatus}'.");
                    }

                    // per documention, update short address
                    Address.ShortAddress = response.ShortAddress;
                }
            }
        }
예제 #2
0
파일: FrameTests.cs 프로젝트: splitice/XBee
        public void TxRequestFrameTest()
        {
            var txRequestFrame = new TxRequestExtFrame(new LongAddress(0x0013A200400A0127),
                                                       new byte[] { 0x54, 0x78, 0x44, 0x61, 0x74, 0x61, 0x30, 0x41 })
            {
                FrameId = 0x01
            };

            var expectedValue = new byte[]
            {
                0x7e, 0x00, 0x16, 0x10, 0x01, 0x00, 0x13, 0xA2,
                0x00, 0x40, 0x0A, 0x01, 0x27, 0xff, 0xfe, 0x00,
                0x00, 0x54, 0x78, 0x44, 0x61, 0x74, 0x61, 0x30,
                0x41, 0x13
            };

            Check(txRequestFrame, expectedValue);
        }
예제 #3
0
        public override async Task TransmitDataAsync(byte[] data, CancellationToken cancellationToken, bool enableAck = true)
        {
            if (Address == null)
            {
                throw new InvalidOperationException("Can't send data to local device.");
            }

            var transmitRequest = new TxRequestExtFrame(Address.LongAddress, data);

            if (!enableAck)
            {
                transmitRequest.Options = TransmitOptionsExt.DisableAck;
                await Controller.ExecuteAsync(transmitRequest, cancellationToken);
            }
            else
            {
                TxStatusExtFrame response = await Controller.ExecuteQueryAsync <TxStatusExtFrame>(transmitRequest, cancellationToken);

                if (response.DeliveryStatus != DeliveryStatusExt.Success)
                {
                    throw new XBeeException($"Delivery failed with status code '{response.DeliveryStatus}'.");
                }
            }
        }