0

I am trying to read one sample from a sensor using libiio, but for some reason I always get the same sample unless I restart the application.

Here is a minimal example

#include <stdio.h>
#include <iio.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>


/* Global objects */
static struct iio_buffer *device_buffer = NULL;
static struct iio_channel *channel = NULL;
static struct iio_context *local_ctx = NULL;
static struct iio_device *dev = NULL;
static const char *dev_name = NULL;
static const char *channel_id = NULL;
static uint8_t *in_buf;


int main()
{

        /* Getting local iio device context */
        local_ctx = iio_create_local_context();
        dev = iio_context_get_device(local_ctx, 0);
        dev_name = iio_device_get_name(dev);
        channel = iio_device_get_channel(dev, 0);

        iio_channel_enable(channel);
        if (iio_channel_is_enabled(channel))
                return -1;

        uint32_t sample_size = 3;
        uint32_t buffer_length = 1;

        struct iio_buffer *device_buffer =
                iio_device_create_buffer(dev, buffer_length, false);

        if (!device_buffer)
                return -1;

        while (true) {
                ssize_t nbytes_rx = iio_buffer_refill(device_buffer);

                if (nbytes_rx <= 0) {
                        printf("Error refilling buf %d\n", (int) nbytes_rx);
                        return -1;
                }

                in_buf = (uint8_t*)malloc(sample_size * buffer_length);
                iio_channel_read(channel, device_buffer, in_buf, sample_size * buffer_length);
                printf("%" PRIi32 "\n", ((int32_t *)in_buf));
                free(in_buf);

        }
}

In the actual code I am properly error checking, handling SIGINT and destroying all the buffers. Everything works. The only issue is that the same sample is printed over and over, whereas I would assume the buffer to get refilled and a new sample to get printed.

Marcus Müller
  • 21,602
  • 2
  • 39
  • 54
neolith
  • 213
  • 2
  • 7
  • Are you actually printing the sample or just the address of the sample? – Marcus Müller Dec 27 '22 at 22:23
  • Not sure. I have done this after the reference examples on libiio page https://analogdevicesinc.github.io/libiio/v0.24/libiio/dummy-iiostream_8c-example.html. It is true that the in_buf is not dereferenced. If I do, I always get 0. – neolith Dec 28 '22 at 10:07

0 Answers0