Python编写Phidget Relay控制
nc7372
8年前
<p>这几天收拾的时候发现了Phidget,这还是上一个案子客户给的。型号是1014_2,记得是当时拿来做压力测试用的。</p> <p>过太久都忘了怎么用了,所以再拾遗一下。</p> <p>因为重装 Ubuntu 16.04 的系统,所以就从头装一遍phidget在Linux平台上的运行环境。</p> <p>可参考 <a href="/misc/goto?guid=4959713351452330778" rel="nofollow,noindex">Phidget官网</a> ,这里只是大体说一下。</p> <h2>Linux环境的搭建</h2> <p>安装libusb的开发库</p> <pre> <code class="language-bash">sudo apt-get install libusb-1.0-0-dev </code></pre> <p>安装Phidget库</p> <p>点击 <a href="/misc/goto?guid=4959713351546188534" rel="nofollow,noindex">下载路径</a> 下载Phidget库。</p> <p>解压后,依次执行:</p> <pre> <code class="language-bash">./configure make sudo make install </code></pre> <h2>检查Phidget库的安装</h2> <p>Software:</p> <p>下载官方提供的 <a href="/misc/goto?guid=4959713351634542975" rel="nofollow,noindex">示例</a></p> <p>解压后编译 HelloWorld.c</p> <pre> <code class="language-bash">gcc HelloWorld.c -o HelloWorld -lphidget21 </code></pre> <p>运行HelloWorld</p> <pre> <code class="language-bash">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... </code></pre> <p>正常情况下,伴随着phidget设备的插入/拔出,能对应的看到Hello/Goodbye的信息。</p> <p>Hardware:</p> <p>使用 kernal log 查看插入/拔出信息:</p> <p>插入:</p> <pre> <code class="language-bash">jasper@jasper:~$ dmesg | tail ...... [17239.182460] usb 2-1.4: new low-speed USB device number 13 using ehci-pci ...... </code></pre> <p>拔出:</p> <pre> <code class="language-bash">jasper@jasper:~$ dmesg | tail ...... [17262.852520] usb 2-1.4: USB disconnect, device number 13 ...... </code></pre> <p>正常情况下插入时用到ehci-pci的device number会在拔出时disconnect掉。</p> <h2>Phidget的python套件安装</h2> <p>下载并解压phidget的 <a href="/misc/goto?guid=4959713351704366076" rel="nofollow,noindex">python套件</a></p> <pre> <code class="language-bash">jasper@jasper:~/phidget/PhidgetsPython$ sudo python setup.py install </code></pre> <p>可以下载 <a href="/misc/goto?guid=4959713351791724763" rel="nofollow,noindex">官方示例</a> 验证。</p> <p>至此,Phidget在Linux平台上的Python环境就安装好了。</p> <h2>Phidget的使用步骤</h2> <p>Python API可参考 <a href="/misc/goto?guid=4959713351871132782" rel="nofollow,noindex">官方API文档</a></p> <p>Step 1:设备的初始化和打开</p> <pre> <code class="language-bash">self.device = InterfaceKit() self.device.openPhidget() </code></pre> <p>Step 2:等待phidget设备的接入</p> <pre> <code class="language-bash">self.device.waitForAttach(10000) </code></pre> <p>Step 3:对设备的操作</p> <pre> <code class="language-bash">self.device.setOutputState(output, 1) </code></pre> <p>Step 4:设备的关闭</p> <pre> <code class="language-bash">self.device.closePhidget() </code></pre> <h2>实际操作</h2> <p>手头材料</p> <p>phidget板: 型号是1014_2(实际上就是4个relay),能做的无非就是当作开关控制线路。</p> <p>小米电风扇: 小米官网可购,19块好像。</p> <p>USB线: 延长线更好,能省去不少麻烦。</p> <p>Micro USB线: 10年以后的Phidget 1014_2采用了Micro USB连接。</p> <p>实操</p> <p>因为只是为了控制电风扇的开关,所以USB的四条线我们只用电源正级和接地就好,因为用不到数据部分,所以D+/D-就不用管了。</p> <p>将USB小的那头剪去,并扯出红线和黑线。将红线剪下一半。然后按照下图连接Phidget和风扇就好。这里在连风扇的USB时很麻烦,有延长线的话就简单多了,但是我这里就一条,舍不得浪费….接风扇的时候连USB长的那两条即可,中间那两个是数据传输用的。</p> <p><img src="https://simg.open-open.com/show/2e5fc4ebcac5f31e6791036f63f7dddf.jpg"></p> <p>连好后的总体效果如图:</p> <p><img src="https://simg.open-open.com/show/50f333a9e4cdfdc576934438d0fd94ce.jpg"></p> <p>控制代码如下:</p> <pre> <code class="language-python">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() </code></pre> <p>这些统统可以在 <a href="/misc/goto?guid=4959713351871132782" rel="nofollow,noindex">官方API文档</a> 里找得到。</p> <p>效果就是控制风扇的开关。仅此而已(好无聊@@)</p> <h2>结语</h2> <p>想了解更多Phidget的信息可以查看 <a href="/misc/goto?guid=4959713351964834045" rel="nofollow,noindex">官方网址</a> 。</p> <p>想要了解 1014_2 原理的可以参考 <a href="/misc/goto?guid=4959713352050552535" rel="nofollow,noindex">这里</a> ,只要你学过高中物理,我觉得理解应该没有问题。</p> <p> </p> <p>来自:http://4coding.cn/2016/08/30/tech/phidget/</p> <p> </p> <p><span style="background:rgb(189, 8, 28) url("data:image/svg+xml; border-radius:2px; border:medium none; color:rgb(255, 255, 255); cursor:pointer; display:none; font:bold 11px/20px "Helvetica Neue",Helvetica,sans-serif; left:30px; opacity:0.85; padding:0px 4px 0px 0px; position:absolute; text-align:center; text-indent:20px; top:3672px; width:auto; z-index:8675309">Save</span></p>