public double AcquisitionTemperature() { byte[] temp = new byte[2]; //Besoin de 2 octets double realtemp = 0.0; //15 chiffres de précision(virgule flottante) MyI2C.Config = Temperature; xActions = new I2CDevice.I2CTransaction[3]; //Création de 3 transactions byte[] commande1 = new byte[1] { 238 }; //Commande EE(Début de conversion) byte[] commande2 = new byte[1] { 170 }; //Commande AA(Lecture de la température) //Transactions xActions[0] = I2CDevice.CreateWriteTransaction(commande1); //Représente une transaction I2c qui écrit dans le dispositif adressé xActions[1] = I2CDevice.CreateWriteTransaction(commande2); xActions[2] = I2CDevice.CreateReadTransaction(temp); //Représente une transaction I2c qui lit le dispositif adressé MyI2C.Execute(xActions, 1000); //Accès au bus I2c avec un délai de 1sec realtemp = temp[0]; if (temp[1] != 0) //(opérateur d'inégalité) { realtemp = realtemp + 0.5; } return(realtemp); }
public byte[] readI2CRegister(int addr) { Debug.Print("Attempting to read I2C register " + addr.ToString("X2")); i2cDevice.Config = i2cReadConfig; byte Addr = (byte)(addr & 0x00FF); byte[] buffer = new byte[1]; I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[2]; reading[0] = I2CDevice.CreateWriteTransaction(new byte[] { Addr }); reading[1] = I2CDevice.CreateReadTransaction(buffer); Debug.Print("Executing read transaction..."); int bytesRead = i2cDevice.Execute(reading, 100); if (bytesRead == 0) { throw new NullReferenceException("0 bytes read for i2c register " + addr.ToString("X2")); } string message = "Read I2c " + addr.ToString("X2") + ":"; for (int index = 0; index < buffer.Length; index++) { message += " " + buffer[index].ToString("X2"); } Debug.Print("Transaction complete, reading " + buffer.Length + " bytes"); Debug.Print(message); return(buffer); }
private void ds1624Thread() { I2CDevice.I2CTransaction[] cmdStartConversion = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0xEE }) }; I2CDevice.I2CTransaction[] cmdStartRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { 0xAA }) }; byte[] temperatureData = new byte[2]; I2CDevice.I2CTransaction[] cmdRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(temperatureData) }; while (runThread == true) { if (i2cDevice.Execute(cmdStartConversion, 100) == 0) { throw new Exception("i2c"); } Thread.Sleep(1000); if (i2cDevice.Execute(cmdStartRead, 100) == 0) { throw new Exception("i2c"); } if (i2cDevice.Execute(cmdRead, 100) < 2) { throw new Exception("i2c"); } else { this.lastTemperature = this.ConvertTemperature(temperatureData); this.TemperatureEvent(this.LastTemperature); } } }
public void Write(I2CDevice.Configuration config, byte[] buffer, int transactionTimeout) { _slaveDevice.Config = config; I2CDevice.I2CTransaction[] i2CTransactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; lock (_slaveDevice) _slaveDevice.Execute(i2CTransactions, transactionTimeout); }
public SiliconLabsSI7005() { using (I2CDevice device = new I2CDevice(new I2CDevice.Configuration(DeviceId, ClockRateKHz))) { byte[] writeBuffer = { RegisterIdDeviceId }; byte[] readBuffer = new byte[1]; // check that sensor connected I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer), I2CDevice.CreateReadTransaction(readBuffer) }; if (device.Execute(action, TransactionTimeoutMilliseconds) == 0) { // The first read always fails } if (device.Execute(action, TransactionTimeoutMilliseconds) == 0) { throw new Exception("Unable to read DeviceId"); } if (readBuffer[0] != RegisterDeviceId) { throw new Exception("DeviceId invalid"); } } }
/// <summary> /// Receive data back from the nunchuck, /// returns 1 on successful read. returns 0 on failure /// </summary> public bool GetData() { if (_isDisposed) { throw new ObjectDisposedException(); } if (!IsConnected) { Init(InitTimeout); return(false); } // send request for data WriteToDevice(0x00); // get the data byte[] inputBuffer = new byte[6]; var readTransaction = I2CDevice.CreateReadTransaction(inputBuffer); // execute both transactions int tranferred = _device.Execute(new I2CDevice.I2CTransaction[] { readTransaction }, TransactionTimeout); // less then 6 bytes read? if (tranferred != inputBuffer.Length) { // communication error, no need to reinitialize return(false); } if (!_disableEncryption) { // decrypt data in buffer for (int i = 0; i < 6; i++) { inputBuffer[i] = DecodeByte(inputBuffer[i]); } } // check if all 0xff read? byte cnt = 0; for (int i = 0; i < inputBuffer.Length; i++) { if (inputBuffer[i] == 0xff) { cnt++; } } if (cnt == inputBuffer.Length) { // this is connection error. _isConnected = false; return(false); } ExtractData(inputBuffer); return(true); }
public void SetChannel(int channel) { byte[] TxBuff = new byte[1]; int requestchannel = channel - 1; if (channel > 4) { requestchannel = requestchannel - 4; } TxBuff[0] = ChannelArray[requestchannel]; if (channel <= 4) { MyI2C.Config = conADCA; } else { MyI2C.Config = conADCB; } I2CDevice.I2CTransaction[] WriteTran = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(TxBuff) }; MyI2C.Execute(WriteTran, 1000); Thread.Sleep(5); }
/// <summary> /// Reads an arbitrary RTC or RAM register /// </summary> /// <param name="address">Register address between 0x00 and 0x3f</param> /// <param name="length">The number of bytes to read</param> /// <returns>The value of the bytes read at the address</returns> public byte[] ReadRegister(byte address, int length = 1) { if (length < 1) { throw new ArgumentOutOfRangeException("length", "Must read at least 1 byte"); } if (address + length - 1 > DS1307_RAM_END_ADDRESS) { throw new ArgumentOutOfRangeException("Invalid register address"); } var buffer = new byte[length]; lock (clock) { clock.Config = config; // Read the RAM register @ the address var transaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { address }), I2CDevice.CreateReadTransaction(buffer) }; if (clock.Execute(transaction, DS1307_I2C_TRANSACTION_TIMEOUT_MS) == 0) { throw new Exception("I2C transaction failed"); } } return(buffer); }
public void SetupDevice() { relayPort = new OutputPort((Cpu.Pin) 21, false); relayPort.Write(true); var i2cConfig = new I2CDevice.Configuration(0x51, 400); i2cSensor = new I2CDevice(i2cConfig); var xwsaction = I2CDevice.CreateWriteTransaction(new byte[] { 0x01 }); var xrsaction = I2CDevice.CreateReadTransaction(new byte[8]); i2cSensor.Execute(new I2CDevice.I2CTransaction[] { xwsaction, xrsaction }, 100); var read = xrsaction.Buffer; var temperature = BitConverter.ToDouble(read, 0); var accelOrder = I2CDevice.CreateWriteTransaction(new byte[] { 0x02 }); var accelRead = I2CDevice.CreateReadTransaction(new byte[24]); i2cSensor.Execute(new I2CDevice.I2CTransaction[] { accelOrder, accelRead }, 100); var accelX = BitConverter.ToDouble(accelRead.Buffer, 0); var accelY = BitConverter.ToDouble(accelRead.Buffer, 8); var accelZ = BitConverter.ToDouble(accelRead.Buffer, 16); }
/// <summary> /// Generic write operation to I2C slave device. /// </summary> /// <param name="config">I2C slave device configuration.</param> /// <param name="writeBuffer">The array of bytes that will be sent to the device.</param> /// <param name="transactionTimeout">The amount of time the system will wait before resuming execution of the transaction.</param> public int Write(I2CDevice.Configuration config, byte[] writeBuffer, int transactionTimeout) { lock (_slaveDevice) { // Set i2c device configuration. _slaveDevice.Config = config; // create an i2c write transaction to be sent to the device. I2CDevice.I2CTransaction[] writeXAction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; // the i2c data is sent here to the device. int transferred = 0; do { transferred = _slaveDevice.Execute(writeXAction, transactionTimeout); if (transferred == 0) { throw new Exception("Could not write to device."); } }while (transferred == 0); // make sure the data was sent. if (transferred != writeBuffer.Length) { throw new Exception("Could not write to device."); } return(transferred); } }
private static void WriteToCompass(byte[] command) { I2CDevice.I2CWriteTransaction writeTransaction = I2CDevice.CreateWriteTransaction(command); I2CDevice.I2CTransaction[] transaction = new I2CDevice.I2CTransaction[] { writeTransaction }; compass.Execute(transaction, 1000); compass.Execute(transaction, 1000); }
public int Execute(I2CDevice.Configuration config, I2CDevice.I2CTransaction[] actions, int timeoutMilliseconds) { ThrowIfDisposed(); lock (_nativeBus) { _nativeBus.Config = config; return(_nativeBus.Execute(actions, timeoutMilliseconds)); } }
//I2C write function that takes the address and data private static void I2CWrite(byte Address, byte Data) { WriteCommand = new I2CDevice.I2CTransaction[1]; WriteCommand[0] = I2CDevice.CreateWriteTransaction(new byte[2]); WriteCommand[0].Buffer[0] = Address; WriteCommand[0].Buffer[1] = Data; MyI2C.Execute(WriteCommand, 100); }
public byte CurrentByteRead() { byte[] readBuffer = { 0x0 }; I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(readBuffer); I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { read }; m_transfer_count = I2C_device.Execute(transactions, m_timeout); m_transfer_type = transfer_type.read; return(readBuffer[0]); }
/// <summary> /// I2C Send /// </summary> /// <param name="values">Byte Array</param> private void send(byte[] values) { // Init Bus, multible Devices I2CcommBus.Config = I2C_dev; I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1]; xActions[0] = I2CDevice.CreateWriteTransaction(values); I2CcommBus.Execute(xActions, 50); }
private void write(byte reg, byte value) { Debug.Print("> " + reg.ToString() + " " + value.ToString()); var Actions = new I2CDevice.I2CTransaction[1]; Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { reg, value }); I2C.Execute(Actions, 1000); Thread.Sleep(5); // Mandatory after each Write transaction !!! }
public static bool TryGetRegisters(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte[] result) { int bytesTransfered = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { register }) }, timeout); Thread.Sleep(writePause); bytesTransfered += device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(result) }, timeout); return(bytesTransfered == result.Length + 1); }
public byte ReadConfiguration() { if (device.Execute(xReadAction, TIME_OUT) == 0) { throw new Exception(TIME_OUT_ERROR); } return(dataReg[Resolution == MCP324xResolution.EighteenBits ? 3 : 2]); }
void WriteRegister(byte registerOffset, byte data) { Trace.Print("Register " + registerOffset.ToString() + " ==> " + data.ToString()); byte[] writeBuffer = { registerOffset, data }; var operations = new I2CDevice.I2CTransaction[1]; operations[0] = I2CDevice.CreateWriteTransaction(writeBuffer); i2cDevice.Execute(operations, Pca9685.I2CTimeout); }
/* Sonar I2C write function * * @param Address Address written to * @param Data Data being written */ private void I2CWrite(byte Address, byte Data) { /* make sure our config is selected */ MyI2C.Config = SonarConfig; WriteCommand = new I2CDevice.I2CTransaction[1]; WriteCommand[0] = I2CDevice.CreateWriteTransaction(new byte[2]); WriteCommand[0].Buffer[0] = Address; WriteCommand[0].Buffer[1] = Data; MyI2C.Execute(WriteCommand, 100); }
/// <summary> /// Write to a I2C slave device. /// </summary> /// <param name="device">I2C slave device.</param> /// <param name="writeBuffer">Bytes to be written to the slave.</param> /// <param name="transactionTimeout">Time in mS the system will allow for a transaction.</param> public void Write(I2CSlave device, byte[] writeBuffer, int transactionTimeout) { Device.Config = new I2CDevice.Configuration(device.Address, ClockRate); I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(writeBuffer) }; lock (Device) if (Device.Execute(writeTransaction, transactionTimeout) != writeBuffer.Length) { throw new IOException("Could not write to slave."); } }
public void WriteData(byte data) { var buffer = new byte[] { data }; var transactions = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(buffer) }; _bus.Config = _dataDeviceConfiguration; _bus.Execute(transactions, _timeout); }
public static int I2CEEPROM_ReadBytes(UInt16 Addr, byte[] data) { I2CDevice.I2CTransaction[] trans = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Addr >> 8), (byte)(Addr & 0xff) }), I2CDevice.CreateReadTransaction(data) }; int bytesTransfered = _24LC16.Execute(trans, 1000); return(bytesTransfered); }
/// <summary> /// 指定アドレスのデータを取得する /// </summary> /// <param name="reg">データ取得対象のアドレス</param> /// <returns>取得データ</returns> protected byte RegRead(byte reg) { _adata[0] = reg; _trRegRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(_adata), I2CDevice.CreateReadTransaction(_rdata) }; _i2C.Execute(_trRegRead, _timeout); return(_rdata[0]); }
public static bool TryGetRegisters(this I2CDevice device, I2CDevice.Configuration config, int timeout, byte register, byte[] parameters, byte[] result) { byte[] data = new byte[parameters.Length + 1]; data[0] = register; Array.Copy(parameters, 0, data, 1, parameters.Length); int bytesTransfered = device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(data) }, timeout); Thread.Sleep(writePause); bytesTransfered += device.Execute(config, new I2CDevice.I2CTransaction[] { I2CDevice.CreateReadTransaction(result) }, timeout); return(bytesTransfered == result.Length + data.Length); }
private void ReadI2C(byte address, byte[] data) { i2c.Execute( new I2CDevice.I2CTransaction[1] { I2CDevice.CreateWriteTransaction(new Byte[] { address }) }, Timeout); i2c.Execute( new I2CDevice.I2CTransaction[1] { I2CDevice.CreateReadTransaction(data) }, Timeout); return; }
/* * isr_func関数 * Caller : コンストラクタ * BMI422_CNTL2_VALのDRPbitの設定で、DRDY端子の立ち上がりもしくは立下りで割り込み関数を設定する * C#版をどのように実装する??? * * arduino用の外部割込みが発生した場合に実行する関数: * attachInterrupt(割り込み番号, 関数名, 割り込みモード) * * 割り込みモード: * RISING ピンの状態がLOWからHIGHに変わったときに発生 * FALLING ピンの状態がHIGHからLOWに変わったときに発生 */ /* * public void isr_func(int int_num, void func(void)) * { * if (BM1422_CNTL2_VAL & BM1422_CNTL2_DRP) { * attachInterrupt(int_num, func, RISING); * } else { * attachInterrupt(int_num, func, FALLING); * } * } * */ /** * RegRead(byte, ref byte[]) * レジスタからデータを読み取る * addr : デバイスに送信されるバイトの配列(アドレス) * rdata : デバイスから読み取られたデータを格納 * 戻り値:レジスタへ転送したバイト数 */ public byte RegRead(byte addr, ref byte[] rdata) { adata[0] = addr; trRegRead = new I2CDevice.I2CTransaction[] { I2CDevice.CreateWriteTransaction(adata), //デバイスへアドレスを転送 I2CDevice.CreateReadTransaction(rdata) }; //デバイスよりデータを読み込み int rc = i2c.Execute(trRegRead, timeout); //戻り値:転送されたバイト数 #if DEBUG_REGREAD Debug.Print("Reg Read, Numbar of transfer data = " + rc); Debug.Print("REG[0x" + adata[0].ToString("X2") + "]=0x" + rdata[0].ToString("X2")); #endif return((byte)rc); }
//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; }
/// <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."); }
public TSL2561(byte I2CAddress, int ClockinKHz) { I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz); I2C = new I2CDevice(I2CConfig); // read the ID register. var Actions = new I2CDevice.I2CTransaction[2]; byte[] rx = new byte[1]; Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a }); Actions[1] = I2CDevice.CreateReadTransaction(rx); if (I2C.Execute(Actions, 1000) == 0) { Debug.Print("Read ID Register failed"); // exit or something } else { Debug.Print("ID value: " + rx[0].ToString()); } // 4 msb must be 0001 for a TSL2561 if ((rx[0] & 0xf0) != 0xa0) { // exit or something } setGain(0x10); Thread.Sleep(5); // Mandatory after each Write transaction !!! }