Examples
Basic Usage Example
This example demonstrates basic usage of the C# bindings to read from an IIO device.
1/*
2 * Copyright (C) 2014 Analog Devices, Inc.
3 * Author: Paul Cercueil <paul.cercueil@analog.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * */
19
20using System;
21using System.Collections.Generic;
22using System.Linq;
23using System.Text;
24using System.Threading.Tasks;
25using iio;
26
27namespace IIOCSharp
28{
29 class ExampleProgram
30 {
31 static void Main(string[] args)
32 {
33 const uint blocksize = 1024;
34 Scan scan = new Scan();
35 if (scan.nb_results > 0) {
36 Console.WriteLine("* Scanned contexts: " + scan.nb_results);
37 }
38 foreach (var entry in scan.results) {
39 Console.WriteLine("\t" + entry.Key + " " + entry.Value);
40 }
41 scan.Dispose();
42
43 Context ctx;
44 try
45 {
46 ctx = new Context("ip:192.168.2.1");
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/*
2 * Copyright (C) 2024 Analog Devices, Inc.
3 * Author: Alexandra Trifan <Alexandra.Trifan@analog.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * */
19
20using System;
21using System.Collections.Generic;
22using System.Linq;
23using System.Text;
24using System.Threading.Tasks;
25using iio;
26
27namespace IIOCSharp
28{
29 class ExampleIIOEvent
30 {
31 static void Main(string[] args)
32 {
33 Scan scan = new Scan();
34 if (scan.nb_results > 0) {
35 Console.WriteLine("* Scanned contexts: " + scan.nb_results);
36 }
37 foreach (var entry in scan.results) {
38 Console.WriteLine("\t" + entry.Key + " " + entry.Value);
39 }
40 scan.Dispose();
41
42 if (args.Length <= 1)
43 {
44 Console.WriteLine("Please provide the context URI and IIO device.\n");
45 return;
46 }
47
48 string uri = args[0];
49 string ev_device = args[1];
50 Context ctx;
51 try
52 {
53 ctx = new Context(uri);
54 } catch (IIOException e)
55 {
56 Console.WriteLine(e.ToString());
57 return;
58 }
59
60 Console.WriteLine("* IIO context created: " + ctx.name);
61 Console.WriteLine("* IIO context description: " + ctx.description);
62
63 Console.WriteLine("* IIO context has " + ctx.devices.Count + " devices:");
64 foreach (Device dev in ctx.devices) {
65 Console.WriteLine("\t" + dev.id + ": " + dev.name);
66 if (dev.name == ev_device)
67 {
68 EventStream event_stream = new EventStream(dev);
69 while (true)
70 {
71 try
72 {
73 IIOEvent ev = event_stream.read_event(false);
74 string timestamp = ev.timestamp.ToString();
75 string print_ev = "Event: time: " + timestamp;
76 if (ev.chn != null)
77 {
78 print_ev += ", channel(s): " + ev.chn.id.ToString();
79 }
80 if (ev.chn_diff != null)
81 {
82 print_ev += " - " + ev.chn_diff.id;
83 }
84 print_ev += ", evtype: " + ev.type;
85 print_ev += ", direction: " + ev.direction;
86 Console.WriteLine(print_ev);
87 }
88 catch (IIOException e)
89 {
90 Console.WriteLine("Error: " + e.ToString());
91 event_stream.Dispose();
92 return;
93 }
94 }
95 event_stream.Dispose();
96 }
97 }
98 }
99 }
100}