/// <summary> /// Reads the raw counts from the ADC /// </summary> /// <param name="number">Number of bytes to read (four if 18-bit conversion else three) </param> /// <returns>Result of 12, 14, 16 or 18-bit conversion</returns> double dataReadRaw(byte number) { Int32 data = 0; byte[] inbuffer = new byte[number]; I2CDevice.I2CReadTransaction readTransaction = I2CDevice.CreateReadTransaction(inbuffer); I2CDevice.I2CTransaction[] xAction = new I2CDevice.I2CTransaction[] { readTransaction }; _i2cBus = new I2CDevice(_config); int transferred = _i2cBus.Execute(xAction, _transactionTimeOut); _i2cBus.Dispose(); if (transferred < inbuffer.Length) { throw new System.IO.IOException("Error i2cBus " + _sla); } else if ((inbuffer[number - 1] & 0x80) == 0) // End of conversion { if (_resolution == SampleRate.EighteenBits) { data = (((Int32)inbuffer[0]) << 16) + (((Int32)inbuffer[1]) << 8) + (Int32)inbuffer[2]; } else { data = (((Int32)inbuffer[0]) << 8) + (Int32)inbuffer[1]; } _endOfConversion = true; return(data &= _dataMask); } else { _endOfConversion = false; return(0); // return something } }
/// <summary> /// Releases clock resources /// </summary> public void Dispose() { if (privateClock) { clock.Dispose(); } }
static ArrayList FindDevices() { var deviceAddresses = new ArrayList(); for (ushort address = 0x48; address < 0x48 + 0x07; address++) { var device = new I2CDevice(new I2CDevice.Configuration(address, ClockRateKHz)); byte[] writeBuffer = { 0x00 }; byte[] readBuffer = new byte[2]; I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) , I2CDevice.CreateReadTransaction(readBuffer) }; var transactionResult = device.Execute(action, transactionTimeout); if (transactionResult == 3) { deviceAddresses.Add(device.Config.Address); Debug.Print("Found device @" + device.Config.Address); } else { Debug.Print("No device found @" + device.Config.Address); } device.Dispose(); } return(deviceAddresses); }
public void Read() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] readActions = new I2CDevice.I2CTransaction[2]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[1] { 2 }; readActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); // create read buffer to read the register byte[] RegisterValue = new byte[1]; readActions[1] = I2CDevice.CreateReadTransaction(RegisterValue); // Now we access the I2C bus and timeout in one second if no responce MyI2C.Execute(readActions, 1000); if (currentValue != RegisterValue[0]) { currentValue = RegisterValue[0]; if (OnChange != null) { OnChange(dialId, currentValue); } } MyI2C.Dispose(); //Debug.Print("Register value: " + RegisterValue[0].ToString()); }
/// <summary> /// Scan range of addresses and print devices to debug output. /// </summary> /// <param name="startAddress">Start of scanning (included)</param> /// <param name="endAddress">End of scanning (included)</param> /// <param name="clockRateKhz">frequency in Khz</param> public static void ScanAddresses(ushort startAddress, ushort endAddress, ushort clockRateKhz = 100) { Debug.Print("Scanning..."); for (ushort adr = startAddress; adr <= endAddress; adr++) { I2CDevice device = new I2CDevice(new I2CDevice.Configuration(adr, clockRateKhz)); byte[] buff = new byte[1]; try { I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buff); var ret = device.Execute(new I2CDevice.I2CTransaction[] { read }, 1000); if (ret > 0) { Debug.Print("Device on address: " + adr); } else { Debug.Print("NO: " + adr); } } catch (Exception) { continue; } finally { //otestovat yda se dela pokazde device.Dispose(); device = null; } } Debug.Print("Scanning finished."); }
/// <summary> /// Close communication /// </summary> public void Close() { // Dispose Reset Port try { _resetport.Dispose(); } catch { } // Dispose I2C Connection try { _i2cconnection.Dispose(); } catch { } }
public void Dispose() { if (_isDisposed) { return; } _device.Dispose(); _isDisposed = true; }
public void Dispose() { myInputPort.Dispose(); myInterruptPort.Dispose(); myOutputPort.Dispose(); myI2CDevice.Dispose(); mySPI.Dispose(); myTristatePort.Dispose(); mySerialPort.Dispose(); Dispose(true); GC.SuppressFinalize(this); }
/// <summary> /// Releases internal resources held by the device. /// </summary> /// <param name="disposing">True if called from Dispose, false if called from the finalizer.</param> private void Dispose(bool disposing) { if (disposing) { lock (s_deviceLock) { --s_deviceRefs; if ((s_deviceRefs == 0) && (s_device != null)) { s_device.Dispose(); s_device = null; } } } }
void writeConfRegister(byte value) { byte[] outbuffer = { value }; I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(outbuffer); I2CDevice.I2CTransaction[] xAction = new I2CDevice.I2CTransaction[] { writeTransaction }; _i2cBus = new I2CDevice(_config); int transferred = _i2cBus.Execute(xAction, _transactionTimeOut); _i2cBus.Dispose(); if (transferred < outbuffer.Length) { throw new System.IO.IOException("Error i2cBus " + _sla); } }
//Was unable to make this stupd proof due to execpetions. //Input Current Addres in 7bit form and Desired Address in 8 bit form. public SonarModuleAddressChange(byte CurrentAddress, byte DesiredAddress) { I2CDevice.Configuration SonarConfig = new I2CDevice.Configuration(CurrentAddress, 100); if (MyI2C == null) { MyI2C = new I2CDevice(SonarConfig); } //Do a single write read to check if the address works byte[] Data = new byte[1]; I2CDevice.I2CTransaction[] ReadCommand = new I2CDevice.I2CTransaction[2]; ReadCommand[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00 }); ReadCommand[1] = I2CDevice.CreateReadTransaction(Data); AnyValue = MyI2C.Execute(ReadCommand, 100); if (AnyValue != 0) //11 for the SRF08 and 5 for the SRF10; { //Address was found and now we send change address commands I2CDevice.I2CTransaction[] WriteCommand = new I2CDevice.I2CTransaction[1]; WriteCommand[0] = I2CDevice.CreateWriteTransaction(new byte[2]); WriteCommand[0].Buffer[0] = 0x00; WriteCommand[0].Buffer[1] = 0xA0; MyI2C.Execute(WriteCommand, 100); WriteCommand[0].Buffer[0] = 0x00; WriteCommand[0].Buffer[1] = 0xAA; MyI2C.Execute(WriteCommand, 100); WriteCommand[0].Buffer[0] = 0x00; WriteCommand[0].Buffer[1] = 0xA5; MyI2C.Execute(WriteCommand, 100); WriteCommand[0].Buffer[0] = 0x00; WriteCommand[0].Buffer[1] = DesiredAddress; MyI2C.Execute(WriteCommand, 100); Debug.Print("Success"); Thread.Sleep(5000); } else { Debug.Print("Failed"); Thread.Sleep(5000); } MyI2C.Dispose(); MyI2C = null; }
public void Init() { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] initActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) //initActions[0] = MyI2C.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); I2CDevice.CreateWriteTransaction(new byte[] { 0, (byte)(DialMode.AUT | DialMode.CYC | DialMode.OneFree) }); int result = MyI2C.Execute(initActions, 1000); //Debug.Print("Device Init Result " + result); MyI2C.Dispose(); }
public void SetValue(byte newValue) { I2CDevice MyI2C = new I2CDevice(i2con); //create transactions (we need 2 in this example) I2CDevice.I2CTransaction[] setActions = new I2CDevice.I2CTransaction[1]; // create write buffer (we need one byte) byte[] RegisterNum = new byte[2] { 2, newValue }; setActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum); MyI2C.Execute(setActions, 1000); MyI2C.Dispose(); }
/// <summary>Internal implementation of disposed</summary> /// <param name="disposing"></param> ///<remarks> /// <para>This version of Dispose executes in two distinct scenarios. /// If the Disposing flag is true, the method has been called /// directly or indirectly by a user's code. In this case /// Managed and unmanaged resources can be disposed.</para> /// <para>If the Disposing flag is false, the method has been called /// by the runtime from inside the finalizer and this MUST not /// reference other objects. Only unmanaged resources can be disposed.</para> /// </remarks> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!_isDisposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. _device.Dispose(); } // Note disposing has been done. _isDisposed = true; } }
/// <summary> /// Get 16 bits data in RAM Register /// </summary> /// <param name="register">Air temperature TA = 0x06, Object temperature Tobj1 = 0x07</param> /// <returns>Temperature in Kelvin divided by 0.02</returns> UInt16 getRegister(byte register) { byte[] outbuffer = new byte[] { register }; byte[] inbuffer = new byte[2]; I2CDevice.I2CTransaction[] XAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(outbuffer), I2CDevice.CreateReadTransaction(inbuffer) }; i2cBus = new I2CDevice(config); int transferred = i2cBus.Execute(XAction, 1000); i2cBus.Dispose(); if (transferred < (outbuffer.Length + inbuffer.Length)) { throw new System.IO.IOException(); } else { return((UInt16)(((inbuffer[1] & 0x007F) << 8) + inbuffer[0])); } }
// Destructor public void Dispose() { I2CcommBus.Dispose(); }
public void Dispose() { _slaveDevice.Dispose(); }
const byte ClockRateKHz = 59; //kHz public static void Main() { var devices = FindDevices(); while (true) { try { Thread.Sleep(1000); foreach (ushort deviceAddress in devices) { byte[] writeBuffer = { 0x00 }; byte[] readBuffer = new byte[2]; I2CDevice device = null; try { device = new I2CDevice(new I2CDevice.Configuration(deviceAddress, ClockRateKHz)); I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) , I2CDevice.CreateReadTransaction(readBuffer) }; if (device.Execute(action, transactionTimeout) == 3) { var msb = (short)readBuffer[0]; var lsb = (short)readBuffer[1]; double temp = 0; bool negativeTemp = (msb & 0x80) == 0x80; int tempData = (msb << 8) + lsb; tempData = tempData >> 5; if (negativeTemp) { //TODO: apply two's complement and calc negative temp Debug.Print("Device @" + deviceAddress + ": Negative Temp"); } else { temp = tempData * .125; } Debug.Print("Device @" + deviceAddress + ":" + temp); } else { Debug.Print("Device @" + deviceAddress + " is not responding..."); } } finally { if (device != null) { device.Dispose(); } } } } catch (Exception ex) { Debug.Print(ex.Message); } } }
/// <summary> /// Dispose of the I2C bus. /// </summary> public void Dispose() { Device.Dispose(); }
public new void Dispose() { fm3_i2c.Dispose(); }
protected override void DisposeManagedResources() { _nativeBus.Dispose(); }
//IDisposable public void Dispose() { lock (locker) _slave.Dispose(); }
public override void Dispose() { _device.Dispose(); }
public void Dispose() { _slave.Dispose(); }
/// <summary> /// Releases clock resources /// </summary> public void Dispose() { clock.Dispose(); }
/// <summary> /// Releases clock resources /// </summary> public void Dispose() { Clock.Dispose(); Clock = null; }
public void Dispose() //Libère les ressources utilisées par l'objet de I2CDevice { MyI2C.Dispose(); xActions = null; }
public void Dispose() { I2C_device.Dispose(); }