跳到主要内容

三菱 MELSEC 驱动

概述

三菱电机 PLC 系列(Q / L / iQ-R / iQ-F / FX)的 MC 协议(3E / 1E)通信。

项目
opc_typeMELSEC
实现类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_ipPLC IP192.168.0.60
opc_agent_portMC 端口5000(Q/L 默认 1500)
timecycle轮询周期(ms)1000
options.controller-type系列(Q_LQnAIQ_RA —— iQ-F/FX 为 Q_LQ_L
options.read-modesingle(按标签单独 read,default)/ grouped(分组批量 read —— 最小化往返)single
options.batch-gap-wordsgrouped 模式下允许合并字块的 gap16 (default)
options.string-charsetString 标签编码(支持中韩文)MS949 (default)
options.enable-iqr-extendediQ-R 扩展设备(LCN/LZ/LF/LSTN/LCC/LSC)直接 readfalse (default)

controller-type 未指定时使用 Q_L(Q / L 系列)。


标签 plc_address 格式

采用 iot-communication 表示法。设备代码 + 索引(十进制)组合。

表示含义
D100Data Register 100(16-bit)
D100.W16-bit word(显式)
D100.D32-bit double word(D100~D101)
D100.F32-bit float(D100~D101)
M100Internal relay 100(1 bit)
X100Input X100(1 bit)
Y100Output Y100(1 bit)
B0Link relay
W100Link register
R100File register

D Data Register、M Coil、X/Y I/O、R File Reg、W Link Reg。


data_type / format 映射

data_typeformat位宽 / 解释
Boolean / Bool(empty)1-bit(M、X、Y 等)
Short / Int / Integer(empty)16-bit signed (W)
UInt16UI / UWORD16-bit unsigned
Int32 / DWordDW32-bit signed(2 个字)
UInt32UDW32-bit unsigned
Float / REALREAL32-bit IEEE 754
Double / LREALLREAL64-bit IEEE 754
StringSTR[N]N 个字 → 2N 字节 ASCII

批量模式(IS_BATCH_MODE=true)默认启用。将单台 PLC 的所有标签合并为一次 read 请求进行处理(REQUEST_LIMIT=1)。


常见错误 + 解决方法

消息 / 现象原因解决方法
Connection refusedMC 协议未启用、端口被阻断在 GX Works 中启用 MELSOFT 접속 / TCP / 포트 5000
Wrong device code设备代码不支持确认 controller-type 是否一致(Q 与 iQ-R 的设备代码差异)
32-bit 值不匹配byte order显式指定 D100.D(signed)/ D100.UD(unsigned)
所有标签均为 0network 编号 / station 编号 mismatch在选项中显式指定 networkstation(当前默认 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_typeformatplc_address 示例含义
Boolean(empty)M100Internal relay 100
Boolean(empty)X100Input bit X100
Boolean(empty)Y100Output bit Y100
Boolean(empty)B0Link relay 0

16-bit 整数

data_typeformatplc_address 示例含义
Integer(empty)D100Data Register(signed 16)
Integer(empty)D100.W显式 16-bit Word
IntegerUID100unsigned 16
Integer(empty)W100Link register
Integer(empty)R100File register

32-bit 整数

data_typeformatplc_address 示例含义
IntegerDWD100signed 32(D100~D101 组合)
Integer(empty)D100.D以库表示法表示的 32-bit signed
IntegerUDWD100unsigned 32
Integer(empty)D100.UD以库表示法表示的 unsigned 32

浮点数

data_typeformatplc_address 示例含义
FloatREALD200IEEE 754 32-bit(D200~D201)
Float(empty)D200.F库表示法
DoubleLREALD300IEEE 754 64-bit

字符串

data_typeformatplc_address 示例含义
StringSTR[5]D4005 个字 = 10 byte ASCII
StringSTR[16]D50016 个字 = 32 byte

Formula 应用

用途data_typefomula备注
整数 raw → 实数缩放Float${VALUE}*0.0112345 → 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"}
]
}'