Beispiel #1
0
        /// <summary>
        /// Handles the Click event of the killButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void killButton_Click(object sender, EventArgs e)
        {
            string         message   = "Tag killed successfully!";
            int            startTick = System.Environment.TickCount;
            MessageBoxIcon icon      = MessageBoxIcon.None;

            try
            {
                // Kill Tag
                uint   passwd         = uint.Parse(secPasswdTextBox.Text, System.Globalization.NumberStyles.HexNumber);
                byte   sBank          = (byte)targetBankCB.SelectedIndex;
                uint   sAddress       = (uint)targetStartUD.Value;
                int    sMaskBitLength = (int)targetLengthUD.Value;
                byte[] sMask          = NurApi.HexStringToBin(targetMaskTextBox.Text);
                uint   newPasswd      = uint.Parse(secPasswdTextBox.Text, System.Globalization.NumberStyles.HexNumber);;
                hNur.KillTag(passwd, sBank, sAddress, sMaskBitLength, sMask);
            }
            catch (NurApiException ex)
            {
                message = NurApiErrors.ErrorCodeToString(ex.error);
                icon    = MessageBoxIcon.Hand;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                icon    = MessageBoxIcon.Hand;
            }
            // Show result
            string title = string.Format("{0} {1} ms", Program.appName, System.Environment.TickCount - startTick);

            MessageBox.Show(message, title, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1);
        }
Beispiel #2
0
        /// <summary>
        /// Handles the Click event of the writeMemButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.Exception">
        /// mission impossible
        /// or
        /// Invalid parameter
        /// </exception>
        private void writeMemButton_Click(object sender, EventArgs e)
        {
            string         message   = "Memory written successfully!";
            int            startTick = System.Environment.TickCount;
            MessageBoxIcon icon      = MessageBoxIcon.None;

            try
            {
                // Write Tag memory
                uint   passwd         = uint.Parse(passwdTextBox.Text, System.Globalization.NumberStyles.HexNumber);
                bool   secured        = usePasswdCheckBox.Checked;
                byte   sBank          = (byte)targetBankCB.SelectedIndex;
                uint   sAddress       = (uint)targetStartUD.Value;
                int    sMaskBitLength = (int)targetLengthUD.Value;
                byte[] sMask          = NurApi.HexStringToBin(targetMaskTextBox.Text);
                byte   wrBank         = (Byte)memBankCB.SelectedIndex;
                uint   wrAddress      = (uint)memStartUD.Value;
                byte[] wrBuffer       = NurApi.HexStringToBin(memTextBox.Text);
                hNur.WriteSingulatedTag(passwd, secured, sBank, sAddress, sMaskBitLength, sMask, wrBank, wrAddress, wrBuffer);
            }
            catch (NurApiException ex)
            {
                message = NurApiErrors.ErrorCodeToString(ex.error);
                icon    = MessageBoxIcon.Hand;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                icon    = MessageBoxIcon.Hand;
            }
            // Show result
            string title = string.Format("{0} {1} ms", Program.appName, System.Environment.TickCount - startTick);

            MessageBox.Show(message, title, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1);
        }
Beispiel #3
0
        /// <summary>
        /// Handles the Click event of the setLockStateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void setLockStateButton_Click(object sender, EventArgs e)
        {
            string         message   = "Lock state(s) set successfully!";
            int            startTick = System.Environment.TickCount;
            MessageBoxIcon icon      = MessageBoxIcon.None;

            try
            {
                // Write Tag memory
                uint   passwd         = uint.Parse(passwdTextBox.Text, System.Globalization.NumberStyles.HexNumber);
                bool   secured        = usePasswdCheckBox.Checked;
                byte   sBank          = (byte)targetBankCB.SelectedIndex;
                uint   sAddress       = (uint)targetStartUD.Value;
                int    sMaskBitLength = (int)targetLengthUD.Value;
                byte[] sMask          = NurApi.HexStringToBin(targetMaskTextBox.Text);
                uint   memoryMask     = 0;
                if (lockUserCheckBox.Checked)
                {
                    memoryMask |= NurApi.LOCK_USERMEM;
                }
                if (lockTicCheckBox.Checked)
                {
                    memoryMask |= NurApi.LOCK_TIDMEM;
                }
                if (lockEpcCheckBox.Checked)
                {
                    memoryMask |= NurApi.LOCK_EPCMEM;
                }
                if (lockAccessCheckBox.Checked)
                {
                    memoryMask |= NurApi.LOCK_ACCESSPWD;
                }
                if (lockKillCheckBox.Checked)
                {
                    memoryMask |= NurApi.LOCK_KILLPWD;
                }
                uint action = (uint)secLockStateCB.SelectedIndex;
                hNur.SetLock(passwd, sBank, sAddress, sMaskBitLength, sMask, memoryMask, action);
            }
            catch (NurApiException ex)
            {
                message = NurApiErrors.ErrorCodeToString(ex.error);
                icon    = MessageBoxIcon.Hand;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                icon    = MessageBoxIcon.Hand;
            }
            // Show result
            string title = string.Format("{0} {1} ms", Program.appName, System.Environment.TickCount - startTick);

            MessageBox.Show(message, title, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1);
        }
