3.4. 设置POE设备静态IP

此脚本允许您设置静态或动态IP,或清除OAK-POE设备上的引导加载程序配置。

3.4.1. 演示

示例脚本输出:

Found device with name: 192.168.1.136
-------------------------------------
"1" to set a static IPv4 address
"2" to set a dynamic IPv4 address
"3" to clear the config
1
-------------------------------------
Enter IPv4: 192.168.1.200
Enter IPv4 Mask: 255.255.255.0
Enter IPv4 Gateway: 255.255.255.255
Flashing static IPv4 192.168.1.200, mask 255.255.255.0, gateway 255.255.255.255 to the POE device. Enter 'y' to confirm. y
Flashing successful.

如果 10 秒后再次运行相同的示例,您将看到 IP 更改为192.168.1.200:

Found device with name: 192.168.1.200
-------------------------------------
"1" to set a static IPv4 address
"2" to set a dynamic IPv4 address
"3" to clear the config

3.4.2. 设置

请运行 安装依赖脚本 以下载所有必需的依赖项。请注意,此脚本必须在git上下文中运行,因此您必须先下载 depthai-python 存储库,然后再运行该脚本:

git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py

3.4.3. 源代码

可以在 GitHub 上找到。国内用户也可以在 gitee 上找到。

import depthai as dai

(found, info) = dai.DeviceBootloader.getFirstAvailableDevice()

def check_str(s: str):
    spl = s.split(".")
    if len(spl) != 4:
        raise ValueError(f"Entered value {s} doesn't contain 3 dots. Value has to be in the following format: '255.255.255.255'")
    for num in spl:
        if 255 < int(num):
            raise ValueError("Entered values can't be above 255!")
    return s

if found:
    print(f'Found device with name: {info.desc.name}')
    print('-------------------------------------')
    print('"1" to set a static IPv4 address')
    print('"2" to set a dynamic IPv4 address')
    print('"3" to clear the config')
    key = input('Enter the number: ').strip()
    print('-------------------------------------')
    if int(key) < 1 or 3 < int(key):
        raise ValueError("Entered value should either be '1', '2' or '3'!")

    with dai.DeviceBootloader(info) as bl:
        if key in ['1', '2']:
            ipv4 = check_str(input("Enter IPv4: ").strip())
            mask = check_str(input("Enter IPv4 Mask: ").strip())
            gateway = check_str(input("Enter IPv4 Gateway: ").strip())
            mode = 'static' if key == '1' else 'dynamic'
            val = input(f"Flashing {mode} IPv4 {ipv4}, mask {mask}, gateway {gateway} to the POE device. Enter 'y' to confirm. ").strip()
            if val != 'y':
                raise Exception("Flashing aborted.")

            conf = dai.DeviceBootloader.Config()
            if key == '1': conf.setStaticIPv4(ipv4, mask, gateway)
            elif key == '2': conf.setDynamicIPv4(ipv4, mask, gateway)
            (success, error) = bl.flashConfig(conf)
        elif key == '3':
            (success, error) = bl.flashConfigClear()

        if not success:
            print(f"Flashing failed: {error}")
        else:
            print(f"Flashing successful.")

有疑问?

我们很乐意为您提供代码或其他问题的帮助。

我们的联系方式

售后技术支持
oak_china_wechat

企业微信:OAK中国

售前技术和项目咨询
WeChat

微信号:13951940532

加好友请备注"OAK咨询"

欢迎到淘宝选购
taobao
OAK中国官方淘宝店

还可以通过我们发布的视频和文章了解OAK