private static void Console_Execute_WriteBytes(DocInterfaceControl _docIntControl) { //WriteBytes function needs a Tag ID as parameter --> Obtained using "Identify" if (m_LastTagID == null) { Console.WriteLine("Perform \"Identify\" until a transponder is found before calling WriteBytes"); return; } //First make sure DocInterfaceControl is initialized if (_docIntControl != null) { if (_docIntControl.IsInitialized) { try { Console.WriteLine(""); Console.WriteLine("Writing \"00-11-22-33-44-55-66-77-88-99-AA-BB-CC-DD-EE-FF\" from position 0 (for UHF using page 3)"); byte[] toWrite = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; DateTime startTime = DateTime.UtcNow; //Call WriteBytes and show result // TagID --> m_LastTagID (contins first TagID found in last call to Identify) // Page --> 3 (for HF tags not needed, for UHF page 3 is user-block) // From --> 0 (first byte in memory) // Data --> "toWrite" array containing the data to be written // Lock --> false (memory address will not be locked. Lock process is not reversible) if (_docIntControl.WriteBytes(m_LastTagID, 3, 0, toWrite, false)) { Console.WriteLine("Data written successfully"); Console.WriteLine(string.Format("(Duration: {0})", DateTime.UtcNow - startTime)); } else { Console.WriteLine("\tNOT WRITTEN"); Console.WriteLine(string.Format("Result: FAIL. Duration: {0}", DateTime.UtcNow - startTime)); } } catch { Console.WriteLine("Exception"); } } else { Console.WriteLine("DocInterfaceControl not initialized!"); } } }
private void Button_WriteBytes_Click(object sender, RoutedEventArgs e) { if (comboBox_TagID.SelectedIndex == -1) { //For Writebytes the UID must be already read using "Identify" MessageBox.Show(this, "Identifier is needed --> Call Identify first"); return; } //Get parameters from Window int from = int.Parse(textBox_From.Text); int length = int.Parse(textBox_Length.Text); int page = int.Parse(textBox_Page.Text); byte[] tagID = iIDReaderLibrary.Utils.HelperFunctions.HexConverter.ToByteArray(comboBox_TagID.Text.Replace("-", " ")); //Initialize bytes to write => length defined in TextBox byte[] dataToWrite = new byte[length]; //Get data to write from TextBox & convert from HEX-String string[] aux = textBox_DataToWriteHex.Text.Split(new char[] { ' ', '-' }); if (aux.Length < length) { MessageBox.Show(this, "Not enough data available to write in TextBox"); return; } int j = 0; foreach (string str in aux) { if (str.Equals(" ")) { continue; } try { dataToWrite[j] = Convert.ToByte(str, 16); } catch { MessageBox.Show(this, "Error converting from HEX"); return; } j++; } if (m_DocInterface != null) { if (m_DocInterface.IsInitialized) { DateTime startTime = DateTime.UtcNow; try { textBox_ThreadLog.Text += "\n = WriteBytes =\n"; bool result = m_DocInterface.WriteBytes(tagID, page, from, dataToWrite, false); TimeSpan processSpan = DateTime.UtcNow - startTime; //Update result in UI string toLog = string.Format("Result: {0}. Duration: {1}\n", result ? "OK" : "FAIL", processSpan); if (result) { toLog += string.Format("\tPage: {0}\n", page); toLog += string.Format("\tStartByte: {0}, Length: {1}\n", from, length); toLog += string.Format(" DataWritten: {0}", BitConverter.ToString(dataToWrite)); } toLog += "\n"; textBox_ThreadLog.Text += toLog; textBox_ThreadLog.ScrollToEnd(); } catch (Exception ex) { TimeSpan processSpan = DateTime.UtcNow - startTime; textBox_ThreadLog.Text += string.Format("Result: Exception. Duration: {0}\n", processSpan); textBox_ThreadLog.ScrollToEnd(); System.Diagnostics.Debug.WriteLine(ex.ToString()); } } } }