/// <summary> /// Read from sensor's Main Register /// </summary> /// <param name="location">Location to read (sensor data starts at 128)</param> /// <param name="nBytes">Number of bytes to read (max = 32)</param> /// <param name="i2CAddress">I2C Address</param> /// <returns>4 byte timestamp followed by nBytes of data</returns> public byte[] ReadFromMainRegister(byte location, byte nBytes, byte i2CAddress) { if (nBytes > 32) { MessageBox.Show("Error - Trying to read too much"); return(null); } if (location + nBytes > 191) //0 - 191 are valid memory locations { MessageBox.Show("Error - Trying to read off the end of the main register"); return(null); } byte[] cmdToArduino = SerialCommand.GenerateReadCommand(i2CAddress, cmdItr_++, location, nBytes); try { serialPort_.Write(cmdToArduino, 0, cmdToArduino.Length); } catch (Exception) // USB has been unplugged { return(null); } bool acknowledged = false; long attempts = 50; Thread.Sleep(10); //Give comms time to happen while (false == acknowledged && attempts > 0) { byte[] cmdFromArduino = ProcessSerialBuffer(); if (null != cmdFromArduino) { if (cmdToArduino[I2C_ID_BYTE] == cmdFromArduino[I2C_ID_BYTE]) { acknowledged = true; byte[] toReturn = new byte[nBytes + TIMESTAMP_SIZE]; Array.Copy(cmdFromArduino, I2C_TIMESTAMP, toReturn, 0, TIMESTAMP_SIZE); Array.Copy(cmdFromArduino, I2C_START_OF_DATA, toReturn, TIMESTAMP_SIZE, nBytes); return(toReturn); } else { MessageBox.Show("Comms error - failed ack"); acknowledged = true; //Move on anyway return(null); } } else { Thread.Sleep(10); attempts--; } } return(null); // no ack }
/// <summary> /// Write to sensor's Calibration Register /// </summary> /// <param name="toSend">Data to write (max 28 bytes)</param> /// <param name="location">Main register location (can write upto byte 128)</param> /// <param name="i2CAddress">I2C Address</param> /// <returns>Was successful?</returns> public bool WriteToCalibrationRegister(byte[] toSend, byte location, byte i2CAddress) { if (toSend.Length > 28) //Max write length (limited by 32 byte i2c transfer length = 28 bytes of data + header info) { MessageBox.Show("Trying to write a packet that is too large", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (toSend.Length + location > 1024) { MessageBox.Show("Trying to write into read only region", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } byte[] cmdToArduino = SerialCommand.GenerateWriteCalCommand(i2CAddress, cmdItr_++, location, toSend); serialPort_.Write(cmdToArduino, 0, cmdToArduino.Length); bool acknowledged = false; int attempts = 20; //Typically takes 4 - 6 attempts as the chip needs to //write the settings to flash which takes about 50ms. Thread.Sleep(10); //Give comms time to happen while (false == acknowledged && attempts > 0) { byte[] cmdFromArduino = ProcessSerialBuffer(); if (null != cmdFromArduino) { if (cmdToArduino[I2C_ID_BYTE] == cmdFromArduino[I2C_ID_BYTE]) { acknowledged = true; return(true); } else { MessageBox.Show("Comms Error - Failed Acknoledge Write Command", "Communications Error", MessageBoxButtons.OK, MessageBoxIcon.Error); acknowledged = true; //Move on anyway return(false); } } else { Thread.Sleep(10); //Keep waiting attempts--; } } return(false); // no ack, give up }
/// <summary> /// Write to Toggle the PIN /// </summary> /// <param name="toSend">Pins to Toggle</param> /// <returns>Was successful?</returns> public bool WriteToggleCommand(byte toSend) { byte[] cmdToArduino = SerialCommand.GenerateToggleCommand(4, cmdItr_++, 0, toSend); serialPort_.Write(cmdToArduino, 0, cmdToArduino.Length); bool acknowledged = false; long attempts = 50; Thread.Sleep(10); //Give comms time to happen while (false == acknowledged && attempts > 0) { byte[] cmdFromArduino = ProcessSerialBuffer(); if (null != cmdFromArduino) { if (cmdToArduino[I2C_ID_BYTE] == cmdFromArduino[I2C_ID_BYTE]) { acknowledged = true; return(true); } else { MessageBox.Show("Comms Error - Failed Acknoledge Write Command", "Communications Error", MessageBoxButtons.OK, MessageBoxIcon.Error); acknowledged = true; //Move on anyway return(false); } } else { Thread.Sleep(10); //Keep waiting attempts--; } } return(false); // no ack, give up }