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
24 if (args.Length < 1)
25 {
26 Console.WriteLine("Scanning for available contexts...");
27 Scan scan = new Scan();
28 if (scan.nb_results > 0) {
29 Console.WriteLine("* Scanned contexts: " + scan.nb_results);
30 }
31 foreach (var entry in scan.results) {
32 Console.WriteLine("\t" + entry.Key + " " + entry.Value);
33 }
34 scan.Dispose();
35
36 Console.WriteLine("\nPlease provide the context URI.\n");
37 Console.WriteLine("Usage: ExampleProgram <uri>");
38 Console.WriteLine("Example: ExampleProgram ip:192.168.2.1\n");
39 return;
40 }
41
42 string uri = args[0];
43 Context ctx;
44 try
45 {
46 ctx = new Context(uri);
47 Console.WriteLine(ctx.xml);
48 } catch (IIOException e)
49 {
50 Console.WriteLine(e.ToString());
51 return;
52 }
53
54 Console.WriteLine("* IIO context created: " + ctx.name);
55 Console.WriteLine("* IIO context description: " + ctx.description);
56
57 Console.WriteLine("* IIO context has " + ctx.devices.Count + " devices:");
58 foreach (Device dev in ctx.devices) {
59 Console.WriteLine("\t" + dev.id + ": " + dev.name);
60
61 if (dev is Trigger)
62 {
63 Console.WriteLine("* Found trigger! Rate=" + ((Trigger) dev).get_rate());
64 }
65
66 Console.WriteLine("\t\t" + dev.channels.Count + " channels found:");
67
68 foreach (Channel chn in dev.channels)
69 {
70 string type = "input";
71 if (chn.output)
72 {
73 type = "output";
74 }
75 Console.WriteLine("\t\t\t" + chn.id + ": " + chn.name + " (" + type + ")");
76
77 if (chn.attrs.Count == 0)
78 {
79 continue;
80 }
81
82 Console.WriteLine("\t\t\t" + chn.attrs.Count + " channel-specific attributes found:");
83 foreach (Attr attr in chn.attrs.Values)
84 {
85 string attr_name = "\t\t\t\t" + attr.name;
86 string attr_val = "";
87 try
88 {
89 if (attr.name == "calibscale")
90 {
91 double val = attr.read_double();
92 Console.WriteLine(attr_name + " " + val);
93 }
94 else
95 {
96 attr_val = attr.read();
97 Console.WriteLine(attr_name + " " + attr_val);
98 }
99 }
100 catch (IIOException)
101 {
102 Console.WriteLine(attr_name);
103 }
104 }
105 }
106
107 /* If we find cf-ad9361-lpc, try to read a few bytes from the first channel */
108 if (dev.name.CompareTo("cf-ad9361-lpc") == 0)
109 {
110 ChannelsMask chnmask = new ChannelsMask((uint)dev.channels.Count);
111 Channel rx0_i = dev.channels[0];
112 rx0_i.enable(chnmask);
113 Channel rx0_q = dev.channels[1];
114 rx0_q.enable(chnmask);
115
116 IOBuffer buf = dev.buffers[0];
117 uint sampleSize = dev.get_sample_size(chnmask);
118 Console.WriteLine("* Sample size is " + sampleSize + "\n");
119
120 Console.WriteLine("\t\t\t" + buf.attrs.Count + " buffer-specific attributes found:");
121 foreach (Attr attr in buf.attrs.Values)
122 {
123 Console.WriteLine("\t\t\t\t" + attr.name + " " + attr.read());
124 }
125
126 iio.Stream stream = new iio.Stream(buf, chnmask, 4, blocksize);
127 for (int i=0; i < 10; i++) {
128 Block block = stream.next();
129 byte[] databuf = new byte[blocksize];
130 block.read(databuf);
131 Console.WriteLine("* ("+ i + ") Read " + databuf.Length + " bytes from hardware");
132 }
133
134 stream.Dispose();
135 chnmask.Dispose();
136 }
137
138 if (dev.attrs.Count == 0)
139 {
140 continue;
141 }
142
143 Console.WriteLine("\n\t\t" + dev.attrs.Count + " device-specific attributes found:");
144 foreach (Attr attr in dev.attrs.Values)
145 {
146 Console.WriteLine("\t\t\t" + attr.name);
147 }
148
149 }
150 }
151 }
152}
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 if (args.Length < 2)
23 {
24 if (args.Length == 0)
25 {
26 Console.WriteLine("Scanning for available contexts...");
27 Scan scan = new Scan();
28 if (scan.nb_results > 0) {
29 Console.WriteLine("* Scanned contexts: " + scan.nb_results);
30 }
31 foreach (var entry in scan.results) {
32 Console.WriteLine("\t" + entry.Key + " " + entry.Value);
33 }
34 scan.Dispose();
35 Console.WriteLine();
36 }
37
38 Console.WriteLine("Please provide the context URI and IIO device.\n");
39 Console.WriteLine("Usage: ExampleIIOEvent <uri> <device>");
40 Console.WriteLine("Example: ExampleIIOEvent ip:192.168.2.1 iio:device0\n");
41 return;
42 }
43
44 string uri = args[0];
45 string ev_device = args[1];
46 Context ctx;
47 try
48 {
49 ctx = new Context(uri);
50 } catch (IIOException e)
51 {
52 Console.WriteLine(e.ToString());
53 return;
54 }
55
56 Console.WriteLine("* IIO context created: " + ctx.name);
57 Console.WriteLine("* IIO context description: " + ctx.description);
58
59 Console.WriteLine("* IIO context has " + ctx.devices.Count + " devices:");
60 foreach (Device dev in ctx.devices) {
61 Console.WriteLine("\t" + dev.id + ": " + dev.name);
62 if (dev.name == ev_device)
63 {
64 EventStream event_stream = new EventStream(dev);
65 while (true)
66 {
67 try
68 {
69 IIOEvent ev = event_stream.read_event(false);
70 string timestamp = ev.timestamp.ToString();
71 string print_ev = "Event: time: " + timestamp;
72 if (ev.chn != null)
73 {
74 print_ev += ", channel(s): " + ev.chn.id.ToString();
75 }
76 if (ev.chn_diff != null)
77 {
78 print_ev += " - " + ev.chn_diff.id;
79 }
80 print_ev += ", evtype: " + ev.type;
81 print_ev += ", direction: " + ev.direction;
82 Console.WriteLine(print_ev);
83 }
84 catch (IIOException e)
85 {
86 Console.WriteLine("Error: " + e.ToString());
87 event_stream.Dispose();
88 return;
89 }
90 }
91 }
92 }
93 }
94 }
95}