8.2 KiB
8.2 KiB
项目流程图
1. 系统整体架构
flowchart TB
PLC1["西门子 S7-1200/1500<br/>PLC #1 (DB100)"]
PLC2["西门子 S7-1200/1500<br/>PLC #2 (DB100)"]
subgraph SN [Node.js 服务端]
SC["Scanner 轮询线程<br/>每 1000ms"]
CL["Snap7 Client<br/>读写 DB 块"]
CO["采集服务<br/>DataReady 处理"]
IL["互锁服务<br/>PrevResultReq 处理"]
API["REST API<br/>Hono (:3000)"]
SW["静态文件服务<br/>web/dist"]
DB[("SQLite<br/>production.db")]
end
subgraph WEB ["浏览器"]
UI["前端页面<br/>Vite + Tailwind"]
end
PLC1 -- "S7 协议" --> CL
PLC2 -- "S7 协议" --> CL
CL --> SC
SC --> CO
SC --> IL
CO --> DB
IL --> DB
API --> DB
API --> SW
SW --> UI
UI -- "http://localhost:3000" --> API
2. 启动流程
flowchart LR
START["npm run start"] --> INIT
INIT["ensureDatabase()"] --> CHECK_DB{"数据库<br/>存在?"}
CHECK_DB -->|"否"| CREATE["创建配置表<br/>MeasurementPoints<br/>PLCs<br/>Stations"]
CHECK_DB -->|"是"| LOAD["加载配置"]
CREATE --> LOAD
LOAD --> SKIP{"SKIP_PLC?"}
SKIP -->|"是"| SKIP_LOG["跳过 PLC 连接"]
SKIP -->|"否"| CONNECT["连接 PLC<br/>ConnectTo(ip, 0, 2)"]
CONNECT --> SUCCESS{"成功?"}
SUCCESS -->|"是"| START_LOG["开始轮询 1000ms"]
SUCCESS -->|"否"| ERR_LOG["记录失败日志"]
ERR_LOG --> START_LOG
SKIP_LOG --> START_LOG
START_LOG --> HTTP["HTTP 服务 :3000"]
3. 数据采集流程(DataReady)
sequenceDiagram
participant PLC as 西门子 PLC
participant SC as Scanner 轮询
participant DB as SQLite
participant CW as ControlWord
PLC->>CW: 写入测量值<br/>DataReady=1
SC->>PLC: 每 1000ms 读 32 字节
PLC-->>SC: ProductID + 测量值 + ControlWord
SC->>CW: 检查 DataReady
CW-->>SC: DataReady=1
SC->>SC: 解析数据
SC->>DB: 查该产品最大 InspectionCount
DB-->>SC: max(InspectionCount)
alt 首次检测
SC->>DB: INSERT<br/>InspectionCount=1
else 返修
SC->>DB: UPDATE<br/>InspectionCount+1
end
SC->>PLC: 写 ControlWord<br/>DataReady=0
4. 工序互锁流程(PrevResultReq)
sequenceDiagram
participant PLC as 西门子 PLC
participant SC as Scanner 轮询
participant DB as SQLite
PLC->>PLC: 需要查上一工序结果<br/>PrevResultReq=1
SC->>PLC: 读 32 字节
PLC-->>SC: ProductID + ControlWord
SC->>SC: 检测到 PrevResultReq=1
SC->>DB: 获取所有工序(按 Id 排序)
DB-->>SC: [预焊接, 主焊接, 焊后检测, ...]
SC->>SC: 找到当前工序位置
alt 是首工序
SC->>SC: PrevResult=1
else 不是首工序
SC->>DB: 查前工序的 IsQualified
DB-->>SC: IsQualified=1 或 0
end
SC->>PLC: 写 ControlWord<br/>PrevResult=1/0<br/>PrevResultReq=0
5. 设备配置流程(前端 → API → 数据库)
flowchart LR
subgraph FE ["前端"]
MP["检测点管理<br/>增删改"]
PLC["PLC 管理<br/>增删改"]
ST["工序管理<br/>增删改<br/>下拉选检测点"]
end
subgraph API ["后端 API"]
GET_MP["GET /api/config/measurement-points"]
POST_MP["POST /api/config/measurement-points"]
GET_PLC["GET /api/config/plcs"]
POST_PLC["POST /api/config/plcs"]
GET_ST["GET /api/config/stations"]
POST_ST["POST /api/config/stations<br/>自动关联检测点"]
end
subgraph DB ["数据库"]
TBL_MP["MeasurementPoints"]
TBL_PLC["PLCs"]
TBL_ST["Stations"]
DATA["MPData_P{id}_{date}"]
end
MP --> GET_MP
MP --> POST_MP
PLC --> GET_PLC
PLC --> POST_PLC
ST --> GET_ST
ST --> POST_ST
GET_MP --> TBL_MP
POST_MP -->|"创建检测点"| TBL_MP
POST_MP -->|"自动创建"| DATA
GET_PLC --> TBL_PLC
POST_PLC --> TBL_PLC
GET_ST --> TBL_ST
POST_ST --> TBL_ST
6. 追溯查询流程
sequenceDiagram
participant UI as 前端页面
participant API as 后端 API
participant DB as SQLite
UI->>UI: 输入产品编号<br/>点击查询
UI->>API: GET /api/query/PROD-001
API->>DB: 查询所有检测点
DB-->>API: [{Id:1, 焊接温度}, {Id:2, 焊接电流}, ...]
loop 遍历每个检测点
API->>DB: SELECT FROM MPData_P{Id}_{date}
DB-->>API: RecordTime, IsQualified,<br/>InspectionCount, 测量值
end
API-->>UI: 返回所有检测点数据
UI->>UI: 按检测点分组展示<br/>合格/不合格 标记<br/>返修标记
7. PLC 内存布局
flowchart TB
subgraph DB1 ["DB1 (PLC接口数据块)"]
ST0["工序 0<br/>偏移 0 ~ 23<br/>24 字节"]
ST1["工序 1<br/>偏移 24 ~ 47<br/>24 字节"]
ST2["工序 2<br/>偏移 48 ~ 71<br/>24 字节"]
ST_N["... 更多工序<br/>每工序 24 字节"]
end
subgraph ST0_D ["工序 0 内部结构"]
P0["ProductID (DINT)<br/>偏移 +0 | 4 字节"]
UL["UpperLimit (REAL)<br/>偏移 +4 | 4 字节"]
LL["LowerLimit (REAL)<br/>偏移 +8 | 4 字节"]
MV["MeasuredValue (REAL)<br/>偏移 +12 | 4 字节"]
CW["ControlWord (WORD)<br/>偏移 +16 | 2 字节"]
RS["Reserved<br/>偏移 +18 | 6 字节"]
end
subgraph CW_D ["ControlWord 位定义"]
B0["Bit 0: DataReady<br/>PLC → PC"]
B1["Bit 1: IsQualified<br/>PLC → PC"]
B2["Bit 2: PrevResultReq<br/>PLC → PC"]
B3["Bit 3: PrevResult<br/>PC → PLC"]
B4["Bit 4~15<br/>保留"]
end
DB1 --> ST0
DB1 --> ST1
DB1 --> ST2
DB1 --> ST_N
ST0 --> ST0_D
CW --> CW_D
8. 数据库表关系
erDiagram
MeasurementPoints ||--o{ Stations : "1:N"
PLCs ||--o{ Stations : "1:N"
Stations ||--o{ "MPData_P{id}_{date}" : "写入数据"
MeasurementPoints {
int Id PK
string PointName
string FieldUnit
}
PLCs {
int Id PK
string PLCName
string IPAddress
int DBNumber
int StationCount
}
Stations {
int Id PK
int PLCId FK
int StationIndex
string StationName
int PointId FK
}
"MPData_P{id}_{date}" {
int Id PK
string ProductID UK
datetime RecordTime
int IsQualified
int InspectionCount
float UpperLimit
float LowerLimit
float MeasuredValue
}
9. 部署架构
flowchart LR
subgraph WIN ["Windows 工控机"]
PROC["Node.js 进程<br/>(tsx 或编译后运行)"]
HTTP["HTTP 服务 :3000"]
DB[("SQLite 数据库<br/>data/production.db")]
WEB_DIST["前端构建产物<br/>web/dist"]
end
subgraph PLC_NET ["车间以太网"]
PLC_A["PLC #1<br/>192.168.0.10"]
PLC_B["PLC #2<br/>192.168.0.11"]
end
subgraph CLIENT ["浏览器"]
ADMIN["管理员电脑<br/>查追溯/配置"]
end
PLC_A -->|"S7 协议"| PROC
PLC_B -->|"S7 协议"| PROC
PROC --> DB
PROC --> WEB_DIST
HTTP --> ADMIN
10. 日志系统
flowchart TB
subgraph LOG ["日志格式"]
TS["时间戳<br/>2026-06-09 12:00:00"]
LVL["级别<br/>D/I/W/E"]
TAG["模块<br/>Scanner/PLC/采集/互锁"]
MSG["消息"]
end
subgraph LVLS ["级别说明"]
D["[D] DEBUG<br/>开发调试"]
I["[I] INFO<br/>正常流程"]
W["[W] WARN<br/>需关注"]
E["[E] ERROR<br/>异常错误"]
end
subgraph MODS ["模块 TAG"]
S["Scanner<br/>轮询/配置"]
P["PLC<br/>连接/读写"]
C["采集<br/>数据入库"]
I2["互锁<br/>前工序查询"]
SC["Schema<br/>建表"]
end
LOG --> LVLS
LOG --> MODS
%% 示例日志
EX1["✅ 正常: [I] [Scanner] PLC01 DataReady=1, ProductID=12345, 开始采集"]
EX2["⚠️ 警告: [W] [PLC] DB100 读取工序 0 失败: tcp连接超时"]
EX3["✅ 采集: [I] [采集] 焊接温度 产品 12345: 首次检测 95.5"]
EX4["✅ 互锁: [I] [互锁] 主焊接 查询 预焊接 → 合格"]