/// <summary> /// Write the specified array of samples corresponding to this channel into the /// given <see cref="iio.IOBuffer"/> object.</summary> /// <param name="buffer">A valid instance of the <see cref="iio.IOBuffer"/> class.</param> /// <param name="array">A <c>byte</c> array containing the samples to write.</param> /// <param name="raw">If set to <c>true</c>, the samples are not converted from their /// host format to their native format.</param> /// <returns>The number of bytes written.</returns> /// <exception cref="System.Exception">The samples could not be written.</exception> public uint write(IOBuffer buffer, byte[] array, bool raw = false) { if (!is_enabled()) throw new Exception("Channel must be enabled before the IOBuffer is instantiated"); if (!this.output) throw new Exception("Unable to write to an input channel"); GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned); IntPtr addr = handle.AddrOfPinnedObject(); uint count; if (raw) count = iio_channel_write_raw(this.chn, buffer.buf, addr, (uint) array.Length); else count = iio_channel_write(this.chn, buffer.buf, addr, (uint) array.Length); handle.Free(); return count; }
static void Main(string[] args) { Context ctx = new Context("10.44.2.241"); if (ctx == null) { Console.WriteLine("Unable to create IIO context"); 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) { Console.WriteLine("\t\t\t\t" + attr.name); if (attr.name.CompareTo("frequency") == 0) { Console.WriteLine("Attribute content: " + attr.read()); } } } /* If we find cf-ad9361-lpc, try to read a few bytes from the first channel */ if (dev.name.CompareTo("cf-ad9361-lpc") == 0) { Channel chn = dev.channels[0]; chn.enable(); IOBuffer buf = new IOBuffer(dev, 0x8000); buf.refill(); Console.WriteLine("Read " + chn.read(buf).Length + " bytes from hardware"); buf.Dispose(); } if (dev.attrs.Count == 0) continue; Console.WriteLine("\t\t" + dev.attrs.Count + " device-specific attributes found:"); foreach (Attr attr in dev.attrs) Console.WriteLine("\t\t\t" + attr.name); } /* Wait for user input */ Console.ReadLine(); }
/// <summary>Extract the samples corresponding to this channel from the /// given <see cref="iio.IOBuffer"/> object.</summary> /// <param name="buffer">A valid instance of the <see cref="iio.IOBuffer"/> class.</param> /// <param name="raw">If set to <c>true</c>, the samples are not converted from their /// hardware format to their host format.</param> /// <returns>A <c>byte</c> array containing the extracted samples.</returns> /// <exception cref="System.Exception">The samples could not be read.</exception> public byte[] read(IOBuffer buffer, bool raw = false) { if (!is_enabled()) throw new Exception("Channel must be enabled before the IOBuffer is instantiated"); if (this.output) throw new Exception("Unable to read from output channel"); byte[] array = new byte[(int) (buffer.samples_count * sample_size)]; MemoryStream stream = new MemoryStream(array, true); GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned); IntPtr addr = handle.AddrOfPinnedObject(); uint count; if (raw) count = iio_channel_read_raw(this.chn, buffer.buf, addr, buffer.samples_count * sample_size); else count = iio_channel_read(this.chn, buffer.buf, addr, buffer.samples_count * sample_size); handle.Free(); stream.SetLength((long) count); return stream.ToArray(); }