PLC Communication Protocol with Siemens PLC

1. Protocol Reverse Engineering Record (Wireshark Capture Code)

# Wireshark filter rule (key fields marked in red)tcp.port == 102 && cotp && s7comm
/* Typical handshake message structure
0000  03 00 00 16 11 e0  [TSAP:0x0100]  [TSAP:0x0101]  00 00 00 01 00 c0 01 0a
      |_________| |___| |_____________| |_____________| |_TPKT_| |_COTP_| |S7C|
*/
#pragma pack(1)
struct S7_PDU {      // Measured Siemens 1500 maximum PDU length 240 bytes
    uint8_t proto_id;    // 0x32=Job,0x72=ACK
    uint8_t rosctr;      // 01-Job,02-ACK,03-Data
    uint16_t req_id;     // Incremental sequence number (marked in red)
    uint16_t param_len;  // Parameter block length
    uint16_t data_len;   // Data block length
    uint8_t func_code;   // 0x04-Read,0x05-Write
    // Variable area changes according to func_code
};

Note: When capturing packets, be sure to lock the TSAP addressing black box rules, as the TSAP magic number varies greatly between different PLC models (e.g., 1200 uses 0x0100/0x0102, 1500 uses 0x0100/0x0101), misconfiguration leads to handshake failure directly.

PLC Communication Protocol with Siemens PLC

2. Message Encryption Algorithm Cracking Example

// S7Comm CRC16 check algorithm reverse (key parameters marked in red)public ushort CalculateCrc(byte[] data) {
    ushort crc = 0xFFFF;  // Initial value
    for(int i=0; i<data.Length; i++){
        crc ^= data[i];
        for(int j=0; j<8; j++){
            if((crc & 0x0001) != 0) {
                crc = (ushort)((crc >> 1) ^ 0x8408); // Polynomial reverse
            } else {
                crc >>= 1;
            }
        }
    }
    return (ushort)~crc; // Final XOR value (marked in red)
}

Note: The new version V16 firmware begins to mix PROFINET CRC32, check the firmware version first when traditional CRC16 check fails!

PLC Communication Protocol with Siemens PLC

3. Gigabit Data Acquisition Optimization Plan

// C# asynchronous Socket + dynamic PDU tuning (core parameters marked in red)const int MAX_PDU = 240; // S7-1500 limit
Socket.BeginReceive(buffer, 0, MAX_PDU * 20, // Batch process 20 PDU
    SocketFlags.None, OnDataReceived, null);
// Throughput calculation formula (marked in red ΔT window)
double Q = deltaT / (N * t); // N=concurrent connections, t=request duration
Note: Asynchronous mode must handle sticky packets! It has been measured that the third byte of the Siemens message header stores the length of subsequent data, and slicing according to this value is necessary for correct unpacking.
PLC Communication Protocol with Siemens PLC

S7 Protocol Exception Code Quick Reference Table (Top 10 Fatal Faults)

| Error Code | Fatal Level | Key Points for Investigation

| 0x0320 | ★★★★★ | PDU length exceeded, immediately check PLC model corresponding PDU table |

| 0x0011 | ★★★★☆ | TSAP address conflict, restart port service |

| 0xD501 | ★★★★☆ | Data block out of bounds, check DB block number and offset |

| 0x0503 | ★★★☆☆ | Function code not supported, common in encrypted function block calls |

| 0x8104 | ★★★☆☆ | Resource lock conflict, reduce polling frequency |

PLC Communication Protocol with Siemens PLC

Leave a Comment