Let me start by saying I have the same code logic wise on an arduino.
Trying to read a set of 3 magnet sensors via I2c. For some reason it is taking almost half a second to got through the read cycle for each sensor, as you will see by the code it takes a few commands to complete a reading. So I'm getting a full 3 sensor read in 1.3 so not even a full read per second. On the arduino I am getting around 25-35 reads per second. I remember reading that certain things are slower on the netduino, but I don't think that meant that huge a difference. So I assume I am doing something wrong with the I2C maybe? Can anyone point me in the correct direction? Code Below: **Note I changed to a using in the loop as it would error out if I tried to make mulitple I2C devices at once...
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using System.Threading; namespace UDPMag_NetduinoPlus2 { class MagSensor { private MagDataStruct magData = new MagDataStruct(); private bool collect = false; public struct MagDataStruct { public byte mag0Xh; public byte mag0Yh; public byte mag0Zh; public byte mag0Xl; public byte mag0Yl; public byte mag0Zl; public byte mag1Xh; public byte mag1Yh; public byte mag1Zh; public byte mag1Xl; public byte mag1Yl; public byte mag1Zl; public byte mag2Xh; public byte mag2Yh; public byte mag2Zh; public byte mag2Xl; public byte mag2Yl; public byte mag2Zl; } public delegate void DataReceived(MagDataStruct data); public event DataReceived OnDataReceived = null; public bool SensorRunning { get { return collect; } } public MagSensor() { // Create a delegate of type "ThreadStart", which is a pointer to the // worker thread's main function ThreadStart delegateWorkerThread = new ThreadStart(Main); // Next create the actual thread, passing it the delegate to the main function Thread threadWorker = new Thread(delegateWorkerThread); // Now start the thread. threadWorker.Start(); } public void StartCollecting() { collect = true; } public void StopCollecting() { collect = false; } #region The actual worker thread code /// <summary> /// This is a method which does the work of the worker thread. /// It waits (sleeps) for a UDP message to be received and passes it using the /// supplied Interface. /// </summary> private void Main() { // Loop forever while (true) { if (collect) { magData = new MagDataStruct(); DateTime t1 = DateTime.Now; for (int i = 0; i < 3; i++) { ushort addy = 0x30; //Mag 0 if (i == 1) { addy = 0x31; //Mag 1 } else { addy = 0x32; //Mag 2 } using (I2CDevice mag = new I2CDevice(new I2CDevice.Configuration(addy, 400))) { runSensor(mag, i); } } DateTime t2 = DateTime.Now; TimeSpan ts = t2 - t1; Debug.Print(ts.Minutes.ToString() + ":" + ts.Seconds.ToString() + ":" + ts.Milliseconds.ToString()); // Call Event! if (OnDataReceived != null) OnDataReceived(magData); } } } private void runSensor(I2CDevice device, int id) { i2c_write(device, 0x07, 0x80); // Refill Charge Cap i2c_write(device, 0x07, 0x20); // SET Instruction i2c_write(device, 0x07, 0x40); // RESET Instruction i2c_write(device, 0x07, 0x01); // Take Measurement TM Flag == 1 while ((i2c_read(device, 0x06) & 0x01) != 1) { }; // Wait formeasurment to be Done. switch (id) //Set byes in data struct. { case 0: magData.mag0Xl = i2c_read(device, 0x00); magData.mag0Xh = i2c_read(device, 0x01); magData.mag0Yl = i2c_read(device, 0x02); magData.mag0Yh = i2c_read(device, 0x03); magData.mag0Zl = i2c_read(device, 0x04); magData.mag0Zh = i2c_read(device, 0x05); break; case 1: magData.mag1Xl = i2c_read(device, 0x00); magData.mag1Xh = i2c_read(device, 0x01); magData.mag1Yl = i2c_read(device, 0x02); magData.mag1Yh = i2c_read(device, 0x03); magData.mag1Zl = i2c_read(device, 0x04); magData.mag1Zh = i2c_read(device, 0x05); break; case 2: magData.mag2Xl = i2c_read(device, 0x00); magData.mag2Xh = i2c_read(device, 0x01); magData.mag2Yl = i2c_read(device, 0x02); magData.mag2Yh = i2c_read(device, 0x03); magData.mag2Zl = i2c_read(device, 0x04); magData.mag2Zh = i2c_read(device, 0x05); break; } } private void i2c_write(I2CDevice device, byte register, byte data) { byte[] trans = { register, data }; I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1]; i2cTx[0] = I2CDevice.CreateWriteTransaction(trans); device.Execute(i2cTx, 1); } private byte i2c_read(I2CDevice device, byte register) { byte[] trans = { register }; byte[] read = new byte[1]; I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[2]; i2cTx[0] = I2CDevice.CreateWriteTransaction(trans); i2cTx[1] = I2CDevice.CreateReadTransaction(read); device.Execute(i2cTx,1); return read[0]; //Return 1 byte... } #endregion } }