{"id":3,"date":"2016-07-13T12:00:17","date_gmt":"2016-07-13T16:00:17","guid":{"rendered":"http:\/\/notebook.chaopricha.com\/?p=3"},"modified":"2016-10-27T00:03:19","modified_gmt":"2016-10-27T04:03:19","slug":"using-raspberry-pi-model-b-to-interface-to-ftdi-devices","status":"publish","type":"post","link":"https:\/\/notebook.chaopricha.com\/?p=3","title":{"rendered":"Using Raspberry Pi (Model B+) to interface to FTDI devices"},"content":{"rendered":"<p>I have always used Windows computers to interface to an FTDI based USB devices I built. Now due to the low cost and great availability of Raspberry Pi modules, it makes sense to see whether I could use a Raspberry Pi to talk to my FTDI based USB devices.<\/p>\n<p>I have several Raspberry Pi Model B+ available from previous projects working on cluster computing and from my general hoarding of Rasberry Pi modules.<\/p>\n<p>Googling the internet is the first thing I try to learn more about how to create an interface from Raspberry Pi to an FTDI device.<\/p>\n<p>I found 2 ways: 1) Use PySerial and 2) Use pyLibFTDI. I&#8217;ll talk about both methods since they are both useful.<\/p>\n<p>*Note: \u00a0I&#8217;ll use &#8216;$&#8217; in command line to show that it&#8217;s in the console terminal and if there&#8217;s no &#8216;$&#8217; then it&#8217;s a python code. \u00a0Don&#8217;t type $ in the console or the command won&#8217;t run.<\/p>\n<p><strong>PySerial Method<\/strong>:<br \/>\nThis method is pretty straight forward since Raspberry Pi should already come with PySerial installed. \u00a0If not, run the install with the command line:<\/p>\n<pre>$ sudo apt-get install python-serial<\/pre>\n<p>Once the installation is done, you can launch into python by typing<\/p>\n<pre>$ python<\/pre>\n<p>You can also write a python file and just run it using the command below where &lt;myfile&gt; is your file name. \u00a0There are many text editor in Raspberry Pi, but I normally use nano. \u00a0I&#8217;ll talk about this later when you have to create a rule file.<\/p>\n<pre>$ python &lt;myfile&gt;.py<\/pre>\n<p>In order to figure out all the serial devices attached to your Raspberry Pi, you can run the following codes in Python. \u00a0I&#8217;m using Python 2.7 by the way.<\/p>\n<pre>import glob\r\nports = glob.glob('\/dev\/tty[A-Za-z]*')\r\nfor port in ports:\r\n  print(port)<\/pre>\n<p>You might see a listing like this:<\/p>\n<pre>\/dev\/ttyUSB1\r\n\/dev\/ttyUSB0\r\n\/dev\/ttyprintk\r\n\/dev\/ttyAMA0<\/pre>\n<p>Now you can interface to your device using the proper named port from the list. \u00a0My USB device is the ttyUSB0. \u00a0The ttyAMA0 is the GPIO serial port that you can actually use to to communicate to Raspbery Pi via serial via your computer. \u00a0You can google more about using ttyAMA0 if you are curious since I won&#8217;t cover that here.<\/p>\n<pre>import serial\r\nser = serial.Serial (\"\/dev\/ttyUSB0\")    #Open named port \r\nser.baudrate = 9600                     #Set baud rate to 9600\r\nser.write('command\\r')                  #send command string \r\nser.read(ser.inWaiting())               #read data in read buffer\r\n#You could also use ser.readline() to read but need to know # lines.\r\nser.close()<\/pre>\n<p>You can refer to PySerial API website to get all the commands and various things you can do with PySerial.<\/p>\n<p>pyLibFTDI Method:<br \/>\nThis is another way to interface to a USB device but you need them to be an FTDI device to work. \u00a0The nice thing about this method is the speed and reliability of the communication due to the use of FTDI&#8217;s own driver to talk to the device. \u00a0You can even bitbang the port using this method. \u00a0I&#8217;m not covering that topic, but you can find more details on pyLibFTDI website.<\/p>\n<p>To use pyLibFTDI in Raspberry PI, you first need to install LibFTDI by running the following command:<\/p>\n<pre>$ sudo apt-get install libftdi-dev<\/pre>\n<p>After that is done, install pyLibFTDI. \u00a0You need PIP to install but I think that should already be available in\u00a0Raspbian. \u00a0I&#8217;m using Raspbian Jessie (2016-05-27).<\/p>\n<pre>$ sudo pip install pylibftdi<\/pre>\n<p>The thing to worry about is the port access permission. \u00a0You can&#8217;t really access the port unless you are a root account or use Sudo to send the command. \u00a0This is problematic if you want to be able to just run the interface from any account. \u00a0To address this issue, defining a new rule by creating one like this:<\/p>\n<pre> $ sudo nano \/etc\/udev\/rules.d\/99-libftdi.rules<\/pre>\n<p>Nano is my favorite editor on Raspberry Pi. Add these lines to the rule file then Ctrl+o (letter &#8216;O&#8217; not zero) to write the file then Ctrl+x to exit.<\/p>\n<pre>SUBSYSTEMS==\"usb\", ATTRS{idVendor}==\"0403\", ATTRS{idProduct}==\"6001\", GROUP=\"dialout\", MODE=\"0660\"\r\nSUBSYSTEMS==\"usb\", ATTRS{idVendor}==\"0403\", ATTRS{idProduct}==\"6014\", GROUP=\"dialout\", MODE=\"0660\"<\/pre>\n<p>You want to reload the rules so that this new rules take effect.<\/p>\n<pre># Reload rules after edit.\r\n$ udevadm control --reload-rules&lt;\\pre&gt;<\/pre>\n<p>Now you are ready to try pyLibFTDI. Try listing the devices available by typing in console (not in python):<\/p>\n<pre>$ python -m pylibftdi.examples.list_devices<\/pre>\n<p>It should list the device(s) assuming you have FTDI device(s) connected to the Raspberry Pi. If you see an empty listing like<\/p>\n<pre>::<\/pre>\n<p>There&#8217;s a permission problem to access the port. Restarting Raspberry Pi seems to help to reload the rules when the reload rules command doesn&#8217;t work.<\/p>\n<p>Now we can test pyLibFTDI in python. If you have listed your devices, you can use the device identity to talk to it. \u00a0For example, when I ran the list_devices example, I have<\/p>\n<pre>FTDI:FT232R USB UART:AK003XYT\r\nFTDI:FT232R USB UART:A603X5C1<\/pre>\n<p>These are the 2 FTDI USB devices I have attached to my Raspberry Pi. \u00a0I&#8217;ll try to talk to the first one.<\/p>\n<pre>from pylibftdi import Device\r\ndevice = Device('AK003XYN')\r\ndevice.baudrate = 38400\r\ndevice.open()\r\ndevice.write('?\\r')             #send an identity query command.\r\n                                #This device needs \\r to end command.\r\ndevice.readlines()              #Return data in buffer\r\ndevice.close()                  #Close the device<\/pre>\n<p>These two methods to interface to your FTDI devices should get you started on having an ability to talk to your serial port devices without the need of a Windows based computer. \u00a0Enjoy and happy building and coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have always used Windows computers to interface to an FTDI based USB devices I built. Now due to the low cost and great availability of Raspberry Pi modules, it makes sense to see whether I could use a Raspberry &hellip; <a href=\"https:\/\/notebook.chaopricha.com\/?p=3\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/3"}],"collection":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3"}],"version-history":[{"count":4,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/3\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/3\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}