Пример #1
0
        // TODO: Merge common functionality between KeyboardWedgeRead and ReadTags.ReadtheTags
        private void KeyboardWedgeRead()
        {
            List <SendInputWrapper.INPUT> inputList = new List <SendInputWrapper.INPUT>();
            string startIndicator = ".";

            // Signal start of read
            inputList.Clear();
            AddKeypresses(inputList, startIndicator);
            SendInput(inputList);

            properties = Utilities.GetProperties();

            SoundPlayer startSound = new SoundPlayer(properties["startscanwavefile"]);

            if (properties["audiblealert"].ToLower() == "yes")
            {
                startSound.Play();
            }
            SoundPlayer stopSound = new SoundPlayer(properties["endscanwavefile"]);

            stopSound.LoadAsync();

            CoreDLL.SYSTEM_POWER_STATUS_EX status = new CoreDLL.SYSTEM_POWER_STATUS_EX();
            //Check the battery power level
            if (CoreDLL.GetSystemPowerStatusEx(status, false) == 1)
            {
                if (status.BatteryLifePercent <= 5)
                {
                    if (status.ACLineStatus == 0)
                    {
                        MessageBox.Show("Battery level is low to read tags");
                        return;
                    }
                }
            }
            try
            {
                TagReadData[] reads;

                //Utilities.PowerManager.PowerNotify += new PowerManager.PowerEventHandler(PowerManager_PowerNotify);
                using (ThingMagic.RFIDSearchLight.ReadMgr.Session rsess = ThingMagic.RFIDSearchLight.ReadMgr.GetSession())
                {
#if DEBUG
                    if (properties["audiblealert"].ToLower() == "yes")
                    {
                        startSound.Play();
                    }
#endif

                    int radioPower = 0;
                    if (properties["readpower"].ToString() == "")
                    {
                        radioPower = 2300;//While reading read power should be max
                    }
                    else
                    {
                        radioPower = Convert.ToInt32(properties["readpower"].ToString());
                    }

                    //Set the region
                    string region = properties["region"];
                    try
                    {
                        Utilities.SwitchRegion(region);
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show(
                            "Unknown Region: " + region + "\r\n" +
                            "Please run RFIDSearchLight to initialize the region."
                            );
                    }

                    rsess.Reader.ParamSet("/reader/powerMode", Reader.PowerMode.FULL);
                    rsess.Reader.ParamSet("/reader/radio/readPower", radioPower);
                    rsess.Reader.ParamSet("/reader/antenna/txRxMap", new int[][] { new int[] { 1, 1, 1 } });
                    List <int> ant = new List <int>();
                    ant.Add(1);
                    //set the tag population settings
                    rsess.Reader.ParamSet("/reader/gen2/target", Gen2.Target.A);//default target
                    string tagPopulation = properties["tagpopulation"];
                    switch (tagPopulation)
                    {
                    case "small":
                        rsess.Reader.ParamSet("/reader/gen2/q", new Gen2.StaticQ(2));
                        rsess.Reader.ParamSet("/reader/gen2/session", Gen2.Session.S0);
                        rsess.Reader.ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M4);
                        break;

                    case "medium":
                        rsess.Reader.ParamSet("/reader/gen2/q", new Gen2.StaticQ(4));
                        rsess.Reader.ParamSet("/reader/gen2/session", Gen2.Session.S1);
                        rsess.Reader.ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M4);
                        break;

                    case "large":
                        rsess.Reader.ParamSet("/reader/gen2/q", new Gen2.StaticQ(6));
                        rsess.Reader.ParamSet("/reader/gen2/session", Gen2.Session.S2);
                        rsess.Reader.ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M2);
                        break;

                    default: break;
                    }
                    //set the read plan and filter
                    TagFilter filter;
                    int       addressToRead = int.Parse(properties["selectionaddress"]);
                    Gen2.Bank bank          = Gen2.Bank.EPC;
                    switch (properties["tagselection"].ToLower())
                    {
                    case "None":
                    case "epc": bank = Gen2.Bank.EPC; break;

                    case "tid": bank = Gen2.Bank.TID; break;

                    case "user": bank = Gen2.Bank.USER; break;

                    case "reserved": bank = Gen2.Bank.RESERVED; break;

                    default: break;
                    }
                    if ("yes" == properties["ismaskselected"])
                    {
                        filter = new Gen2.Select(true, bank, (uint)addressToRead * 8, (ushort)(properties["selectionmask"].Length * 4), ByteFormat.FromHex(properties["selectionmask"]));
                    }
                    else
                    {
                        filter = new Gen2.Select(false, bank, (uint)addressToRead * 8, (ushort)(properties["selectionmask"].Length * 4), ByteFormat.FromHex(properties["selectionmask"]));
                    }
                    //set the read plan
                    SimpleReadPlan srp;
                    if (properties["tagselection"].ToLower() == "none")
                    {
                        srp = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2, null, 0);
                    }
                    else
                    {
                        srp = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2, filter, 0);
                    }
                    rsess.Reader.ParamSet("/reader/read/plan", srp);

                    double readDuration = Convert.ToDouble(properties["scanduration"].ToString()) * 1000;
                    int    readTimeout  = Convert.ToInt32(readDuration);

                    //Do a sync read for the readduration
