Example #1
0
        public ReaderService(IReaderLowLevel reader)
        {
            _reader = reader;

            _reader.Initialize();

#if CHATTY_READER
            Trace.Listeners.Add(new TextWriterTraceListener(CHATTY_OUTPUT_FILE));
#endif

            /*
             * NOTE: from https://www.dataloggerinc.com/downloads/caenrfid/CAEN_RFID_API_UserMan_rev_04.pdf
             */
            /*
             * {
             *  double Gain = 8.0;
             *  double Loss = 1.5;
             *  double ERPPower = 1500.0;
             *  int OutPower;
             *  OutPower = (int)(ERPPower / Math.Pow(10, ((Gain - Loss - 2.14) / 10)));
             *  M3Client.reader.SetPower(OutPower);
             *
             *  Console.WriteLine("Set effective radiate power to {0} mW", ERPPower);
             * }
             * {
             *  double Gain = 8.0;
             *  double Loss = 1.5;
             *  double ERPPower;
             *  int OutPower;
             *  OutPower = M3Client.reader.GetPower();
             *  ERPPower = ((double)OutPower) * ((double)Math.Pow(10, ((Gain - Loss - 2.14) / 10)));
             *  Console.WriteLine("Current effective radiate power, mW: {0}", ERPPower);
             * }*/
        }
Example #2
0
        void MyWorkerThread_Read(object request)
        {
            if (!_reader.IsReady())
            {
                _reader.Initialize();
            }

            object mtag = null;

            // TODO: wait for a signal to cancel?
            // - how to implement such a signal?
            if (OverwriteStatus != null)
            {
                OverwriteStatus(this, new GenericEventArgs <string>("Singulating tag..."));
            }
            const int RETRIES = 4;

            {
                int ntries = 0;
                while (mtag == null && ntries < RETRIES) // AS-20160718: decreasing the retries greatly
                {
                    mtag = _reader.SingulateTag();
                    System.Threading.Thread.Sleep(100);
                    ntries++;
                }
            }
            if (mtag == null)
            {
                if (TagRead != null)
                {
                    TagRead(this, new GenericEventArgs <IReadResult>(new ReadResult("Unable to singulate tag!")));
                }
                return;
            }

            if (OverwriteStatus != null)
            {
                OverwriteStatus(this, new GenericEventArgs <string>("Tag singulated. Reading EPC..."));
            }

            string uri;
            var    res = DoWithRetries <string>(RETRIES, out uri, (out string uri0) =>
            {
                DecodeError err;

                // NOTE: when reading, be prepared to supply your password!
                // how to handle that??

                // try to make sense of the tag first
                TagEPCURI_SGTIN sgtin0;
                err = _reader.ReadEPC_SGTIN(mtag, out sgtin0);
                if (err == DecodeError.None)
                {
                    uri0 = sgtin0.Identity.URI;
                    return(err);
                }

                // try to fallback to raw URI
                TagRaw raw0;
                err = _reader.ReadEPC_Raw(mtag, out raw0);
                if (err == DecodeError.None)
                {
                    var sbuf = new StringBuilder();
                    raw0.GetURI(sbuf);
                    uri0 = sbuf.ToString();
                    return(err);
                }

                uri0 = null;
                return(err);
            });

            switch (res)
            {
            case DecodeError.IO:
                // could not read the EPC after the retries, bail out
                if (TagRead != null)
                {
                    TagRead(this, new GenericEventArgs <IReadResult>(new ReadResult("Unable to read EPC! Try again.")));
                }
                return;

            case DecodeError.Invalid:
                // invalid tag, have to use the GTIN provided by the server
                if (TagRead != null)
                {
                    TagRead(this, new GenericEventArgs <IReadResult>(new ReadResult("Unable to decode EPC (empty or invalid format)")));
                }
                return;

            case DecodeError.None:
                // good tag, let's roll
                break;
            }
            if (TagRead != null)
            {
                TagRead(this, new GenericEventArgs <IReadResult>(new ReadResult()
                {
                    URI = uri
                }));
            }
        }