/** @brief Checks in all possible I2C address if there is a device connected there. * * @param addressToDiscard is a list of addressess to be ommitted on the scanning. It is * useful to exclude LCD or temperature sensor addresses * @param callback is a function to be called after each address check with its result * * @return a list with the result of each checking */ public List <I2CData> scanning(List <int> addressToDiscard, Action <I2CData> callback = null) { List <I2CData> addressLog = new List <I2CData>(); for (int i = 0; i < MAX_ADDRESS; i += BYTES_LENGTH) { if (!addressToDiscard.Contains(i)) { var res = checkNthDevice(i); addressLog.Add(res); if (callback != null) { callback(res); } } else { I2CData output = new I2CData() { error = I2CError.Nak, log = "This address has been omitted. Another device may be connected there." }; addressLog.Add(output); callback(output); } } return(addressLog); }
private List <I2CData> readRange(byte i2CAddress, int beginAddress, int endAddress, Action <I2CData> callback = null) { int i = beginAddress; I2CData lastBlock = new I2CData() { error = I2CError.Success }; List <I2CData> range = new List <I2CData>(); while (i < endAddress && lastBlock.error == I2CError.Success) { lastBlock = read(i2CAddress, (byte)i); if (lastBlock == null) { throw new Exception("Device may not be connected. Read could not be performed."); } range.Add(lastBlock); if (callback != null) { callback(lastBlock); } i++; } return(range); }
private List <I2CData> writeRange(byte i2cAddress, byte[][] writeBytes, int writeStartIdx, int beginAddress, int endAddress, Action <I2CData> callback = null) { int memoryIdx = beginAddress; I2CData lastBlock = new I2CData() { error = I2CError.Success }; List <I2CData> data = new List <I2CData>(); int length = endAddress - beginAddress; int j = 0; byte[][] _wB = new byte[length][]; Array.Copy(writeBytes, writeStartIdx, _wB, 0, length); while (memoryIdx < endAddress && lastBlock.error == I2CError.Success) { lastBlock = write(i2cAddress, (byte)memoryIdx, _wB[j]); data.Add(lastBlock); if (callback != null) { callback(lastBlock); } memoryIdx++; j++; } return(data); }
/** @brief Read a session register. * * * @param i2cAddress is the address of the device to be found on the i2c bus * @param ntagType indicates if the NTag I2C is 1k or 2k version * @param registerAddress is the specific register address memory * * @return the result of the transaction */ public I2CData readSessionRegister(byte i2cAddress, TagType ntagType, byte registerAddress) { I2CData SessionRegister = null; byte memoryBegin = getSessionMemoryBegin(ntagType); SessionRegister = i2cCommand.readRegister(i2cAddress, memoryBegin, registerAddress); return(SessionRegister); }
/** @brief Writes a session register to the Ntag I2C * * @param i2caddress is the address of the device to be found on the i2c bus * @param ntagType indicates if the NTag I2C is 1k or 2k version * @param registerAddress is the specific register address memory * @param registerValue is the value of the register * * @return the response of the transaction. */ public I2CData writeSessionRegister(byte i2cAddress, TagType ntagType, byte registerAddress, byte registerValue) { I2CData sessionRegister = null; byte memoryBegin = getSessionMemoryBegin(ntagType); sessionRegister = i2cCommand.writeRegister(i2cAddress, memoryBegin, registerAddress, registerValue); return(sessionRegister); }
/** @brief Write all configuration registers to the Ntag I2C. * * @param i2cAddress is the address of the device to be found on the i2c bus * @param ntagType indicates if the NTag I2C is 1k or 2k version * @param registers is a byte array with the block of the registers value * * @return the response of the transaction. */ public I2CData writeAllConfigRegister(byte i2cAddress, byte[] registers, TagType ntagType) { int numRegs = Enum.GetNames(typeof(ConfigReg)).Length; I2CData output = null; byte memoryBegin = getConfigMemoryBegin(ntagType); output = i2cCommand.writeBlock(i2cAddress, memoryBegin, registers); return(output); }
public byte[] Send(I2CData data) { mData = data; UpdateGUIFromData(mData.Content); ctlI2CAddress1.Addr7 = mData.Address; chkRead.Checked = mData.IsRead; chkWrite.Checked = mData.IsWrite; numReadLength.Value = mData.ReadDataLength; return(Send()); }
/** @brief Reinitialize the communication with the port * * @param device is the object representing the device to connect with. * @param busSpeedInKHz is the bus speed on the bus for the communication * @return the response from the command to init the port. */ public I2CData reinit(int busSpeedInKHz, I2C_Device device) { I2CData result = null; setBusSpeedInHz(busSpeedInKHz * 1000); if (device.isConnected()) { result = init(device); } return(result); }
/** @brief Writes a configuration register to the Ntag I2C * * @param i2cAddress is the address of the device to be found on the i2c bus * @param ntagType indicates if the NTag I2C is 1k or 2k version * @param registerAddress is the specific register address memory * @param registerValue is the value of the register * * @return the response of the transaction. */ public I2CData writeConfigRegister(byte i2cAddress, TagType ntagType, byte registerAddress, byte registerValue) { I2CData configRegister = null; byte memoryBegin = getConfigMemoryBegin(ntagType); var allRegisters = readAllConfigRegister(i2cAddress, ntagType); byte[] registers = allRegisters.data; registers[(int)registerAddress] = registerValue; configRegister = i2cCommand.writeBlock(i2cAddress, (byte)(memoryBegin), registers); return(configRegister); }
/** @brief Extracts the data from the report. * * It extracts the interesting data from the report * like the flag, error and log information. This data * is encapsulated in a I2CData object. * * @return an object witht a set of informatoin of the report. */ public I2CData getI2CDataFromReport(int dataLength) { var _data = new I2CData { flag = getReportFlag(), error = getReportErrorInformation(), log = getI2CReportLog() }; if (dataLength > 0) { _data.data = getReadData(dataLength); } return(_data); }
private void btnEdit_Click(object sender, EventArgs e) { UpdateData(); mData.Address = ctlI2CAddress1.Addr7; Controls.frmI2CDataEdit frm = new Controls.frmI2CDataEdit(mData, this); if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { mData = frm.Data; UpdateGUIFromData(mData.Content); ctlI2CAddress1.Addr7 = mData.Address; chkRead.Checked = mData.IsRead; chkWrite.Checked = mData.IsWrite; numReadLength.Value = mData.ReadDataLength; } }
/** @brief Performs a transaction of data with the device * * It sends data to the device. It reads the response and the * data specified by the readDataLength. * * @param sentData is the data to send to the device * @param readDataLength is the length of the data requested to get * @return the response of the transaction */ public I2CData performTransaction(byte[] sentData, int readDataLength = 0) { I2CData returned = null; var reportSent = Write(sentData); if (reportSent != null && reportSent.IsStatusSuccess()) { var reportRead = Read(); if (reportRead != null && reportRead.IsStatusSuccess()) { returned = reportRead.getI2CDataFromReport(readDataLength); } } return(returned); }
private void frmMain_Load(object sender, EventArgs e) { //InitFtdiControls(); EnumBridgeX(); btnScan.Enabled = false; btnSend.Enabled = false; Configure config = new Configure(); config.Load(); mData = config.Data; if (i2cBridgeXs != null) { for (int i = 0; i < i2cBridgeXs.Length; i++) { if (i2cBridgeXs[i].PortName == config.PortName) { mBridge = i2cBridgeXs[i]; cmbI2CBridge.SelectedIndex = i; if (config.Opened) { OpenI2CAdapter(); } break; } } } if (mData == null) { mData = new I2CData(); } ctlI2CAddress1.Addr7 = mData.Address; UpdateGUIFromData(mData.Content); chkWrite.Checked = mData.IsWrite; chkRead.Checked = mData.IsRead; numReadLength.Value = mData.ReadDataLength; SetFormat(mData.Format); cmbLogDataType.SelectedIndex = (int)config.LogDataType; LoadDevicesConfigure(); }
//load the configure file public void Load() { try { XmlReader reader = XmlReader.Create("Config.xml"); XmlSerializer serializer = new XmlSerializer(typeof(Configure)); Configure temp = (Configure)serializer.Deserialize(reader); Data = temp.Data; LocationID = temp.LocationID; Opened = temp.Opened; PortName = temp.PortName; LogDataType = temp.LogDataType; reader.Close(); } catch (Exception e) { Debug.Print("Fail to load configure file."); } }
/// <summary> /// Send i2c data /// </summary> /// <param name="data"></param> /// <param name="readLength"></param> /// <returns></returns> public byte[] SendI2C(byte[] writedata, byte readLength) { try { I2CData data = new I2CData(); byte[] data2 = new byte[writedata.Length - 1]; for (int i = 1; i < writedata.Length; i++) { data2[i - 1] = writedata[i]; } // check if it is read only package if (IsReadOnly(writedata) && readLength > 0) { data.IsWrite = false; } else { data.IsWrite = true; } data.Content = data2; data.ReadDataLength = readLength; data.IsRead = readLength > 0; data.Address = (byte)(writedata[0]); data.Format = GetFormat(); mData = data; UpdateGUIFromData(mData.Content); ctlI2CAddress1.Addr7 = mData.Address; chkRead.Checked = mData.IsRead; chkWrite.Checked = mData.IsWrite; numReadLength.Value = mData.ReadDataLength; return(Send()); } catch (Exception ex) { LogText(ex.Message); } return(null); }
/** @brief Writes all session registers to the Ntag I2C * * @param i2caddress is the address of the device to be found on the i2c bus * @param ntagType indicates if the NTag I2C is 1k or 2k version * @param registers is a byte array with the block of the registers value * * @return the response of the transaction. */ public I2CData writeAllSessionRegister(byte i2cAddress, byte[] registers, TagType ntagType) { int numRegs = Enum.GetNames(typeof(SessionReg)).Length; int i = 0; I2CData writeOutput = new I2CData() { error = I2CError.Success }; do { var regOut = writeSessionRegister(i2cAddress, ntagType, (byte)i, registers[i]); if (regOut.error != I2CError.Success) { writeOutput.error = regOut.error; } i++; } while (i < numRegs && writeOutput.error == I2CError.Success); return(writeOutput); }
private I2CData checkNthDevice(int i) { string log = "Scan for I²C Address: 0x" + i.ToString("X2") + Environment.NewLine + i.ToString("X2"); I2CData scanOutput = readAddressOnNthDevice(i); if (scanOutput != null) { log += scanOutput.log; if (scanOutput.error == I2CError.Success) { log += "I²C device found at address 0x" + i.ToString("X2"); log += "I²C Success" + Environment.NewLine + i.ToString("X2"); } else { log += "I²C device not found"; } } return(scanOutput); }
private void writeTextNdef(object sender, EventArgs e) { if (this.myDevice.isPresent) { this.NDEFtext = this.textNdef_textBox.Text; byte[] NDEFmessage = Encoding.UTF8.GetBytes(this.NDEFtext.ToCharArray()); this.isNDEFbuttonpressed = true; //this.CreateNdefText(Encoding.UTF8.GetBytes(this.NDEFtext.ToCharArray()), out NDEFmessage); byte i2cAddress = Utils.HexToByte(this.addressInput_TxtB.Text); I2CData result = this.myDevice.writeNdef(i2cAddress, NDEFmessage); this.logging.Text = result.log; if (result.error == I2CError.Success) { this.updateTablesWithNdef(result.data); } this.operationStatus_Lbl.Text = this.GetTextError(result.error); dgvUtils.markTlvs(type); } else { Utils.showDevLostAndClose(this); } }
// When the Write button in the default NDEF groupbox is pressed, the form is closed private void writeDefaultNdef(object sender, EventArgs e) { if (utils == null) { utils = new NDEFUtils(this.myDevice); } byte i2cAddress = Utils.HexToByte(this.addressInput_TxtB.Text); byte[] NDEFmessage; this.isNDEFbuttonpressed = true; this.NDEFtext = this.textNdef_textBox.Text; if (this.myDevice.isPresent) { utils.CreateDefaultNdef(out NDEFmessage); // Write the message into the tag and update the data grid views I2CData result = this.myDevice.writeMessage(i2cAddress, NDEFmessage); this.logging.Text = result.log; if (result.error == I2CError.Success) { this.updateTablesWithNdef(NDEFmessage); } // Update error status bar this.operationStatus_Lbl.Text = this.GetTextError(result.error); dgvUtils.markTlvs(type); } else { Utils.showDevLostAndClose(this); } }
/// <summary> /// Send i2c data /// </summary> /// <param name="data"></param> /// <param name="readLength"></param> /// <returns></returns> public byte[] SendI2C(byte[] writedata, byte readLength) { try { I2CData data = new I2CData(); byte[] data2 = new byte[writedata.Length - 1]; for (int i = 1; i < writedata.Length; i++) { data2[i - 1] = writedata[i]; } // check if it is read only package if (IsReadOnly(writedata) && readLength > 0) { data.IsWrite = false; } else { data.IsWrite = true; } data.Content = data2; data.ReadDataLength = readLength; data.IsRead = readLength > 0; data.Address = (byte)(writedata[0] >> 1); data.Format = GetFormat(); mData = data; UpdateGUIFromData(mData.Content); ctlI2CAddress1.Addr7 = mData.Address; chkRead.Checked = mData.IsRead; chkWrite.Checked = mData.IsWrite; numReadLength.Value = mData.ReadDataLength; UpdateData(); if (chkWrite.Checked) { StringBuilder sb = new StringBuilder(); sb.Append("W:"); string format = GetFormat() == emViewFormat.Hex ? "{0:X2} " : "{0:d} "; sb.AppendFormat(format, ctlI2CAddress1.Addr7 * 2); for (int i = 0; i < mData.Content.Length; i++) { sb.AppendFormat(format, mData.Content[i]); } if (cmbLogDataType.SelectedIndex == 0) { LogText(sb.ToString()); } if (!mBridge.Write((byte)numPort.Value, mData.Address, mData.Content)) { //OnWriteDataError(); throw new Exception("On Write Data Error"); } } if (chkRead.Checked) { try { byte[] readData = mBridge.ReadData((byte)numPort.Value, ctlI2CAddress1.Addr7, (byte)numReadLength.Value); if (IsFail(readData)) { LogText("Read Data Fail"); } else if (readData != null && cmbLogDataType.SelectedIndex == 0) { StringBuilder sb = new StringBuilder(); string format = GetFormat() == emViewFormat.Hex ? "{0:X2} " : "{0:d} "; sb.Append("W:"); sb.AppendFormat(format, ctlI2CAddress1.Addr7 * 2 + 1); sb.AppendLine(); sb.Append("R:"); for (int i = 0; i < readData.Length; i++) { sb.AppendFormat(format, readData[i]); } LogText(sb.ToString()); return(readData); } } catch (Exception ex) { //OnReadDataError(); throw new Exception("On Read Data Error"); } } } catch (Exception ex) { LogText(ex.Message); } return(null); }
private void setFlag(I2CData read) { this.flag = read.flag; }
public frmI2CDataEdit(I2CData data, frmMain parent) { InitializeComponent(); mData = data; mParent = parent; }