#if DEBUG
                    if (properties["audiblealert"].ToLower() == "yes")
                    {
                        startSound.Play();
                    }
#endif
                    reads = rsess.Reader.Read(readTimeout);

                    rsess.Reader.ParamSet("/reader/powerMode", Reader.PowerMode.MINSAVE);
                    if (properties["audiblealert"].ToLower() == "yes")
                    {
                        stopSound.Play();
                    }
                    // Clear start indicator
                    inputList.Clear();
                    for (int i = 0; i < startIndicator.Length; i++)
                    {
                        AddKeypresses(inputList, new byte[] {
                            // Don't send Backspace -- that's one of our hotkeys,
                            // so it'll put us in an infinite loop.
                            (byte)Keys.Left,
                            (byte)Keys.Delete,
                        });
                    }
                    SendInput(inputList);
                }

                inputList.Clear();
                //HideWindow();
                bool     timestamp = false, rssi = false, position = false;
                string[] metadata = properties["metadatatodisplay"].Split(',');
                //Metadata boolean variables
                foreach (string mdata in metadata)
                {
                    switch (mdata.ToLower())
                    {
                    case "timestamp": timestamp = true; break;

                    case "rssi": rssi = true; break;

                    case "position": position = true; break;

                    default: break;
                    }
                }
                string metadataseparator       = properties["metadataseparator"];
                byte   metadataseparatorInByte = 0x00;
                switch (metadataseparator.ToLower())
                {
                //The byte representation of special characters can be found here - http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
                case "comma": metadataseparatorInByte = 0xBC; break;

                case "space": metadataseparatorInByte = 0x20; break;

                case "enter": metadataseparatorInByte = 0x0D; break;

                case "tab": metadataseparatorInByte = 0x09; break;

                default: break;
                }
                //Print the epc in caps
                AddKeypress(inputList, (byte)Keys.Capital);  // Toggle Caps Lock

                //Print the tag reads
                foreach (TagReadData dat in reads)
                {
                    string tagData = string.Empty;
                    string epc     = string.Empty;
                    if (properties["displayformat"] == "base36")
                    {
                        epc = ConvertEPC.ConvertHexToBase36(dat.EpcString);
                    }
                    else
                    {
                        epc = dat.EpcString;
                    }

                    AddKeypresses(inputList, properties["prefix"].ToUpper());
                    AddKeypresses(inputList, epc.ToUpper());
                    AddKeypresses(inputList, properties["suffix"].ToUpper());

                    if (timestamp)
                    {
                        AddKeypress(inputList, metadataseparatorInByte);
                        AddKeypresses(inputList, dat.Time.ToString("yyyy-MM-dd-HH-mm-ss"));
                    }
                    if (rssi)
                    {
                        AddKeypress(inputList, metadataseparatorInByte);
                        AddKeypresses(inputList, dat.Rssi.ToString());
                    }
                    if (position)
                    {
                        AddKeypress(inputList, metadataseparatorInByte);
                        AddKeypresses(inputList, GpsMgr.LatLonString);
                    }

                    switch (properties["multipletagseparator"].ToLower())
                    {
                    case "comma": AddKeypress(inputList, 0xBC); break;

                    case "space": AddKeypress(inputList, 0x20); break;

                    case "enter": AddKeypress(inputList, 0x0D); break;

                    case "tab": AddKeypress(inputList, 0x09); break;

                    case "pipe":
                        AddInput(inputList, SHIFT_DOWN);
                        AddKeypress(inputList, 0xDC);
                        AddInput(inputList, SHIFT_UP);
                        break;

                    default: break;
                    }
                    // Send keystrokes after each tag read record -- input buffer
                    // isn't big enough to hold more than a few lines
                    SendInput(inputList);
                    inputList.Clear();
                }
                //Turn caps lock back off
                AddKeypress(inputList, (byte)Keys.Capital);  // Toggle Caps Lock
                SendInput(inputList);
            }

            catch (Exception ex)
            {
                logger.Error("In KeyboardWedgeRead(): " + ex.ToString());
                //MessageBox.Show(ex.Message);
                //Debug.Log(ex.ToString());
            }
        }
