Examples

Basic Usage Example

This example demonstrates basic usage of the C# bindings to read from an IIO device.

  1// SPDX-License-Identifier: MIT
  2/*
  3 * libiio - Library for interfacing industrial I/O (IIO) devices
  4 *
  5 * Copyright (C) 2014 Analog Devices, Inc.
  6 * Author: Paul Cercueil <paul.cercueil@analog.com>
  7 */
  8
  9using System;
 10using System.Collections.Generic;
 11using System.Linq;
 12using System.Text;
 13using System.Threading.Tasks;
 14using iio;
 15
 16namespace IIOCSharp
 17{
 18    class ExampleProgram
 19    {
 20        static void Main(string[] args)
 21        {
 22            const uint blocksize = 1024;
 23            Scan scan = new Scan();
 24            if (scan.nb_results > 0) {
 25                Console.WriteLine("* Scanned contexts: " + scan.nb_results);
 26            }
 27            foreach (var entry in scan.results) {
 28                Console.WriteLine("\t" + entry.Key + " " + entry.Value);
 29            }
 30            scan.Dispose();
 31
 32            Context ctx;
 33            try
 34            {
 35                ctx = new Context("ip:192.168.2.1");
 36                Console.WriteLine(ctx.xml);
 37            } catch (IIOException e)
 38            {
 39                Console.WriteLine(e.ToString());
 40                return;
 41            }
 42
 43            Console.WriteLine("* IIO context created: " + ctx.name);
 44            Console.WriteLine("* IIO context description: " + ctx.description);
 45
 46            Console.WriteLine("* IIO context has " + ctx.devices.Count + " devices:");
 47            foreach (Device dev in ctx.devices) {
 48                Console.WriteLine("\t" + dev.id + ": " + dev.name);
 49
 50                if (dev is Trigger)
 51                {
 52                    Console.WriteLine("* Found trigger! Rate=" + ((Trigger) dev).get_rate());
 53                }
 54
 55                Console.WriteLine("\t\t" + dev.channels.Count + " channels found:");
 56
 57                foreach (Channel chn in dev.channels)
 58                {
 59                    string type = "input";
 60                    if (chn.output)
 61                    {
 62                        type = "output";
 63                    }
 64                    Console.WriteLine("\t\t\t" + chn.id + ": " + chn.name + " (" + type + ")");
 65
 66                    if (chn.attrs.Count == 0)
 67                    {
 68                        continue;
 69                    }
 70
 71                    Console.WriteLine("\t\t\t" + chn.attrs.Count + " channel-specific attributes found:");
 72                    foreach (Attr attr in chn.attrs.Values)
 73                    {
 74                        string attr_name = "\t\t\t\t" + attr.name;
 75                        string attr_val = "";
 76                        try
 77                        {
 78                            if (attr.name == "calibscale")
 79                            {
 80                                double val = attr.read_double();
 81                                Console.WriteLine(attr_name + " " + val);
 82                            }
 83                            else
 84                            {
 85                                attr_val = attr.read();
 86                                Console.WriteLine(attr_name + " " + attr_val);
 87                            }
 88                        }
 89                        catch (IIOException)
 90                        {
 91                            Console.WriteLine(attr_name);
 92                        }
 93                    }
 94                }
 95
 96				/* If we find cf-ad9361-lpc, try to read a few bytes from the first channel */
 97                if (dev.name.CompareTo("cf-ad9361-lpc") == 0)
 98                {
 99                    ChannelsMask chnmask = new ChannelsMask((uint)dev.channels.Count);
100                    Channel rx0_i = dev.channels[0];
101                    rx0_i.enable(chnmask);
102                    Channel rx0_q = dev.channels[1];
103                    rx0_q.enable(chnmask);
104
105                    IOBuffer buf = dev.buffers[0];
106                    uint sampleSize = dev.get_sample_size(chnmask);
107                    Console.WriteLine("* Sample size is " + sampleSize + "\n");
108
109                    Console.WriteLine("\t\t\t" + buf.attrs.Count + " buffer-specific attributes found:");
110                    foreach (Attr attr in buf.attrs.Values)
111                    {
112                        Console.WriteLine("\t\t\t\t" + attr.name + " " + attr.read());
113                    }
114
115                    iio.Stream stream = new iio.Stream(buf, chnmask, 4, blocksize);
116                    for (int i=0; i < 10; i++) {
117                        Block block = stream.next();
118                        byte[] databuf = new byte[blocksize];
119                        block.read(databuf);
120                        Console.WriteLine("* ("+ i + ") Read " + databuf.Length + " bytes from hardware");
121                    }
122
123                    stream.Dispose();
124                    chnmask.Dispose();
125                }
126
127                if (dev.attrs.Count == 0)
128                {
129                    continue;
130                }
131
132                Console.WriteLine("\n\t\t" + dev.attrs.Count + " device-specific attributes found:");
133                foreach (Attr attr in dev.attrs.Values)
134                {
135                    Console.WriteLine("\t\t\t" + attr.name);
136                }
137
138            }
139        }
140    }
141}

