public byte[] ReadBytes(int address, int count) { var buf = new byte[count]; var res = I2CNativeLib.ReadBytes(busHandle, address, buf, buf.Length); if (res == -1) { var message = String.Format( "Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError())); log.Error(message); throw new IOException(message); } if (res <= 0) { var message = String.Format("Error reading from address '{0}': I2C transaction failed", address); log.Error(message); throw new IOException(message); } if (res < count) { Array.Resize(ref buf, res); } return(buf); }
/// <summary> /// Writes an array of bytes to the specified device address. /// </summary> /// <param name="address"> /// The address of the target device. /// </param> /// <param name="bytes"> /// The byte array to write. /// </param> /// <remarks> /// Currently, RPi drivers do not allow writing more than 3 bytes at a time. /// As such, if an array of greater than 3 bytes is provided, an exception /// is thrown. /// </remarks> /// <exception cref="ObjectDisposedException"> /// This instance has been disposed and can no longer be used. /// </exception> /// <exception cref="InvalidOperationException"> /// You must open a conection to the I2C bus by calling <see cref="Open()"/> /// first. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="bytes"/> cannot be greater than 3 elements in length. /// </exception> /// <exception cref="IOException"> /// An I/O error occurred. The specified address is inacessible or the /// I2C transaction failed. /// </exception> public void WriteBytes(Int32 address, Byte[] bytes) { if (this._isDisposed) { throw new ObjectDisposedException("CyrusBuilt.MonoPi.IO.I2C.I2CBus"); } if (!this._isOpen) { throw new InvalidOperationException("No open connection to write to."); } if (bytes.Length > 3) { throw new ArgumentException("Cannot write more than 3 bytes at a time."); } Int32 result = UnsafeNativeMethods.I2CWriteBytes(this._busHandle, address, bytes, bytes.Length); if (result == -1) { String errDesc = UnixMarshal.GetErrorDescription(Stdlib.GetLastError()); throw new IOException("Error accessing address '" + address.ToString() + "': " + errDesc); } if (result == -2) { throw new IOException("Error writing to address '" + address.ToString() + "': I2C transaction failed."); } }
/// <summary> /// Reads bytes from the device at the specified address. /// </summary> /// <returns> /// The bytes read. /// </returns> /// <param name="address"> /// The address of the device to read from. /// </param> /// <param name="count"> /// The number of bytes to read. /// </param> /// <exception cref="ObjectDisposedException"> /// This instance has been disposed and can no longer be used. /// </exception> /// <exception cref="InvalidOperationException"> /// You must open a conection to the I2C bus by calling <see cref="Open()"/> /// first. /// </exception> /// <exception cref="IOException"> /// An I/O error occurred. The specified address is inacessible or the /// I2C transaction failed. /// </exception> public Byte[] ReadBytes(Int32 address, Int32 count) { if (this._isDisposed) { throw new ObjectDisposedException("CyrusBuilt.MonoPi.IO.I2C.I2CBus"); } if (!this._isOpen) { throw new InvalidOperationException("No open connection to write to."); } Byte[] buffer = new Byte[count]; Int32 result = UnsafeNativeMethods.I2CReadBytes(this._busHandle, address, buffer, buffer.Length); if (result == -1) { String errDesc = UnixMarshal.GetErrorDescription(Stdlib.GetLastError()); throw new IOException("Error accessing address at '" + address.ToString() + "': " + errDesc); } if (result <= 0) { throw new IOException("Error reading from address '" + address.ToString() + "': I2C transaction failed."); } if (result < count) { Array.Resize(ref buffer, result); } return(buffer); }
public static void Main() { string s = UnixMarshal.GetErrorDescription(Errno.ERANGE); Console.WriteLine("ERANGE={0}", s); s = UnixMarshal.GetErrorDescription((Errno)999999); Console.WriteLine("Invalid={0}", s); }
bool ShouldRetryOperation(string text, string file) { Errno errno = Stdlib.GetLastError(); if (errno == Errno.EINTR) { return(true); } return(ShouldRetryOperation(UnixMarshal.GetErrorDescription(errno), text, file)); }
/// <summary> /// Instantiates a new instance of <see cref="LinuxDisk"/>. /// </summary> /// <param name="path">The path to the disk device node.</param> public LinuxDisk(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } fd = Syscall.open(path, OpenFlags.O_RDONLY); if (fd == -1) { var errno = Stdlib.GetLastError(); throw new IOException(UnixMarshal.GetErrorDescription(errno)); } DisplayName = path; }
public void WriteBytes(int address, byte[] bytes) { log.Trace("Writing to I2C. Bus handle {0}", busHandle); var res = I2CNativeLib.WriteBytes(busHandle, address, bytes, bytes.Length); if (res == -1) { string message = String.Format( "Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError())); log.Error(message); throw new IOException(message); } if (res == -2) { var message = String.Format("Error writing to address '{0}': I2C transaction failed", address); log.Error(message); throw new IOException(message); } }
/// <summary> /// Open a connection to the I2C bus. /// </summary> /// <exception cref="ObjectDisposedException"> /// This instance has been disposed. /// </exception> /// <exception cref="IOException"> /// Unable to open the bus connection. /// </exception> public void Open() { if (this._isDisposed) { throw new ObjectDisposedException("CyrusBuilt.MonoPi.IO.I2C.I2CBus"); } if (this._isOpen) { return; } Int32 result = UnsafeNativeMethods.I2COpenBus(this._busPath); if (result < 0) { String errDesc = UnixMarshal.GetErrorDescription(Stdlib.GetLastError()); throw new IOException("Error opening bus '" + this._busPath + "': " + errDesc); } this._busHandle = result; this._isOpen = true; }
private void init() { pixelData = new short[8, 8]; lastPixelData = new short[8, 8]; if (SetMode(AMG88xxCtrlMode.NORMAL) < 0) { throw new IOException(String.Format("Error opening bus '{0}': {1}", i2cPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } if (Reset(AMG88xxReset.FLAG_RESET) < 0) { throw new IOException(String.Format("Error opening bus '{0}': {1}", i2cPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } Thread.Sleep(20); samplingTask = new BackgroundWorker(); samplingTask.DoWork += SamplingTask_DoWork; }
public void Open(string busPath) { log.Trace("I2C bus opening"); BusPath = busPath; int res = I2CNativeLib.OpenBus(busPath); if (res < 0) { throw new IOException(String.Format("Error opening bus '{0}': {1}", busPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } busHandle = res; log.Trace("I2C bus handle - {0}", busHandle); }
/// <summary> /// Writes array of bytes. /// </summary> /// <remarks>Do not write more than 3 bytes at once, RPi drivers don't support this currently.</remarks> /// <param name="address">Address of a destination device</param> /// <param name="bytes"></param> public void WriteBytes(int address, byte[] bytes) { int res = I2CNativeLib.WriteBytes(busHandle, address, bytes, bytes.Length); if (res == -1) { throw new IOException(String.Format("Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } if (res == -2) { throw new IOException(String.Format("Error writing to address '{0}': I2C transaction failed", address)); } }
/// <summary> /// .ctor /// </summary> /// <param name="busPath"></param> protected I2CBus(string busPath) { int res = I2CNativeLib.OpenBus(busPath); if (res < 0) { throw new IOException(String.Format("Error opening bus '{0}': {1}", busPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } busHandle = res; }
/// <summary> /// Writes array of bytes. /// </summary> /// <remarks>Do not write more than 3 bytes at once, RPi drivers don't support this currently.</remarks> /// <param name="address">Address of a destination device</param> /// <param name="bytes"></param> public void WriteBytes(int address, byte[] bytes) { int res = I2CNativeLib.WriteBytes(busHandle, address, bytes, bytes.Length); if (res == -1) { throw new IOException(String.Format("Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError()))); } // HACK: removing res== -2 check because it was disrupting communications //if (res== -2) // throw new IOException(String.Format("Error writing to address '{0}': I2C transaction failed", address)); }
Result CopyDirectory(string source_absolute_path, string target_path, FilePermissions protection) { if (!dirs_created.ContainsKey(target_path)) { while (true) { int r = Syscall.mkdir(target_path, protection | FilePermissions.S_IRWXU); if (r != -1) { break; } Errno errno = Stdlib.GetLastError(); if (errno == Errno.EINTR) { continue; } if (errno == Errno.EEXIST || errno == Errno.EISDIR) { break; } var msg = UnixMarshal.GetErrorDescription(errno); switch (Interaction.Query(OResult.RetryIgnoreCancel, msg, "While creating \"{0}\"", target_path)) { case OResult.Retry: continue; case OResult.Ignore: break; case OResult.Cancel: return(Result.Cancel); } } dirs_created [target_path] = protection; } var udi = new UnixDirectoryInfo(source_absolute_path); foreach (var entry in udi.GetFileSystemEntries()) { if (entry.Name == "." || entry.Name == "..") { continue; } string source = Path.Combine(source_absolute_path, entry.Name); string target = Path.Combine(target_path, entry.Name); if (entry.IsDirectory) { if (CopyDirectory(source, target, entry.Protection) == Result.Cancel) { return(Result.Cancel); } } else { if (!CopyFile(source, target)) { return(skip ? Result.Skip : Result.Cancel); } } } return(Result.Ok); }