FATEK FBs 驱动
概述
FATEK FACON FBs 系列 PLC 的 ASCII 通信。基于 FBs Series Communication Protocol Manual 公开规范自行实现,不依赖外部库 / GitHub 代码。
| 项目 | 值 |
|---|---|
opc_type | FATEK |
| 实现类 | plantpulse.driver.protocol.fatek.FatekDriver |
| 库 | (无 —— 自行实现,依据 FATEK FBs 手册) |
| read | 良好 (cmd 44 / 46) |
| write | 良好 (cmd 45 / 47) |
| 默认端口 | 500 |
| 安全 | 无 |
类 / 结构
FatekDriver (BaseProtocolDriver)
├── Socket / InputStream / OutputStream
├── station — 0~255 (디폴트 1)
├── synchronized read/write
├── Addr (device, index, bit)
├── parseAddress(raw) — R/D/M/X/Y/T/C/WX/WY/WM
├── formatBitItem / formatWordItem
└── sendAndReceive(cmd, body) — STX/ETX/LRC 검증
connect() 之后立即调用 cmd 41 (status) 作为握手,但即使失败也保持连接(PLC 可能不支持)。
wire 格式摘要
Request : STX <station 2hex> <cmd 2hex> <data ASCII> <LRC 2hex> ETX
Response: STX <station 2hex> <cmd 2hex> <error-1char> <data> <LRC 2hex> ETX
STX = 0x02, ETX = 0x03
LRC : STX 다음부터 LRC 직전까지 XOR (ASCII char 단위) → 2자리 hex 대문자
主要命令
| cmd | 动作 |
|---|---|
40 | Read system status |
41 | Get PLC run/stop/error/break status(用于握手) |
42 | Run/Stop PLC |
44 | Read data from registers(连续字) |
45 | Write data to registers(连续字) |
46 | Read individual data(单个位 / 混合) |
47 | Write individual data |
在 sendAndReceive() 中,若响应 error code 不为 '0' 则 IOException(例如:'2' illegal value、'4' illegal command、'5' checksum、'6' illegal address、'A' PLC password locked)。
OPC 注册选项 (options)
| 键 | 含义 | 默认值 |
|---|---|---|
station | PLC station ID (0~255) | 1 |
connect-timeout | ms | 3000 |
read-timeout | ms | 3000 |
标签地址格式
| 表示 | 含义 | bit/word |
|---|---|---|
R0 / R100 | General register | word |
D100 | Data register | word |
WX10 / WY0 / WM10 | Input/Output/Internal word | word |
M100 | Internal relay | bit |
X0 / Y0 / S0 | Input / Output / Step | bit |
T0 / C0 | Timer / Counter 当前值 | word(cmd 44 中的 RT/RC 表示) |
formatWordItem():
- R/D →
<dev>+5자 index(共 6 字符) - WX/WY/WM →
<dev>+4자(共 6 字符) - T →
RT+ 4 字符,C →RC+ 4 字符
数据编码 / 解码
wordCountFor(dt, fmt) 决定 1/2/4 个字。decodeWords() 负责转换 —— FATEK FB 多字采用 little-endian word order(低位字在前)。
| format / data_type | 字数 | 备注 |
|---|---|---|
W / WORD / UI / UW | 1 | signed/unsigned 16 |
B / BYTE | 1 | (直接使用 16-bit 响应) |
DW / DWORD / Int32 | 2 | signed 32 |
UDW / UInt32 | 2 | unsigned 32 |
REAL / FLOAT / Float | 2 | IEEE 32-bit |
LREAL / DOUBLE / Double | 4 | IEEE 64-bit |
LW / LWORD / Int64 | 4 | signed 64 |
支持 / 不支持
- ✅ cmd 44 / 45(连续 N 个字 read/write)
- ✅ cmd 46 / 47(单个位 read/write)
- ✅ STX/ETX/LRC 帧校验 + station/cmd 回显校验
- ✅ 32 / 64-bit 多字、Float、Double
- ❌ 多个位同时 read/write(cmd 46/47 的 mixed 项)
- ❌ Run/Stop PLC (cmd 42)
- ❌ Timer/Counter contact bit 分离(当前仅支持 word)
测试覆盖率
test/java/plantpulse/driver/protocol/fatek/
| 类 | 测试数 |
|---|---|
FatekDriverTest | 48 |
共 48 个测试 —— LRC、buildRequest、地址解析器、字编码/解码、error code。
参考
- FATEK FBs Series Communication Protocol Manual(厂商公开)
- 代码:
src/plantpulse/driver/protocol/fatek/FatekDriver.java