Event Handling Example

This example shows how to handle IIO events using the C# bindings.

 1// SPDX-License-Identifier: MIT
 2/*
 3 * libiio - Library for interfacing industrial I/O (IIO) devices
 4 *
 5 * Copyright (C) 2024 Analog Devices, Inc.
 6 * Author: Alexandra Trifan <Alexandra.Trifan@analog.com>
 7 */
 8
 9using System;
10using System.Collections.Generic;
11using System.Linq;
12using System.Text;
13using System.Threading.Tasks;
14using iio;
15
16namespace IIOCSharp
17{
18    class ExampleIIOEvent
19    {
20        static void Main(string[] args)
21        {
22            Scan scan = new Scan();
23            if (scan.nb_results > 0) {
24                Console.WriteLine("* Scanned contexts: " + scan.nb_results);
25            }
26            foreach (var entry in scan.results) {
27                Console.WriteLine("\t" + entry.Key + " " + entry.Value);
28            }
29            scan.Dispose();
30
31            if (args.Length <= 1)
32            {
33                Console.WriteLine("Please provide the context URI and IIO device.\n");
34                return;
35            }
36
37            string uri = args[0];
38            string ev_device = args[1];
39            Context ctx;
40            try
41            {
42                ctx = new Context(uri);
43            } catch (IIOException e)
44            {
45                Console.WriteLine(e.ToString());
46                return;
47            }
48
49            Console.WriteLine("* IIO context created: " + ctx.name);
50            Console.WriteLine("* IIO context description: " + ctx.description);
51
52            Console.WriteLine("* IIO context has " + ctx.devices.Count + " devices:");
53            foreach (Device dev in ctx.devices) {
54                Console.WriteLine("\t" + dev.id + ": " + dev.name);
55                if (dev.name == ev_device)
56                {
57                    EventStream event_stream = new EventStream(dev);
58                    while (true)
59                    {
60                        try 
61                        {
62                            IIOEvent ev = event_stream.read_event(false);
63                            string timestamp = ev.timestamp.ToString();
64                            string print_ev = "Event: time: " + timestamp;
65                            if (ev.chn != null)
66                            {
67                                print_ev += ", channel(s): " + ev.chn.id.ToString();
68                            }
69                            if (ev.chn_diff != null)
70                            {
71                                print_ev += " - " + ev.chn_diff.id;
72                            }
73                            print_ev += ", evtype: " + ev.type;
74                            print_ev += ", direction: " + ev.direction;
75                            Console.WriteLine(print_ev);
76                        }
77                        catch (IIOException e)
78                        {
79                            Console.WriteLine("Error: " + e.ToString());
80                            event_stream.Dispose();
81                            return;
82                        }
83                    }
84                    event_stream.Dispose();
85                }
86            }
87        }
88    }
89}