Files
dtm-py-all/PLC模拟模式说明.md

157 lines
3.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# PLC 模拟模式使用说明
## 概述
程序现在支持两种 PLC 运行模式:
- **真实 PLC 模式**(默认):连接到真实的 PLC 硬件设备
- **模拟模式**:使用软件模拟 PLC 行为,用于开发和测试
## 使用方法
### 1. 默认模式(真实 PLC
直接运行程序,不带任何参数:
```powershell
python dtmgtApp.py
```
**输出日志**
```
使用真实 PLC 设备
```
### 2. 模拟模式
使用 `--mock` 参数启动程序:
```powershell
python dtmgtApp.py --mock
```
**输出日志**
```
启用 PLC 模拟模式
```
## 模拟模式特性
在模拟模式下,程序会:
**模拟 PLC 寄存器**
- 自动初始化模拟寄存器
- 支持读写寄存器操作
- 支持线圈Coil和保持寄存器Holding Register
**模拟测试流程**
- 自动模拟通道测试过程
- 支持多方向跌落测试
- 自动生成测试结果数据
**无需硬件**
- 不需要真实的 PLC 设备
- 不需要串口连接
- 适合开发和调试
## 其他命令行参数
程序支持以下命令行参数:
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `--mock` | flag | False | 启用 PLC 模拟模式 |
| `--startModbusServer` | flag | True | 启动 Modbus 服务器 |
| `--modbusServer` | string | "127.0.0.1" | Modbus 服务器地址 |
| `--modbusServerPort` | int | 5020 | Modbus 服务器端口 |
| `--no-ui` | flag | False | 不启动 PyQt5 前端界面 |
| `--port` | int | 5050 | HTTP 服务端口 |
## 完整示例
### 示例 1模拟模式 + 自定义端口
```powershell
python dtmgtApp.py --mock --port 8080
```
### 示例 2真实 PLC + 不启动 UI
```powershell
python dtmgtApp.py --no-ui
```
### 示例 3模拟模式 + 自定义 Modbus 端口
```powershell
python dtmgtApp.py --mock --modbusServerPort 5021
```
## 代码修改说明
### modbus_plc.py
```python
MOCK_MODE = False # 默认使用真实 PLC通过命令行参数 --mock 启用模拟模式
```
### dtmgtApp.py
1. **添加命令行参数**
```python
parser.add_argument('--mock', action='store_true', help="启用 PLC 模拟模式")
```
2. **在 main() 函数中设置模式**
```python
if hasattr(args, 'mock') and args.mock:
import modbus_plc
modbus_plc.MOCK_MODE = True
print_with_timestamp("启用 PLC 模拟模式", color='yellow')
else:
print_with_timestamp("使用真实 PLC 设备", color='green')
```
## 注意事项
⚠️ **生产环境**
- 生产环境必须使用真实 PLC 模式
- 不要在生产环境中使用 `--mock` 参数
⚠️ **测试环境**
- 开发测试时可以使用模拟模式
- 模拟模式下的数据仅供测试,不代表真实测试结果
⚠️ **参数检查**
- 程序启动时会在日志中明确显示当前使用的模式
- 请仔细检查日志确认运行模式正确
## 常见问题
### Q1: 如何确认当前是什么模式?
**A**: 查看程序启动时的日志输出:
- `启用 PLC 模拟模式` → 模拟模式
- `使用真实 PLC 设备` → 真实模式
### Q2: 模拟模式下还需要 PLC 硬件吗?
**A**: 不需要。模拟模式完全在软件中运行,无需任何 PLC 硬件或串口连接。
### Q3: 如何在代码中检查当前模式?
**A**: 检查 `modbus_plc.MOCK_MODE` 变量:
```python
import modbus_plc
if modbus_plc.MOCK_MODE:
print("当前是模拟模式")
else:
print("当前是真实 PLC 模式")
```
## 更新历史
- **2025-12-08**: 初始版本
- 将 MOCK_MODE 默认值改为 False
- 添加 --mock 命令行参数
- 添加模式切换日志输出