# melsec — Port 5000 Mitsubishi MELSEC MC 3E Binary PLC Frames (`melsec`)

> **FrameAssembly v3.3.0** · Protocol 117 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/melsec

- **Full Protocol Name:** Mitsubishi Electric MELSEC Communication Protocol (QnA / iQ-R 3E Frame)
- **Keyword:** `melsec`
- **OSI Layer & Transport:** Layer 7 · TCP 5000 (or UDP 5000 via transport udp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** Mitsubishi MELSEC Communication Protocol Reference (SH-080008)
- **Wireshark Display Filter:** `tcp.port == 5000 || udp.port == 5000`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 5000 (or UDP 5000 via transport udp) (transport tcp|udp) -> Mitsubishi MELSEC MC (3E)`

## Overview

Semiconductor and automotive assembly cells rely on Mitsubishi MELSEC iQ-R/Q PLCs on port `5000` — `melsec` writes little-endian QnA-3E binary `0x5000`/`0xd000` subheaders and device codes (`device "D"`, `addr`, `count`, `value`).

MELSEC Communication Protocol (`melsec`, MC Protocol **QnA-compatible 3E Binary Frame**) is Mitsubishi Electric’s workhorse PLC protocol across **MELSEC iQ-R, iQ-F, Q-Series, and L-Series controllers** in semiconductor fabs, EV battery plants, and discrete manufacturing on **TCP or UDP port `5000`** (commonly `5000`–`5007`). Every 3E Binary request starts with Subheader `50 00` (`d0 00` on responses), `Network No.`, `PC No.` (`0xff` local CPU), little-endian 16-bit `Request Destination Module I/O No.` (`0x03ff` own station), `Station No.`, little-endian 16-bit `Request Data Length`, 16-bit `Monitoring Timer`, 16-bit `Command` (`0x0401` Batch Read, `0x1401` Batch Write) + `Subcommand`, 24-bit `Head Device Number` (`addr`), `Device Code` (`D`=`0xa8`, `M`=`0x90`, `X`=`0x9c`, `Y`=`0x9d`, `W`=`0xb4`, `R`=`0xaf`), and 16-bit `Number of Device Points` (`count`).

FrameAssembly encodes exact little-endian 3E binary frames, 24-bit device addresses, and 16-bit `End Code` (`endcode 0x0000`) responses.

## Minimal Quickstart (`melsec_quickstart.frasm`)

```frameassembly
host got_hmi  { ip 192.168.3.10 }
host iqr_plc  { ip 192.168.3.39 }

compile {
    got_hmi -> iqr_plc tcp syn srcport 51500 dstport 5000 wait 1ms
    got_hmi <- iqr_plc tcp syn ack wait 1ms
    got_hmi -> iqr_plc tcp ack wait 1ms
    got_hmi -> iqr_plc melsec read device "D" addr 100 count 4 wait 2ms
    got_hmi <- iqr_plc melsec read response endcode 0x0000 value 2500
}
```

### Automatic Wire Calculations

- Defaults to stateful TCP port `5000` (`transport udp` switches to UDP `5000`) and writes Subheader `50 00` on requests or `d0 00` on `response`/`isresponse` frames.
- Computes the little-endian 16-bit `Request / Response Data Length` in bytes 7..8 automatically.
- Maps Mitsubishi device mnemonics (`"D"` -> `0xa8`, `"M"` -> `0x90`, `"X"` -> `0x9c`, `"Y"` -> `0x9d`, `"W"` -> `0xb4`, `"R"` -> `0xaf`, `"B"` -> `0xa0`) and packs 3-byte little-endian `addr` values.

## Practical Lab Example: MELSEC iQ-R Cell Line Cycle: Data Register Batch Read (D100), Setpoint Batch Write (D200) & UDP Poll (`melsec_iqr_batch_rw.frasm`)

Connects to an iQ-R PLC on TCP `5000` (`io 0x03ff`, `pc 0xff`), batch-reads `D100..D103`, writes a servo target into `D200`, and polls Link Register `W0` over UDP `5002` (`transport udp`).

```frameassembly
host gx_works3 { ip 10.39.1.15 }
host melsec_q  { ip 10.39.1.50 }

compile {
    gx_works3 -> melsec_q tcp syn srcport 53500 dstport 5000 wait 1ms
    gx_works3 <- melsec_q tcp syn ack wait 1ms
    gx_works3 -> melsec_q tcp ack wait 1ms
    // 1. 3E Binary Batch Read (0x0401, Subcmd 0x0000 Word): Read 4 words from D100
    gx_works3 -> melsec_q melsec read network 0 pc 0xff io 0x03ff station 0 timer 16 device "D" addr 100 count 4 wait 2ms
    gx_works3 <- melsec_q melsec read response endcode 0x0000 value 1024 wait 8ms
    // 2. 3E Binary Batch Write (0x1401): Write word value 3600 to D200
    gx_works3 -> melsec_q melsec write device "D" addr 200 count 1 value 3600 wait 2ms
    gx_works3 <- melsec_q melsec write response endcode 0x0000 wait 10ms
    // 3. High-speed UDP 5002 Link Register W0 poll
    gx_works3 -> melsec_q melsec read dstport 5002 device "W" addr 0 count 2 transport udp wait 2ms
    gx_works3 <- melsec_q melsec read response endcode 0x0000 value 1 transport udp
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show melsec_iqr_batch_rw.frasm

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly melsec_iqr_batch_rw.frasm melsec_iqr_batch_rw.pcap
frameassembly melsec_iqr_batch_rw.frasm melsec_iqr_batch_rw.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse melsec_iqr_batch_rw.pcap melsec_iqr_batch_rw_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r melsec_iqr_batch_rw.pcap
```

## All `melsec` Keywords & Header Options

Packet line pattern: `src_host -> dst_host melsec [read | write | cmd "<text_or_num>"] [response | isresponse] [subcmd <0..65535>] [network <0..255>] [pc <0..255>] [io <0..65535>] [station <0..255>] [timer <0..65535>] [device "<text_or_num>"] [addr <0..16777215>] [count <0..65535>] [value <0..65535>] [endcode|errcode <0..65535>] [transport <tcp|udp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `read \| write \| cmd "<text_or_num>" \| [response \| isresponse]` | 16-bit MC Command (`0x0401` Batch Read, `0x1401` Batch Write, `0x1001` Remote Run, `0x1002` Remote Stop) | `read (0x0401), request (50 00)` | Sets the 16-bit little-endian Command Code and Subheader (`50 00` vs `d0 00` with `response`). |
| `subcmd <0..65535>` | 16-bit Subcommand (`0x0000` word units, `0x0001` bit units, `0x0002` iQ-R extended) | `0x0000 (word units)` | Writes bytes 13..14 of the 3E request header. |
| `network <0..255> \| pc <0..255> \| io <0..65535> \| station <0..255> \| timer <0..65535>` | 3E Routing Path + Monitoring Timer (in 250 ms units) | `network 0, pc 0xff, io 0x03ff, station 0, timer 16` | Populates bytes 2..6 (Access Route) and bytes 9..10 (CPU Monitoring Timer) of the 3E header. |
| `device "<text_or_num>" \| addr <0..16777215> \| count <0..65535> \| value <0..65535>` | Device Code ("D","M","X","Y","W","R","B"), 24-bit Head Address, Point Count, and 16-bit Word Value | `device "D" (0xa8), addr 0, count 1, value 0` | Writes the 3-byte little-endian `addr`, 1-byte `Device Code`, 2-byte `count`, and write/read word data. |
| `endcode <0..65535> \| errcode <0..65535> \| transport <tcp\|udp>` | 16-bit Response End Code (`0x0000` Success) & L4 transport | `endcode 0x0000, transport tcp` | Writes the 2-byte little-endian `End Code` in `d0 00` responses or toggles TCP/UDP. |

## Cross-Layer Options Valid with `melsec`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>] [routeralert] [badipchecksum]` | IPv4 ID, DF/MF flags, offset 0..8191, Router Alert & checksum fault | `ipid 1, flags 0, fragoffset 0, valid RFC 1071 checksum` | Sets IPv4 Identification/fragmentation, injects RFC 2113 Router Alert (`20 04 00 00`), or corrupts the 16-bit IPv4 header checksum (`badipchecksum`). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (valid on IPv6 endpoint pairs only). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 5000 (or UDP 5000 via transport udp)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport <tcp\|udp\|sctp>` | `tcp`, `udp`, or `sctp` (dual TCP/UDP + universal L7 SCTP) | `transport tcp` | Switches Layer 4 transport between stateful TCP, connectionless UDP, or Castagnoli CRC-32c SCTP association. |
| `badchecksum \| nochecksum` | L4 checksum fault or UDP zero-checksum flag | `Valid RFC checksum / CRC-32c` | `badchecksum` corrupts the L4 checksum for negative firewall/NIC offload tests; `nochecksum` writes `0x0000` in UDP headers. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window, flags, SACK, TCP-MD5 & TFO options | `psh ack, win 65535, automatic TCP seq/acknum` | When using TCP (`transport tcp` or default TCP): overrides automatic TCP sequence tracking, flags, or `mss`/`wscale`/`sackperm`/`sackblock`/`tsval`/`tsecr`/`tcpmd5`/`tfo`. |

## Common Mistakes & Quick Fixes

### Keep io = 0x03ff and pc = 0xff When Addressing the Local CPU Module

- **Valid v3.3.0 Syntax:** `hmi -> plc melsec read network 0 pc 0xff io 0x03ff station 0 device "D" addr 100 count 2`

Mitsubishi 3E Binary frames default to `network 0`, `pc 0xff`, `io 0x03ff` (own station CPU); changing `io` routes the request to an intelligent function or multi-CPU slot.

### Add response (or isresponse) on <- Replies

- **Valid v3.3.0 Syntax:** `hmi <- plc melsec read response endcode 0x0000 value 500`

Without `response`, the frame emits a `50 00` Request Subheader instead of a `d0 00` 3E Response Subheader + `End Code`.