Beispiel #4
0
        /// <summary>
        /// Handles the Click event of the readPasswordButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void readPasswordButton_Click(object sender, EventArgs e)
        {
            string         message   = "Password read successfully!";
            int            startTick = System.Environment.TickCount;
            MessageBoxIcon icon      = MessageBoxIcon.None;

            try
            {
                // Read memory from Tag
                uint   passwd         = uint.Parse(passwdTextBox.Text, System.Globalization.NumberStyles.HexNumber);
                bool   secured        = usePasswdCheckBox.Checked;
                byte   sBank          = (byte)targetBankCB.SelectedIndex;
                uint   sAddress       = (uint)targetStartUD.Value;
                int    sMaskBitLength = (int)targetLengthUD.Value;
                byte[] sMask          = NurApi.HexStringToBin(targetMaskTextBox.Text);
                uint   password;
                switch (securityCB.SelectedIndex)
                {
                case 0:
                    // Kill
                    password = hNur.GetKillPassword(passwd, secured, sBank, sAddress, sMaskBitLength, sMask);
                    break;

                case 1:
                    // Access
                    password = hNur.GetAccessPassword(passwd, secured, sBank, sAddress, sMaskBitLength, sMask);
                    break;

                default:
                    throw new Exception("Invalid parameter");
                }
                if (BitConverter.IsLittleEndian)
                {
                    password = ReverseBytes(password);
                }
                byte[] pwdBytes = BitConverter.GetBytes(password);
                secPasswdTextBox.Text = NurApi.BinToHexString(pwdBytes);
            }
            catch (NurApiException ex)
            {
                message = NurApiErrors.ErrorCodeToString(ex.error);
                icon    = MessageBoxIcon.Hand;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                icon    = MessageBoxIcon.Hand;
            }
            // Show result
            string title = string.Format("{0} {1} ms", Program.appName, System.Environment.TickCount - startTick);

            MessageBox.Show(message, title, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1);
        }
Beispiel #5
0
        /// <summary>
        /// Handles the Click event of the readMemButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void readMemButton_Click(object sender, EventArgs e)
        {
            string         message;
            int            startTick = System.Environment.TickCount;
            MessageBoxIcon icon      = MessageBoxIcon.None;

            try
            {
                // Read memory from Tag
                uint   passwd         = uint.Parse(passwdTextBox.Text, System.Globalization.NumberStyles.HexNumber);
                bool   secured        = usePasswdCheckBox.Checked;
                byte   sBank          = (byte)targetBankCB.SelectedIndex;
                uint   sAddress       = (uint)targetStartUD.Value;
                int    sMaskBitLength = (int)targetLengthUD.Value;
                byte[] sMask          = NurApi.HexStringToBin(targetMaskTextBox.Text);
                byte   rdBank         = (byte)memBankCB.SelectedIndex;
                uint   rdAddress      = (uint)memStartUD.Value;
                byte   rdByteCount    = (byte)memLengthUD.Value;
                byte[] data;
                if (memUseReadBlockCheckBox.Checked)
                {
                    data = NurRead.ReadBank(hNur, passwd, secured, sBank, sAddress, sMaskBitLength, sMask, rdBank, rdAddress, rdByteCount);
                }
                else
                {
                    data = hNur.ReadSingulatedTag(passwd, secured, sBank, sAddress, sMaskBitLength, sMask, rdBank, rdAddress, rdByteCount);
                }
                memTextBox.Text = NurApi.BinToHexString(data);
                message         = string.Format("{0} bytes read successfully \nin {1} ms.", data.Length, System.Environment.TickCount - startTick);
            }
            catch (NurApiException ex)
            {
                message = NurApiErrors.ErrorCodeToString(ex.error);
                icon    = MessageBoxIcon.Hand;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                icon    = MessageBoxIcon.Hand;
            }
            // Show result
            string title = string.Format("{0} {1} ms", Program.appName, System.Environment.TickCount - startTick);

            MessageBox.Show(message, title, MessageBoxButtons.OK, icon, MessageBoxDefaultButton.Button1);
        }
