DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码

POE 设置 IP

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

演示

脚本输出示例:
Command Line
1Found device with name: 192.168.1.136
2    -------------------------------------
3    "1" to set a static IPv4 address
4    "2" to set a dynamic IPv4 address
5    "3" to clear the config
6    1
7    -------------------------------------
8    Enter IPv4: 192.168.1.200
9    Enter IPv4 Mask: 255.255.255.0
10    Enter IPv4 Gateway: 192.168.1.1
11    Flashing static IPv4 192.168.1.200, mask 255.255.255.0, gateway 192.168.1.1 to the POE device. Enter 'y' to confirm. y
12    Flashing successful.
如果您在 10 秒后再次运行相同的示例,您将看到 IP 已更改为 192.168.1.200
Command Line
1Found device with name: 192.168.1.200
2    -------------------------------------
3    "1" to set a static IPv4 address
4    "2" to set a dynamic IPv4 address
5    "3" to clear the config
您还可以使用设备的 默认 IP 地址,如果未检测到 DHCP 服务器,它将恢复到该地址。有关此工作原理的更多详细信息,请参阅 PoE 部署指南中的 无 DHCP 部分。

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

Python

Python
GitHub
1import depthai as dai
2
3(found, info) = dai.DeviceBootloader.getFirstAvailableDevice()
4
5def check_str(s: str):
6    spl = s.split(".")
7    if len(spl) != 4:
8        raise ValueError(f"Entered value {s} doesn't contain 3 dots. Value has to be in the following format: '255.255.255.255'")
9    for num in spl:
10        if 255 < int(num):
11            raise ValueError("Entered values can't be above 255!")
12    return s
13
14if found:
15    print(f'Found device with name: {info.name}')
16    print('-------------------------------------')
17    print('"1" to set a static IPv4 address')
18    print('"2" to set a dynamic IPv4 address')
19    print('"3" to clear the config')
20    key = input('Enter the number: ').strip()
21    print('-------------------------------------')
22    if int(key) < 1 or 3 < int(key):
23        raise ValueError("Entered value should either be '1', '2' or '3'!")
24
25    with dai.DeviceBootloader(info) as bl:
26        if key in ['1', '2']:
27            ipv4 = check_str(input("Enter IPv4: ").strip())
28            mask = check_str(input("Enter IPv4 Mask: ").strip())
29            gateway = check_str(input("Enter IPv4 Gateway: ").strip())
30            mode = 'static' if key == '1' else 'dynamic'
31            val = input(f"Flashing {mode} IPv4 {ipv4}, mask {mask}, gateway {gateway} to the POE device. Enter 'y' to confirm. ").strip()
32            if val != 'y':
33                raise Exception("Flashing aborted.")
34
35            conf = dai.DeviceBootloader.Config()
36            if key == '1': conf.setStaticIPv4(ipv4, mask, gateway)
37            elif key == '2': conf.setDynamicIPv4(ipv4, mask, gateway)
38            (success, error) = bl.flashConfig(conf)
39        elif key == '3':
40            (success, error) = bl.flashConfigClear()
41
42        if not success:
43            print(f"Flashing failed: {error}")
44        else:
45            print(f"Flashing successful.")

C++

1#include <chrono>
2#include <iostream>
3#include <string>
4
5#include "depthai/depthai.hpp"
6#include "depthai/xlink/XLinkConnection.hpp"
7
8// for string delimiter
9std::vector<int> split(std::string s, std::string delimiter) {
10    size_t pos_start = 0, pos_end, delim_len = delimiter.length();
11    std::string token;
12    std::vector<int> res;
13
14    while((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
15        token = s.substr(pos_start, pos_end - pos_start);
16        pos_start = pos_end + delim_len;
17        res.push_back(stoi(token));
18    }
19    res.push_back(stoi(s.substr(pos_start)));
20    return res;
21}
22
23std::string checkStr(std::string str) {
24    std::vector<int> v = split(str, ".");
25    if(v.size() != 4) {
26        std::cout << "Entered value " << str << " doesn't contain 3 dots. Value has to be in the following format: '255.255.255.255'" << std::endl;
27        exit(0);
28    }
29    for(auto i : v) {
30        if(i < 0 || 255 < i) {
31            std::cout << "Entered values can't be above 255!" << std::endl;
32            exit(0);
33        }
34    }
35    return str;
36}
37
38int main(int argc, char** argv) {
39    bool found = false;
40    dai::DeviceInfo info;
41    std::tie(found, info) = dai::DeviceBootloader::getFirstAvailableDevice();
42    if(!found) {
43        std::cout << "No device found to flash. Exiting." << std::endl;
44        return -1;
45    }
46
47    std::cout << "Found device with name: " << info.getMxId() << std::endl;
48    std::cout << "-------------------------------------" << std::endl;
49    std::cout << "\"1\" to set a static IPv4 address" << std::endl;
50    std::cout << "\"2\" to set a dynamic IPv4 address" << std::endl;
51    std::cout << "\"3\" to clear the config" << std::endl;
52    auto key = std::cin.get();
53    std::cout << "-------------------------------------" << std::endl;
54
55    bool success = false;
56    std::string error;
57    dai::DeviceBootloader bl(info, true);
58    if(key == '1' || key == '2') {
59        std::string ip, mask, gateway, in;
60
61        std::cout << "Enter IPv4: ";
62        std::cin >> ip;
63        checkStr(ip);
64
65        std::cout << "Enter IPv4 Mask: ";
66        std::cin >> mask;
67        checkStr(mask);
68
69        std::cout << "Enter IPv4 Gateway: ";
70        std::cin >> gateway;
71        checkStr(gateway);
72
73        std::string mode = "static";
74        if(key == '2') mode = "dynamic";
75        std::cout << "Flashing " << mode << " IPv4 " << ip << ", mask " << mask << ", gateway " << gateway << " to the POE device. Enter 'y' to confirm. ";
76        std::cin >> in;
77        if(in != "y") {
78            std::cout << "Flashing aborted.";
79            return 0;
80        }
81        auto conf = dai::DeviceBootloader::Config();
82        if(key == '1') {
83            conf.setStaticIPv4(ip, mask, gateway);
84        } else {
85            conf.setDynamicIPv4(ip, mask, gateway);
86        }
87
88        std::tie(success, error) = bl.flashConfig(conf);
89    } else if(key == '3') {
90        std::tie(success, error) = bl.flashConfigClear();
91    } else {
92        std::cout << "Entered value should either be '1', '2' or '3'!";
93        return 0;
94    }
95
96    if(success) {
97        std::cout << "Flashing successful." << std::endl;
98    } else {
99        std::cout << "Flashing failed: " << error << std::endl;
100    }
101}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。