Пример #2
0
        private void ReadtheTags()
        {
            try
            {
                // Make sure reader is connected
                ReadMgr.GetReader();

                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

                if (btnStartReads.Text == "Start Reads")
                {
                    Cursor.Current = Cursors.WaitCursor;
                    try
                    {
                        //Check the battery power level
                        if (CoreDLL.GetSystemPowerStatusEx(status, false) == 1)
                        {
                            if (status.BatteryLifePercent <= 5)
                            {
                                if (status.ACLineStatus == 0)
                                {
                                    MessageBox.Show("Battery level is low to read tags");
                                    return;
                                }
                            }
                        }
                        properties["isreading"] = "yes";
                        Utilities.SaveConfigurations(properties);

                        //disable read power coverage
                        tbTXPower.Enabled = false;

                        ReadMgr.GetReader().ParamSet("/reader/transportTimeout", 2000);
                        int powerLevel = Convert.ToInt32(properties["readpower"]);
                        ReadMgr.GetReader().ParamSet("/reader/radio/readPower", powerLevel);
                        Utilities.SwitchRegion(properties["region"]);
                        ReadMgr.GetReader().ParamSet("/reader/antenna/txRxMap", new int[][] { new int[] { 1, 1, 1 } });
                        ant.Add(1);
                        SimpleReadPlan plan = new SimpleReadPlan(ant.ToArray(), TagProtocol.GEN2);
                        ReadMgr.GetReader().ParamSet("/reader/read/plan", plan);
                        //int readPower = Convert.ToInt32(properties["readpower"].ToString()) * 100;
                        //tbTXPower.Value = (readPower - 1000) / 50;

                        tmrBackLightControl.Enabled = true;
                        miGoToMain.Enabled          = false;

                        //set properties
                        ReadMgr.GetReader().ParamSet("/reader/read/asyncOffTime", 50);
                        ReadMgr.GetReader().ParamSet("/reader/powerMode", Reader.PowerMode.FULL);

                        //set the tag population settings
                        ReadMgr.GetReader().ParamSet("/reader/gen2/target", Gen2.Target.A);//default target
                        string tagPopulation = properties["tagpopulation"];
                        switch (tagPopulation)
                        {
                        case "small":
                            ReadMgr.GetReader().ParamSet("/reader/gen2/q", new Gen2.StaticQ(2));
                            ReadMgr.GetReader().ParamSet("/reader/gen2/session", Gen2.Session.S0);
                            ReadMgr.GetReader().ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M4);
                            break;

                        case "medium":
                            ReadMgr.GetReader().ParamSet("/reader/gen2/q", new Gen2.StaticQ(4));
                            ReadMgr.GetReader().ParamSet("/reader/gen2/session", Gen2.Session.S1);
                            ReadMgr.GetReader().ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M4);
                            break;

                        case "large":
                            ReadMgr.GetReader().ParamSet("/reader/gen2/q", new Gen2.StaticQ(6));
                            ReadMgr.GetReader().ParamSet("/reader/gen2/session", Gen2.Session.S1);
                            ReadMgr.GetReader().ParamSet("/reader/gen2/tagEncoding", Gen2.TagEncoding.M2);
                            break;

                        default: break;
                        }

                        if (null != properties)
                        {
                            Utilities.SetReaderSettings(ReadMgr.GetReader(), properties);
                        }
                        else
                        {
                            MessageBox.Show("properties are null");
                        }
                        //set the read plan and filter
                        TagFilter filter;
                        int       addressToRead = int.Parse(properties["selectionaddress"]);
                        Gen2.Bank bank          = Gen2.Bank.EPC;
                        switch (properties["tagselection"].ToLower())
                        {
                        case "None":
                        case "epc": bank = Gen2.Bank.EPC; break;

                        case "tid": bank = Gen2.Bank.TID; break;

                        case "user": bank = Gen2.Bank.USER; break;

                        case "reserved": bank = Gen2.Bank.RESERVED; break;

                        default: break;
                        }
                        if ("yes" == properties["ismaskselected"])
                        {
                            filter = new Gen2.Select(true, bank, (uint)addressToRead * 8, (ushort)(properties["selectionmask"].Length * 4), ByteFormat.FromHex(properties["selectionmask"]));
                        }
                        else
                        {
                            filter = new Gen2.Select(false, bank, (uint)addressToRead * 8, (ushort)(properties["selectionmask"].Length * 4), ByteFormat.FromHex(properties["selectionmask"]));
                        }

                        SimpleReadPlan srp;
                        if (properties["tagselection"].ToLower() == "none")
                        {
                            srp = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2, null, 0);
                        }
                        else
                        {
                            srp = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2, filter, 0);
                        }
                        ReadMgr.GetReader().ParamSet("/reader/read/plan", srp);

                        btnStartReads.Text = "Stop Reads";
                        setStatus("Reading", System.Drawing.Color.DarkGoldenrod);
                        ReadMgr.GetReader().ReadException += ReadException;
                        ReadMgr.GetReader().TagRead       += PrintTagRead;
                        ReadMgr.GetReader().StartReading();
                        if (properties["audiblealert"].ToLower() == "yes")
                        {
                            if (readTriggeredByTap)
                            {
                                playStartSound();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        tbTXPower.Enabled = true;
                        //MessageBox.Show("Error connecting to reader: " + ex.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        btnStartReads.Text = "Start Reads";
                        setStatus(Status.IDLE);
                        ReadMgr.GetReader().ParamSet("/reader/powerMode", Reader.PowerMode.MAXSAVE);
                        miGoToMain.Enabled          = true;
                        tmrBackLightControl.Enabled = false;
                        properties["isreading"]     = "no";
                        Utilities.SaveConfigurations(properties);
                        throw ex;
                    }
                    finally
                    {
                        Cursor.Current = Cursors.Default;
                    }
                }
                else if (btnStartReads.Text == "Stop Reads")
                {
                    logger.Debug("Stop Reads pressed: Calling StopReads from ReadtheTags");
                    StopReads();
                    logger.Debug("Stop Reads pressed: Called StopReads from ReadtheTags");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (-1 != ex.Message.IndexOf("RFID reader was not found"))
                {
                    MessageBox.Show(ex.Message, "Error");
                }
                else
                {
                    btnStartReads.Text = "Start Reads";
                    setStatus(Status.IDLE);
                    properties["isreading"] = "no";
                    Utilities.SaveConfigurations(properties);
                    throw ex;
                }
            }
        }