C# bindings for libiio: ExampleProgram.cs

C# bindings for libiio 1.0 C# bindings for libIIO

Loading...
Searching...
No Matches
ExampleProgram.cs

Small example program

/*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iio;
namespace IIOCSharp
{
class ExampleProgram
{
static void Main(string[] args)
{
const uint blocksize = 1024;
Scan scan = new Scan();
if (scan. nb_results > 0) {
Console.WriteLine("* Scanned contexts: " + scan.nb_results);
}
foreach (var entry in scan.results) {
Console.WriteLine("\t" + entry.Key + " " + entry.Value);
}
scan.Dispose();
Context ctx;
try
{
ctx = new Context("ip:192.168.2.1");
Console.WriteLine(ctx. xml);
} catch (IIOException e)
{
Console.WriteLine(e.ToString());
return;
}
Console.WriteLine("* IIO context created: " + ctx. name);
Console.WriteLine("* IIO context description: " + ctx. description);
Console.WriteLine("* IIO context has " + ctx. devices.Count + " devices:");
foreach (Device dev in ctx.devices) {
Console.WriteLine("\t" + dev. id + ": " + dev. name);
if (dev is Trigger)
{
Console.WriteLine("* Found trigger! Rate=" + ((Trigger) dev).get_rate());
}
Console.WriteLine("\t\t" + dev. channels.Count + " channels found:");
foreach (Channel chn in dev.channels)
{
string type = "input";
if (chn. output)
{
type = "output";
}
Console.WriteLine("\t\t\t" + chn. id + ": " + chn. name + " (" + type + ")");
if (chn. attrs.Count == 0)
{
continue;
}
Console.WriteLine("\t\t\t" + chn.attrs.Count + " channel-specific attributes found:");
foreach (Attr attr in chn.attrs)
{
string attr_name = "\t\t\t\t" + attr. name;
string attr_val = "";
try
{
if (attr.name == "calibscale")
{
double val = attr. read_double();
Console.WriteLine(attr_name + " " + val);
}
else
{
attr_val = attr. read();
Console.WriteLine(attr_name + " " + attr_val);
}
}
catch (IIOException)
{
Console.WriteLine(attr_name);
}
}
}
/* If we find cf-ad9361-lpc, try to read a few bytes from the first channel */
if (dev.name.CompareTo("cf-ad9361-lpc") == 0)
{
ChannelsMask chnmask = new ChannelsMask((uint)dev.channels.Count);
Channel rx0_i = dev.channels[0];
rx0_i. enable(chnmask);
Channel rx0_q = dev.channels[1];
rx0_q.enable(chnmask);
IOBuffer buf = dev. buffers[0];
uint sampleSize = dev. get_sample_size(chnmask);
Console.WriteLine("* Sample size is " + sampleSize + "\n");
Console.WriteLine("\t\t\t" + buf. attrs.Count + " buffer-specific attributes found:");
foreach (Attr attr in buf.attrs)
{
Console.WriteLine("\t\t\t\t" + attr.name + " " + attr.read());
}
iio.Stream stream = new iio.Stream(buf, chnmask, 4, blocksize);
for (int i=0; i < 10; i++) {
Block block = stream.next();
byte[] databuf = new byte[blocksize];
block.read(databuf);
Console.WriteLine("* ("+ i + ") Read " + databuf.Length + " bytes from hardware");
}
stream.Dispose();
chnmask.Dispose();
}
if (dev. attrs.Count == 0)
{
continue;
}
Console.WriteLine("\n\t\t" + dev.attrs.Count + " device-specific attributes found:");
foreach (Attr attr in dev.attrs)
{
Console.WriteLine("\t\t\t" + attr.name);
}
}
}
}
}
iio.Attr
iio.Attr class: Contains the representation of an iio_attr.
Definition Attr.cs:22
iio.Attr.read
string read()
Read the value of this attribute as a string.
Definition Attr.cs:57
iio.Attr.read_double
double read_double()
Read the value of this attribute as a double.
Definition Attr.cs:103
iio.Attr.name
readonly string name
The name of this attribute.
Definition Attr.cs:43
iio.Channel
iio.Channel class: Contains the representation of an input or output channel.
Definition Channel.cs:22
iio.Channel.name
readonly string name
The name of this channel.
Definition Channel.cs:226
iio.Channel.attrs
readonly List< Attr > attrs
A list of all the attributes that this channel has.
Definition Channel.cs:250
iio.Channel.enable
void enable(ChannelsMask mask)
Enable the current channel, so that it can be used for I/O operations.
Definition Channel.cs:298
iio.Channel.output
readonly bool output
Contains true if the channel is an output channel, false otherwise.
Definition Channel.cs:241
iio.Channel.id
readonly string id
An identifier of this channel.
Definition Channel.cs:234
iio.Context
iio.Context class: Contains the representation of an IIO context.
Definition Context.cs:56
iio.Context.name
readonly string name
The name of the current context.
Definition Context.cs:100
iio.Context.xml
readonly string xml
A XML representation of the current context.
Definition Context.cs:97
iio.Context.description
readonly string description
Retrieve a human-readable information string about the current context.
Definition Context.cs:103
iio.Context.devices
readonly List< Device > devices
A List of all the IIO devices present on the current context.
Definition Context.cs:108
iio.Device
iio.Device class: Contains the representation of an IIO device.
Definition Device.cs:21
iio.Device.channels
readonly List< Channel > channels
A list of all the iio.Channel objects that this device possesses.
Definition Device.cs:95
iio.Device.id
readonly string id
An identifier of this device.
Definition Device.cs:77
iio.Device.name
readonly string name
The name of this device.
Definition Device.cs:80
iio.Device.get_sample_size
uint get_sample_size(ChannelsMask mask)
Get the current sample size of the device.
Definition Device.cs:206
iio.Device.attrs
readonly List< Attr > attrs
A list of all the attributes that this device has.
Definition Device.cs:89
iio.Device.buffers
readonly List< IOBuffer > buffers
A list of all the iio.IOBuffer objects that this device possesses.
Definition Device.cs:98
iio.IOBuffer
iio.IOBuffer class: The class used for all I/O operations.
Definition IOBuffer.cs:18
iio.IOBuffer.attrs
readonly List< Attr > attrs
A list of all the attributes that this buffer has.
Definition IOBuffer.cs:33
iio.Scan
iio.ScanContext class: Class for getting information about the available contexts.
Definition Scan.cs:19
iio.Scan.nb_results
readonly uint nb_results
The number of contexts scanned.
Definition Scan.cs:48
iio.Stream
Definition Stream.cs:15
iio.Trigger
iio.Trigger class: Contains the representation of an IIO device that can act as a trigger.
Definition Trigger.cs:21
iio
Definition Attr.cs:18