文章目录
  1. 1. Linux环境的搭建
    1. 1.0.0.1. 安装libusb的开发库
    2. 1.0.0.2. 安装Phidget库
  • 2. 检查Phidget库的安装
    1. 2.0.0.1. Software:
    2. 2.0.0.2. Hardware:
  • 3. Phidget的python套件安装
  • 4. Phidget的使用步骤
    1. 4.0.0.1. Step 1:设备的初始化和打开
    2. 4.0.0.2. Step 2:等待phidget设备的接入
    3. 4.0.0.3. Step 3:对设备的操作
    4. 4.0.0.4. Step 4:设备的关闭
  • 5. 实际操作
    1. 5.0.0.1. 手头材料
    2. 5.0.0.2. 实操
  • 6. 结语
  • 这几天收拾的时候发现了Phidget,这还是上一个案子客户给的。型号是1014_2,记得是当时拿来做压力测试用的。
    过太久都忘了怎么用了,所以再拾遗一下。
    因为重装Ubuntu 16.04的系统,所以就从头装一遍phidget在Linux平台上的运行环境。
    可参考Phidget官网,这里只是大体说一下。

    Linux环境的搭建

    安装libusb的开发库

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

    安装Phidget库

    点击下载路径下载Phidget库。
    解压后,依次执行:

    1
    2
    3
    ./configure 
    make
    sudo make install

    检查Phidget库的安装

    Software:

    下载官方提供的示例
    解压后编译HelloWorld.c

    1
    gcc HelloWorld.c -o HelloWorld -lphidget21

    运行HelloWorld

    1
    2
    3
    4
    5
    6
    7
    8
    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查看插入/拔出信息:
    插入:

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

    拔出:

    1
    2
    3
    4
    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:设备的初始化和打开

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

    Step 2:等待phidget设备的接入

    1
    self.device.waitForAttach(10000)

    Step 3:对设备的操作

    1
    self.device.setOutputState(output, 1)

    Step 4:设备的关闭

    1
    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长的那两条即可,中间那两个是数据传输用的。

    连好后的总体效果如图:

    控制代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    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原理的可以参考这里,只要你学过高中物理,我觉得理解应该没有问题。

    文章目录
    1. 1. Linux环境的搭建
      1. 1.0.0.1. 安装libusb的开发库
      2. 1.0.0.2. 安装Phidget库
  • 2. 检查Phidget库的安装
    1. 2.0.0.1. Software:
    2. 2.0.0.2. Hardware:
  • 3. Phidget的python套件安装
  • 4. Phidget的使用步骤
    1. 4.0.0.1. Step 1:设备的初始化和打开
    2. 4.0.0.2. Step 2:等待phidget设备的接入
    3. 4.0.0.3. Step 3:对设备的操作
    4. 4.0.0.4. Step 4:设备的关闭
  • 5. 实际操作
    1. 5.0.0.1. 手头材料
    2. 5.0.0.2. 实操
  • 6. 结语