Beispiel #6
0
        /// <summary>
        /// Reads the part of the memory or whole bank.
        /// </summary>
        /// <param name="hNur">The hNur handler.</param>
        /// <param name="passwd">The access password.</param>
        /// <param name="secured">Use password if set to <c>true</c>.</param>
        /// <param name="sBank">The singulation bank.</param>
        /// <param name="sAddress">The singulation address (bits).</param>
        /// <param name="sMaskBitLength">Length of the singulation mask (bits).</param>
        /// <param name="sMask">The singulation mask.</param>
        /// <param name="rdBank">The read bank.</param>
        /// <param name="rdAddress">The read address (words).</param>
        /// <param name="rdByteCount">The rd byte count (bytes).</param>
        /// <returns></returns>
        static public byte[] ReadBank(NurApi hNur, uint passwd, bool secured, byte sBank, uint sAddress, int sMaskBitLength, byte[] sMask, byte rdBank, uint rdAddress, int rdByteCount)
        {
            int t1                 = System.Environment.TickCount;
            int rdNumOfBytes       = 0;
            int rdMaxNumOfBytes    = 100;
            int bankSizeSuggestion = 0;
            int outOfMemory        = Int32.MaxValue;

            byte[] tagIdentifier = null;

            if (rdByteCount == 0)
            {
                // rdByteCount is zero meaning read till the end
                // Get Tag Identifier
                if (sBank != NurApi.BANK_TID || sAddress != 0 || sMaskBitLength < 32)
                {
                    // Check if we already know the TagIdentifier
                    tagIdentifier = TargetCache.GetTagIdentifier(passwd, secured, sBank, sAddress, sMaskBitLength, sMask);
                    if (tagIdentifier == null)
                    {
                        // Unknown TAG type. Get E2, mask designer identifier (MDID)
                        // and Tag model number (TMN) from TID memory.
                        for (int retryTidCnt = 0; retryTidCnt < RETRIES; retryTidCnt++)
                        {
                            try
                            {
                                tagIdentifier = hNur.ReadSingulatedTag(passwd, secured, sBank, sAddress, sMaskBitLength, sMask, NurApi.BANK_TID, 0, 32 / 8);
                                if (new ByteArrayComparer().Equals(tagIdentifier, new byte[] { 0, 0, 0, 0 }))
                                {
                                    // Fix the alignment bug seen in the Fujitsu's tag
                                    tagIdentifier = hNur.ReadSingulatedTag(passwd, secured, sBank, sAddress, sMaskBitLength, sMask, NurApi.BANK_TID, 2, 32 / 8);
                                }
                                // Store the read TagIdentifier to the TargetCache for future use.
                                TargetCache.SetTagIdentifier(tagIdentifier, passwd, secured, sBank, sAddress, sMaskBitLength, sMask);
                                break;
                            }
                            catch (NurApiException)
                            {
                            }
                        }
                    }
                }
                else
                {
                    // We already know the Tag Identifier
                    // Just copy it.
                    tagIdentifier = new byte[32 / 8];
                    Array.Copy(sMask, tagIdentifier, 32 / 8);
                }

                // Get bank size suggestion
                int[] bankSize;
                if (tagIdentifier != null && bankSizeDictionary.TryGetValue(tagIdentifier, out bankSize))
                {
                    // Get known bank size suggestion
                    bankSizeSuggestion = bankSize[rdBank];
                }
                else
                {
                    // Get general purpose bank size suggestion
                    bankSizeSuggestion = generalPurposeBSS[rdBank];
                }
                rdNumOfBytes = (int)bankSizeSuggestion;
            }
            else
            {
                // Read fixed number of bytes
                rdNumOfBytes = rdByteCount;
            }

            NurApiException tempException = null;
            List <byte>     byteList      = new List <byte>();

            // Read Tag
            int retryCnt = 0;

            chancelReading = false;
            do
            {
                // Chancel reading if requested
                if (chancelReading)
                {
                    break;
                }

                // Are we reached the fixed number of bytes (rdByteCount > 0)
                if (rdByteCount > 0 &&
                    byteList.Count >= rdByteCount)
                {
                    break;
                }

                // First check the error / retry counter
                if (retryCnt > RETRIES)
                {
                    break;
                }

                // Calculate some usefull values
                int startAddress       = (int)rdAddress * 2;
                int suggestedBytesLeft = bankSizeSuggestion - startAddress;
                int outOfMemoryLeft    = outOfMemory - startAddress;
                int lastReadAddress    = rdNumOfBytes + startAddress;

                // rdNumOfBytes = suggested size
                if (suggestedBytesLeft < rdNumOfBytes && startAddress < bankSizeSuggestion)
                {
                    rdNumOfBytes = (int)suggestedBytesLeft;
                }

                // Make sure that the rdNumOfBytes < than latest outOfMemory
                if (outOfMemoryLeft - 4 <= rdNumOfBytes)
                {
                    rdNumOfBytes = (int)outOfMemoryLeft - 4;
                }

                // Make sure that the rdNumOfBytes is not bigget than latest known MaxNumOfBytes
                if (rdNumOfBytes > rdMaxNumOfBytes)
                {
                    rdNumOfBytes = rdMaxNumOfBytes;
                }

                // Last but not least, make sure that we read at least one word
                if (rdNumOfBytes < 2)
                {
                    rdNumOfBytes = 2;
                }

                // Make sure thet we don't read more than requested rdByteCount
                if (rdByteCount > 0 &&
                    byteList.Count + rdNumOfBytes > rdByteCount)
                {
                    rdNumOfBytes = rdByteCount - byteList.Count;
                }

                try
                {
                    // Read the content of the memory bank
                    byte[] bytes = null;
                    //RFID_Demo.Debug.WriteToFile(string.Format("Read addr:{0}, Bytes:{1}", rdAddress*2, rdNumOfBytes));
                    bytes = hNur.ReadSingulatedTag(passwd, secured, sBank, sAddress, sMaskBitLength, sMask, rdBank, rdAddress, rdNumOfBytes);
                    byteList.AddRange(bytes);
                    tempException = null;
                    retryCnt      = 0;
                }
                catch (NurApiException ex)
                {
                    //RFID_Demo.Debug.WriteToFile(string.Format("NurApiException {0}: {1}", ex.error, NurApiErrors.ErrorCodeToString(ex.error)));
                    tempException = ex;
                    if (ex.error == NurApiErrors.NUR_ERROR_G2_TAG_MEM_OVERRUN)
                    {
                        // Out of memory!!!
                        retryCnt    = 0;
                        outOfMemory = ((int)rdAddress * 2) + rdNumOfBytes;
                        if (rdNumOfBytes > 2)
                        {
                            // Read rest of mem by word by word.
                            rdNumOfBytes = 2;
                            continue;
                        }
                        // The end of memory reached. Stop reading and clear the exception.
                        tempException = null;
                        break;
                    }
                    else if (ex.error == NurApiErrors.NUR_ERROR_G2_READ)
                    {
                        // This may occur if we try to read too much or the connection is too weak
                        if (rdNumOfBytes == rdMaxNumOfBytes)
                        {
                            rdMaxNumOfBytes -= 4;
                        }
                        continue;
                    }

                    // Some other error occurred
                    System.Diagnostics.Debug.WriteLine(string.Format("Error {0}, {2}: {1}", ex.error, NurApiErrors.ErrorCodeToString(ex.error), rdNumOfBytes));
                    retryCnt++;
                    continue;
                }

                // Move word address to forward.
                rdAddress += (uint)rdNumOfBytes / 2;

                // If the suggested end of the memory is reached try to read only the next word
                if (bankSizeSuggestion == rdAddress * 2)
                {
                    rdNumOfBytes = 2;
                }

                // Try increase the reading speed if there is mode memory than suggested.
                if (bankSizeSuggestion < rdAddress * 2)
                {
                    rdNumOfBytes *= 2;
                }
            } while (true);

            // throw exception if exist
            if (tempException != null)
            {
                throw tempException;
            }

            if (rdByteCount == 0 && tagIdentifier != null)
            {
                // Update the bank size suggestion
                // Get bank size suggestion
                if (bankSizeDictionary.ContainsKey(tagIdentifier))
                {
                    bankSizeDictionary[tagIdentifier][rdBank] = byteList.Count;
                }
                else
                {
                    int[] newBSS = new int[generalPurposeBSS.Length];
                    generalPurposeBSS.CopyTo(newBSS, 0);
                    newBSS[rdBank] = byteList.Count;
                    bankSizeDictionary.Add(tagIdentifier, newBSS);
                }
            }
            System.Diagnostics.Debug.WriteLine(System.Environment.TickCount - t1);
            return(byteList.ToArray());
        }