OpenPLC在PREEMPT_RT实时Linux上部署

发布时间:2026/7/8 3:39:49
OpenPLC在PREEMPT_RT实时Linux上部署 Web 服务器与plc_main绑定在一个CPU上1. 找到 OpenPLC 的 systemd 服务文件通常位于/etc/systemd/system/openplc.service2. 编辑服务文件找到文件路径后使用vim编辑vim /etc/systemd/system/openplc.service3. 在 [Service] 部分添加/修改以下指令[Unit] DescriptionOpenPLC Runtime v4 Service Afternetwork.target ​ [Service] Typesimple Restartalways RestartSec5 Userroot Grouproot # 设置实时调度策略和优先级 (99 为最高) CPUSchedulingPolicyfifo CPUSchedulingPriority99 # 绑定到特定 CPU 核心 (例如 CPU 2) CPUAffinity2 # 允许锁定内存防止被换出 LimitMEMLOCKinfinity ​ WorkingDirectory/run/media/mmcblk2p12/openplc-runtime ExecStart/run/media/mmcblk2p12/openplc-runtime/start_openplc.sh ​ [Install] WantedBymulti-user.target ​4. 重启 systemd 并启动服务systemctl daemon-reload systemctl restart openplc-runtime.service5. 验证是否生效chrt -p $(pgrep plc_main)输出中应显示pid 1714s current scheduling policy: SCHED_FIFO。Web 服务器与plc_main绑定在不同的CPU上1. 创建正确的openplc.servicetee /etc/systemd/system/openplc.service EOF [Unit] DescriptionOpenPLC v4 - Web Server Afternetwork.target PartOfopenplc.target ​ [Service] Typesimple Userroot WorkingDirectory/run/media/mmcblk2p12/openplc-runtime CPUAffinity0 EnvironmentPYTHONPATH/run/media/mmcblk2p12/openplc-runtime ExecStart/run/media/mmcblk2p12/openplc-runtime/venvs/runtime/bin/python /run/media/mmcblk2p12/openplc-runtime/webserver/app.py ExecStop/usr/bin/pkill -f app.py Restarton-failure RestartSec5 ​ [Install] WantedByopenplc.target EOF2. 创建正确的openplc-plc.servicetee /etc/systemd/system/openplc-plc.service EOF [Unit] DescriptionOpenPLC v4 - PLC Core Afternetwork.target openplc.service PartOfopenplc.target Wantsopenplc.service ​ [Service] Typesimple Userroot WorkingDirectory/run/media/mmcblk2p12/openplc-runtime CPUAffinity2 CPUSchedulingPolicyfifo CPUSchedulingPriority99 LimitMEMLOCKinfinity ExecStart/run/media/mmcblk2p12/openplc-runtime/build/plc_main ExecStop/usr/bin/pkill -f plc_main Restarton-failure RestartSec5 ​ [Install] WantedByopenplc.target EOF4. 创建 target 文件可选用于统一管理tee /etc/systemd/system/openplc.target EOF [Unit] DescriptionOpenPLC v4 - All Services Requiresopenplc.service openplc-plc.service Afteropenplc.service openplc-plc.service EOF 使用 target 的日常管理命令操作命令启动所有服务systemctl start openplc.target停止所有服务systemctl stop openplc.target重启所有服务systemctl restart openplc.target查看整体状态systemctl status openplc.target查看依赖的服务systemctl list-dependencies openplc.target5. 重新加载 systemdsystemctl daemon-reload6. 验证文件已被识别systemctl list-unit-files | grep openplc应该看到textopenplc-plc.service disabled disabled openplc-runtime.service enabled enabled openplc.service disabled disabled openplc.target static -target 不需要 enable7. 启用并启动服务systemctl enable openplc.service systemctl enable openplc-plc.service systemctl start openplc.service systemctl start openplc-plc.service8. 检查状态sudo systemctl status openplc.service sudo systemctl status openplc-plc.service 如果openplc-runtime.service与你冲突如果openplc-runtime.service也会启动 OpenPLC你可能需要先停止并禁用它避免重复启动systemctl stop openplc-runtime.service systemctl disable openplc-runtime.service验证 target 是否工作# 启动所有服务 sudo systemctl start openplc.target ​ # 检查状态 sudo systemctl status openplc.target sudo systemctl status openplc.service sudo systemctl status openplc-plc.service ​ # 确认进程 ps -eo pid,comm,psr,rtprio,policy,args | grep -E app|webserver|openplc | grep -v grep