Python編寫Phidget Relay控制例子

來源:互聯網
上載者:User

這幾天收拾的時候發現了Phidget,這還是上一個案子客戶給的。型號是1014_2,記得是當時拿來做壓力測試用的。
過太久都忘了怎麼用了,所以再拾遺一下

因為重裝Ubuntu 16.04的系統,所以就從頭裝一遍phidget在Linux平台上的運行環境。
可參考Phidget官網,這裡只是大體說一下。

Linux環境的搭建

安裝libusb的開發庫

sudo apt-get install libusb-1.0-0-dev

安裝Phidget庫

點擊下載路徑下載Phidget庫。

解壓後,依次執行:

./configure
make
sudo make install

檢查Phidget庫的安裝

Software:

下載官方提供的樣本

解壓後編譯HelloWorld.c

gcc HelloWorld.c -o HelloWorld -lphidget21

運行HelloWorld


jasper@jasper:~/phidget/phidget21-c-examples-2.1.8.20151217$ sudo ./HelloWorld
Opening...
Phidget Simple Playground (plug and unplug devices)
Press Enter to end anytime...
Hello Device Phidget InterfaceKit 0/0/4, Serial Number: 381507
Goodbye Device Phidget InterfaceKit 0/0/4, Serial Number: 381507

Closing...

正常情況下,伴隨著phidget裝置的插入/拔出,能對應的看到Hello/Goodbye的資訊。
Hardware:

使用kernal log查看插入/拔出資訊:
插入:


jasper@jasper:~$ dmesg | tail
......
[17239.182460] usb 2-1.4: new low-speed USB device number 13 using ehci-pci
......
拔出:


jasper@jasper:~$ dmesg | tail
......
[17262.852520] usb 2-1.4: USB disconnect, device number 13
......
正常情況下插入時用到ehci-pci的device number會在拔出時disconnect掉。
Phidget的python套件安裝
下載並解壓phidget的python套件
1

jasper@jasper:~/phidget/PhidgetsPython$ sudo python setup.py install
可以下載官方樣本驗證。
至此,Phidget在Linux平台上的Python環境就安裝好了。
Phidget的使用步驟
Python API可參考官方API文檔
Step 1:裝置的初始化和開啟

self.device = InterfaceKit()
self.device.openPhidget()

Step 2:等待phidget裝置的接入

self.device.waitForAttach(10000)

Step 3:對裝置的操作

self.device.setOutputState(output, 1)

Step 4:裝置的關閉

self.device.closePhidget()

實際操作

手頭材料

phidget板: 型號是1014_2(實際上就是4個relay),能做的無非就是當作開關控制線路。
小米電風扇: 小米官網可購,19塊好像。
USB線: 延長線更好,能省去不少麻煩。
Micro USB線: 10年以後的Phidget 1014_2採用了Micro USB串連。
實操
因為只是為了控制電風扇的開關,所以USB的四條線我們只用電源正級和接地就好,因為用不到資料部分,所以D+/D-就不用管了。
將USB小的那頭剪去,並扯出紅線和黑線。將紅線剪下一半。然後按照下圖串連Phidget和風扇就好。這裡在連風扇的USB時很麻煩,有延長線的話就簡單多了,但是我這裡就一條,捨不得浪費….接風扇的時候連USB長的那兩條即可,中間那兩個是資料轉送用的。

連好後的總體效果如圖:

控制碼如下:


import time

from Phidgets.PhidgetException import *
from Phidgets.Devices.InterfaceKit import *

class TestPhidget(object):
 def __init__(self):
  pass

 def __enter__(self):
  try:
   self.device = InterfaceKit()
   self.device.openPhidget()
   self.device.waitForAttach(10000)
   return self
  except PhidgetException, e:
   exit(1)

 def relayOn(self, output):
  self.device.setOutputState(output, 1)
  time.sleep(1)

 def relayOff(self, output):
  self.device.setOutputState(output, 0)
  time.sleep(1)

 def __exit__(self, e_t, e_v, t_b):
  try:
   self.device.closePhidget()
  except PhidgetException, e:   
   exit(1)

def test():
 import optparse

 parser = optparse.OptionParser()
 parser.add_option("-n",
  dest = "counts",
  action = "store",
  help = "Number of test counts"
 )
   
 (options, args) = parser.parse_args()
 counts = int(options.counts)

 with TestPhidget() as testphidget:
  time.sleep(1)
  for i in range(0, counts):
   testphidget.relayOff(3)
   time.sleep(7)
   testphidget.relayOn(3)
   time.sleep(5)

if __name__ == '__main__':
 test()
這些統統可以在官方API文檔裡找得到。
效果就是控制風扇的開關。僅此而已(好無聊@@)
結語
想瞭解更多Phidget的資訊可以查看官方網址。
想要瞭解1014_2原理的可以參考這裡,只要你學過高中物理,我覺得理解應該沒有問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.