blog community

Welcome to blog community Sign in | Join | Help
in Search

Erik Oppedijk

BizTalk, ASP.NET and Robotics Developer Studio

Phidgets RFID Service

To use the Phidgets RFID device with Robotics Developer Studio 2008, we first need to install the Phidgets drivers Phidget 21 MSI. (don't download the Robotics Studio drivers, there is no RFID support in there)

For the complete solution, download the PhidgetsRFID sample file(run DssProjectMigration first to update all references).

To start from scratch, create a new project and reference the Phidget21.NET.dll. Now we can attach the event handlers in the start event:

  protected override void Start()
        {
            base.Start();
            try
            {
                _state.Connected = false;
                rfid = new RFID(); //Declare an RFID object
                
                //initialize our Phidgets RFID reader and hook the event handlers
                rfid.Attach  += new AttachEventHandler(rfid_Attach);
                rfid.Detach  += new DetachEventHandler(rfid_Detach);
                rfid.Error   += new ErrorEventHandler(rfid_Error);
                rfid.Tag     += new TagEventHandler(rfid_Tag);
                rfid.TagLost += new TagEventHandler(rfid_TagLost);

                rfid.open();

                LogInfo(LogGroups.Console,"waiting for attachment..."); 
            }
            catch (PhidgetException ex)
            {
                LogError(null,ex.Description);
            }

Here is an example to catch an event and directly post it to the MainPort where the TagChangedHandler is called: 

     //Print the tag code of the scanned tag
        void rfid_Tag(object sender, TagEventArgs e)
        {
            TagChanged t = new TagChanged();
            t.Body.TagID = e.Tag;
            _mainPort.Post(t);
        }
        [ServiceHandler(ServiceHandlerBehavior.Exclusive)]
        public virtual IEnumerator<ITask> TagChangedHandler(TagChanged t)
        {
            if (_state.LastTag != t.Body.TagID)
            {
                _state.LastTag = t.Body.TagID;
                _state.LastDateTime = DateTime.Now;

                LogInfo(LogGroups.Console, string.Format("Tag {0} scanned", _state.LastTag));

                SendNotification<TagChanged>(_subMgrPort, t);
                t.ResponsePort.Post(_state);
            }
            yield break;
        }

And to store the date we need a State clss

    [DataContract()]
    public class PhidgetsRFIDState
    {
        [DataMember()]
        public bool Connected { get; set; }
        [DataMember()]
        public string SerialID { get; set; }
        [DataMember()]
        public string LastTag { get; set; }
        [DataMember()]
        public DateTime LastDateTime { get; set; }
    }

To let other service know that a Tag was read, we need a subscription manager, and a Subscribe method. In the TagChangedHandler the SendNotification is called to update all subscribers:

        [SubscriptionManagerPartner()]
        private SubscriptionManagerPort _subMgrPort = new SubscriptionManagerPort();


 [ServiceHandler(ServiceHandlerBehavior.Exclusive)]
        public virtual void SubscribeHandler(Subscribe s)
        {
            base.SubscribeHelper(_subMgrPort, s.Body, s.ResponsePort);
        }

back

Published Monday, August 04, 2008 11:02 AM by Erik Oppedijk
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems