一、给树莓派安装Debian12或者Ubuntu22.04操作系统(兼容即可)

debian换源示例

sudo nano /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

# deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware

二、安装 EtherCAT Master

添加 GPG 密钥

export KEYRING=/usr/share/keyrings/etherlab.gpg
curl -fsSL https://download.opensuse.org/repositories/science:/EtherLab/Debian_12/Release.key | gpg --dearmor | sudo tee "$KEYRING" >/dev/null

添加软件源

echo "deb [signed-by=$KEYRING] https://download.opensuse.org/repositories/science:/EtherLab/Debian_12/ ./" | sudo tee /etc/apt/sources.list.d/etherlab.list > /dev/null

更新包列表并安装 EtherCAT

sudo apt update
sudo apt install ethercat-master libethercat-dev

三、配置 EtherCAT Master

找到你的以太网接口 MAC 地址( eth0的mac)

ifconfig

编辑配置文件 /etc/ethercat.conf:

sudo nano /etc/ethercat.conf

填写配置

MASTER0_DEVICE="你获取到的mac地址"  
DEVICE_MODULES="generic"

注意:原配置文件有空内容,需要注释掉,否则会报错:要求你重新配置


四、启动EtherCAT服务

开机自启

sudo systemctl enable ethercat.service

立即启动服务

sudo systemctl start ethercat.service 

查看状态

sudo systemctl status ethercat.service

                                                               报错见 三

赋予用户访问 EtherCAT 设备节点的权限(避免每次都要 sudo)

sudo chmod 666 /dev/EtherCAT0

五、案例准备

大佬源头的软件验证

https://github.com/TonyInvent/ethercat_daq_flask

 创建虚拟环境

python3 -m venv 环境名字

 启动虚拟环境

source venv/bin/activate

安装依赖卡的话用镜像

pip3 install -r requirements.txt
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

六,启动项目

 显示当前EtherCAT主站的状态和信息

sudo ethercat master

扫描并列出所有连接到EtherCAT网络中的从站设备

sudo ethercat slaves

启动软件(二选一)

python read_daq.py

 报错了的话就用  sudo    虚拟环境的python路径   py文件的路径

启动网页监视(二选一)

python daq_web_server.py

                                                        同上

 然后就可以在局域网中的其他设备上访问查看信息

http://<树莓派IP>:5000

更多推荐