三菱 MELSEC 驱动
概述
三菱电机 PLC 系列(Q / L / iQ-R / iQ-F / FX)的 MC 协议(3E / 1E)通信。
| 项目 | 值 |
|---|---|
opc_type | MELSEC |
| 实现类 | plantpulse.driver.protocol.melsec.Melsec_34_Driver |
| 库 | iot-communication (plantpulse.plc.protocol.melsec) |
| read | ✅(批量模式) |
| write | ✅(Boolean/Integer/Long/Float/Double/String + BIN[n] 字位 RMW) |
| 安全 | 无(TCP 直连,以企业内网为前提) |
OPC 注册表单
| 字段 | 含义 | 示例 |
|---|---|---|
opc_agent_ip | PLC IP | 192.168.0.60 |
opc_agent_port | MC 端口 | 5000(Q/L 默认 1500) |
timecycle | 轮询周期(ms) | 1000 |
options.controller-type | 系列(Q_L、QnA、IQ_R、A —— iQ-F/FX 为 Q_L) | Q_L |
options.read-mode | single(按标签单独 read,default)/ grouped(分组批量 read —— 最小化往返) | single |
options.batch-gap-words | grouped 模式下允许合并字块的 gap | 16 (default) |
options.string-charset | String 标签编码(支持中韩文) | MS949 (default) |
options.enable-iqr-extended | iQ-R 扩展设备(LCN/LZ/LF/LSTN/LCC/LSC)直接 read | false (default) |
controller-type 未指定时使用 Q_L(Q / L 系列)。
标签 plc_address 格式
采用 iot-communication 表示法。设备代码 + 索引(十进制)组合。
| 表示 | 含义 |
|---|---|
D100 | Data Register 100(16-bit) |
D100.W | 16-bit word(显式) |
D100.D | 32-bit double word(D100~D101) |
D100.F | 32-bit float(D100~D101) |
M100 | Internal relay 100(1 bit) |
X100 | Input X100(1 bit) |
Y100 | Output Y100(1 bit) |
B0 | Link relay |
W100 | Link register |
R100 | File register |
D Data Register、M Coil、X/Y I/O、R File Reg、W Link Reg。
data_type / format 映射
data_type | format | 位宽 / 解释 |
|---|---|---|
| Boolean / Bool | (empty) | 1-bit(M、X、Y 等) |
| Short / Int / Integer | (empty) | 16-bit signed (W) |
| UInt16 | UI / UWORD | 16-bit unsigned |
| Int32 / DWord | DW | 32-bit signed(2 个字) |
| UInt32 | UDW | 32-bit unsigned |
| Float / REAL | REAL | 32-bit IEEE 754 |
| Double / LREAL | LREAL | 64-bit IEEE 754 |
| String | STR[N] | N 个字 → 2N 字节 ASCII |
批量模式(IS_BATCH_MODE=true)默认启用。将单台 PLC 的所有标签合并为一次 read 请求进行处理(REQUEST_LIMIT=1)。
常见错误 + 解决方法
| 消息 / 现象 | 原因 | 解决方法 |
|---|---|---|
| Connection refused | MC 协议未启用、端口被阻断 | 在 GX Works 中启用 MELSOFT 접속 / TCP / 포트 5000 |
Wrong device code | 设备代码不支持 | 确认 controller-type 是否一致(Q 与 iQ-R 的设备代码差异) |
| 32-bit 值不匹配 | byte order | 显式指定 D100.D(signed)/ D100.UD(unsigned) |
| 所有标签均为 0 | network 编号 / station 编号 mismatch | 在选项中显式指定 network、station(当前默认 0) |
curl 注册示例
curl -X POST http://<edge-host>/api/v1/opc \
-H "Content-Type: application/json" \
-d '{
"opc_id": "OPC_MELSEC_LINE1",
"opc_type": "MELSEC",
"opc_name": "Line1 Q-Series",
"opc_agent_ip": "192.168.0.60",
"opc_agent_port": "5000",
"site_id": "SITE_00001",
"auto_collect": true,
"timecycle": 1000,
"options": { "controller-type": "Q_L" },
"tag_list": [
{
"tag_id": "OPC_MELSEC_LINE1_TAG_00001",
"tag_name": "Counter",
"plc_address": "D100",
"data_type": "Integer"
},
{
"tag_id": "OPC_MELSEC_LINE1_TAG_00002",
"tag_name": "Pressure",
"plc_address": "D200",
"data_type": "Float",
"format": "REAL"
},
{
"tag_id": "OPC_MELSEC_LINE1_TAG_00003",
"tag_name": "PumpRun",
"plc_address": "M100",
"data_type": "Boolean"
}
]
}'
示例集(按数据类型)
三菱设备代码:D (Data)、M (Internal Relay)、X (Input)、Y (Output)、B (Link Relay)、W (Link Register)、R (File Register)、T (Timer)、C (Counter)。
位 / 布尔(Boolean)
data_type | format | plc_address 示例 | 含义 |
|---|---|---|---|
Boolean | (empty) | M100 | Internal relay 100 |
Boolean | (empty) | X100 | Input bit X100 |
Boolean | (empty) | Y100 | Output bit Y100 |
Boolean | (empty) | B0 | Link relay 0 |
16-bit 整数
data_type | format | plc_address 示例 | 含义 |
|---|---|---|---|
Integer | (empty) | D100 | Data Register(signed 16) |
Integer | (empty) | D100.W | 显式 16-bit Word |
Integer | UI | D100 | unsigned 16 |
Integer | (empty) | W100 | Link register |
Integer | (empty) | R100 | File register |
32-bit 整数
data_type | format | plc_address 示例 | 含义 |
|---|---|---|---|
Integer | DW | D100 | signed 32(D100~D101 组合) |
Integer | (empty) | D100.D | 以库表示法表示的 32-bit signed |
Integer | UDW | D100 | unsigned 32 |
Integer | (empty) | D100.UD | 以库表示法表示的 unsigned 32 |
浮点数
data_type | format | plc_address 示例 | 含义 |
|---|---|---|---|
Float | REAL | D200 | IEEE 754 32-bit(D200~D201) |
Float | (empty) | D200.F | 库表示法 |
Double | LREAL | D300 | IEEE 754 64-bit |
字符串
data_type | format | plc_address 示例 | 含义 |
|---|---|---|---|
String | STR[5] | D400 | 5 个字 = 10 byte ASCII |
String | STR[16] | D500 | 16 个字 = 32 byte |
Formula 应用
| 用途 | data_type | fomula | 备注 |
|---|---|---|---|
| 整数 raw → 实数缩放 | Float | ${VALUE}*0.01 | 12345 → 123.45 |
| 计数器累计(与其他标签求和) | Integer | ${VALUE}+${TAG_PREV} | 与上一累计值相加 |
| 单位换算(g → kg) | Float | ${VALUE}/1000 |
curl 综合示例
curl -X POST http://<edge-host>/api/v1/opc \
-H "Content-Type: application/json" \
-d '{
"opc_id": "OPC_MELSEC_FULL",
"opc_type": "MELSEC",
"opc_name": "MELSEC iQ-R Full",
"opc_agent_ip": "192.168.0.60",
"opc_agent_port": "5000",
"site_id": "SITE_00001",
"auto_collect": true,
"timecycle": 1000,
"options": { "controller-type": "IQ_R" },
"tag_list": [
{"tag_id":"OPC_MELSEC_FULL_T01", "tag_name":"InternalBit", "plc_address":"M100", "data_type":"Boolean"},
{"tag_id":"OPC_MELSEC_FULL_T02", "tag_name":"InputBit", "plc_address":"X100", "data_type":"Boolean"},
{"tag_id":"OPC_MELSEC_FULL_T03", "tag_name":"DataWord", "plc_address":"D100", "data_type":"Integer"},
{"tag_id":"OPC_MELSEC_FULL_T04", "tag_name":"DataDWord", "plc_address":"D102", "data_type":"Integer", "format":"DW"},
{"tag_id":"OPC_MELSEC_FULL_T05", "tag_name":"Pressure", "plc_address":"D200", "data_type":"Float", "format":"REAL"},
{"tag_id":"OPC_MELSEC_FULL_T06", "tag_name":"Power", "plc_address":"D300", "data_type":"Double", "format":"LREAL"},
{"tag_id":"OPC_MELSEC_FULL_T07", "tag_name":"Batch", "plc_address":"D500", "data_type":"String", "format":"STR[16]"},
{"tag_id":"OPC_MELSEC_FULL_T08", "tag_name":"PressKpa", "plc_address":"D200", "data_type":"Float", "format":"REAL", "fomula":"${VALUE}*0.001"}
]
}'