{"id":1926,"date":"2019-12-25T13:04:29","date_gmt":"2019-12-25T05:04:29","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1926"},"modified":"2020-05-12T14:06:34","modified_gmt":"2020-05-12T06:06:34","slug":"how-to-read-rfid-tags-from-sparkfun-rfid-usb-reader-with-python-3","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-read-rfid-tags-from-sparkfun-rfid-usb-reader-with-python-3\/","title":{"rendered":"How to read RFID tags from SparkFun RFID USB Reader with Python 3"},"content":{"rendered":"<p>When you have an <a href=\"https:\/\/www.amazon.com\/RFID-Starter-Kit-by-Sparkfun\/dp\/B00EPH81RS\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=753ea9a682203aa0d2ea21cb1fe522f3&language=en_US\" rel=\"noopener\" target=\"_blank\">RFID starter kit from Sparkfun<\/a>, you will be able to read RFID tags through serial.<\/p>\n<p>Previously, we saw <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-an-esp32-development-board-to-read-rfid-tags-from-a-sparkfun-rfid-usb-reader\/\" rel=\"noopener\" target=\"_blank\">how to use an ESP32 board to read RFID tags from a SparkFun RFID USB Reader<\/a>.<\/p>\n<p>Given that, I was able to build a ESP32 prototype to scan tag ids from RFID cards.<\/p>\n<p>In order for that ESP32 prototype to recognise what each of my tag card represents, I need to label the ids. Whenever my ESP32 prototype gets a tag id, it will query a tag catalogue to see what that tag id represent.<\/p>\n<p>Since it is easier to label the tag cards from a computer, I built a Python 3 application to read the RFID tags from the SparkFun RFID reader.<\/p>\n<p>So how we can read RFID tags from SparkFun RFID USB Reader with Python 3? <\/p>\n<p>If you are looking for a way to read RFID tags from SparkFun RFID USB Reader with Python 3, then this post is for you.<\/p>\n<h2>Connecting the SparkFun RFID USB reader to a computer<\/h2>\n<p>Before the application can read from the SparkFun RFID USB reader, we need to connect it to a computer with a <a href=\"https:\/\/www.amazon.com\/AmazonBasics-USB-2-0-Cable-Male\/dp\/B00NH11N5A\/\" rel=\"noopener\" target=\"_blank\">mini USB cable<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/SparkFun-RFID-USB-reader-connected-to-mini-USB-cable.jpg\" alt=\"SparkFun RFID USB reader connected to mini USB cable\" \/><\/p>\n<h3>Finding the path to the serial device on macOS<\/h3>\n<p>After I had connected the RFID reader to my MacBook, I started a terminal program and typed the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls \/dev\/tty.*\r\n<\/pre>\n<p>When the command returned an output, I found <strong>\/dev\/tty.usbserial-AB0JPNCG<\/strong> listed as one of the items.<\/p>\n<p>If you are using Linux, then you should be able to use the same command to find the path to the serial device.<\/p>\n<p>In case you are using Windows, use the device manager to find the COM port to communicate with your RFID reader. <\/p>\n<h2>Installing the Python 3 library to help read data from serial<\/h2>\n<p>After getting the serial port to read from, I proceeded to install the <a href=\"https:\/\/pyserial.readthedocs.io\/en\/latest\/index.html\" rel=\"noopener\" target=\"_blank\">PySerial library<\/a> into my Python 3 environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install pySerial\r\n<\/pre>\n<h2>Writing a sample Python 3 program to read RFID tags from SparkFun RFID USB Reader<\/h2>\n<p>Given that, I created the following Python 3 script and saved it as <strong><code>read-rfid.py<\/code><\/strong>:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport argparse\r\nimport serial\r\n\r\nif __name__ == '__main__':\r\n\r\n    parser = argparse.ArgumentParser()\r\n    parser.add_argument('--port', help='Serial port to read tags from', required=True)\r\n    args = parser.parse_args()\r\n\r\n    serial_port = args.port\r\n    with serial.Serial(serial_port, 9600) as rfid_reader:\r\n        while(True):\r\n            tag = rfid_reader.readline().decode('UTF-8').strip()\r\n            print(tag)\r\n\r\n<\/pre>\n<p>As shown above, I use the <a href=\"https:\/\/docs.python.org\/3\/library\/argparse.html\" rel=\"noopener\" target=\"_blank\">argparse library<\/a> to help me build a command line application.<\/p>\n<p>After importing the Python libraries, an <a href=\"https:\/\/docs.python.org\/3\/library\/argparse.html#argumentparser-objects\" rel=\"noopener\" target=\"_blank\">Argument Parser<\/a> object is created to read command line arguments. In this situation, I had created a way for the serial port path to be included as a command line argument.<\/p>\n<p>After getting the serial port into the <code>serial_port<\/code> variable, a <a href=\"https:\/\/pyserial.readthedocs.io\/en\/latest\/pyserial_api.html#serial.Serial\" rel=\"noopener\" target=\"_blank\">Serial object<\/a> is created.<\/p>\n<p>Once we have created the Serial object as <code>rfid_reader<\/code>, we get into an infinite loop to read from the serial port. <\/p>\n<p>Whenever the SparkFun RFID USB reader detects a tag id, <a href=\"https:\/\/pyserial.readthedocs.io\/en\/latest\/pyserial_api.html#serial.Serial.readline\" rel=\"noopener\" target=\"_blank\">rfid_reader.readline()<\/a> will unblock and return the tag id. Given that tag id, we decode it as an UTF-8 string, strip off trialing whitespaces and save the result into the <code>tag<\/code> variable. <\/p>\n<p>After we have a value for the tag variable, we print the tag id to standard output.<\/p>\n<h2>Running the application to read RFID tags from SparkFun RFID USB Reader<\/h2>\n<p>So how can I run this application?<\/p>\n<p>In order to start the application, I will run the following command in a terminal program: <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython read-rfid.py --port \/dev\/tty.usbserial-AB0JPNCG\r\n<\/pre>\n<p>As mentioned earlier, the application will get into an infinite loop. After that, any tag id read by the RFID reader will be printed to standard output.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-v4\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-v4&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-v4&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1926&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you have an <a href=\"https:\/\/www.amazon.com\/RFID-Starter-Kit-by-Sparkfun\/dp\/B00EPH81RS\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=753ea9a682203aa0d2ea21cb1fe522f3&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">RFID starter kit from Sparkfun<\/a>, you will be able to read RFID tags through serial.<\/p>\n<p>Previously, we saw <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-an-esp32-development-board-to-read-rfid-tags-from-a-sparkfun-rfid-usb-reader\/\" rel=\"noopener\" target=\"_blank\">how to use an ESP32 board to read RFID tags from a SparkFun RFID USB Reader<\/a>.<\/p>\n<p>Given that, I was able to build a ESP32 prototype to scan tag ids from RFID cards.<\/p>\n<p>In order for that ESP32 prototype to recognise what each of my tag card represents, I need to label the ids. Whenever my ESP32 prototype gets a tag id, it will query a tag catalogue to see what that tag id represent.<\/p>\n<p>Since it is easier to label the tag cards from a computer, I built a Python 3 application to read the RFID tags from the SparkFun RFID reader.<\/p>\n<p>So how we can read RFID tags from SparkFun RFID USB Reader with Python 3? <\/p>\n<p>If you are looking for a way to read RFID tags from SparkFun RFID USB Reader with Python 3, then this post is for you.<\/p>\n","protected":false},"author":1,"featured_media":1927,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[484,712,226,233,705,706],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/SparkFun-RFID-USB-reader-connected-to-mini-USB-cable.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-v4","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1926"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1926"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1926\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1927"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}