Python編寫Phidget Relay控制
這幾天收拾的時候發現了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套件
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 原理的可以參考 這里 ,只要你學過高中物理,我覺得理解應該沒有問題。
來自:http://4coding.cn/2016/08/30/tech/phidget/