西门子通信协议S7COMM
首先,这里所说的S7Comm 协议只是西门子S7通讯协议簇里的一种,以0x32开始的报文结构。
1、S7Comm协议结构:
借助WireShark抓包,可以看到,S7Comm 以太网协议基于OSI模型:
OSI layer Protocol
7 Application Layer S7 communication
6 Presentation Layer S
7 communication(COTP)
5 Session Layer S7 communication(TPKT)
4 Transport Layer ISO-on-TCP (RFC 1006)
3 Network Layer IP
2 Data Link Layer Ethernet
1 Physical Layer Ethernet
其中,第1~4层会由计算机自己完成(底层驱动程序);关于这些神马的定义,大家可以上网查一下;
第5层TPKT,应用程数据传输协议,介于TCP和COTP协议之间;这是一个传输服务协议,主要用来在COTP 和TCP之间建立桥梁;
"TPKT is an"encapsulation" protocol. It carries the OSI packet in its ownpacket's data payload and then passes the resulting structure to TCP, from thenon, the packet is processed as a TCP/IP packet. The OSI programs passing datato TPKT are unaware that their data will be carried over TCP/IP because TPKTemulates the OSI protocol Transport Service Access Point (TSAP)."
第6层COTP,按照维基百科的解释,COTP 是OSI 7层协议定义的位于TCP之上的协议。COTP 以“Packet”为基本单位来传输数据,这样接收方会得到与发送方具有相同边界的数据;
第7层,S7 communication,这一层和用户数据相关,对PLC数据的读取报文在这里完成;
刚看到TPKT和COPT也许会很迷惑,其实在具体的报文中,TPKT的作用是包含用户协议(5~7层)的数据长度(字节数);COTP的作用是定义了数据传输的基本单位(在S7Comm中PDU TYPE:DT data);
S7Comm与标准TCP/IP比较:S7Comm是一个7层协议;TCP/IP是四层协议,用户数据在第四层TCP层完成;
计算机与PLC进行通讯,可以连接102端口,这是西门子开放的一个通讯端口;
2、第七层S7 communication协议
S7 communication包含三部分:1-Header;2-Parameter;3 - Data。
根据实现的功能不同,S7 communication协议的结构会有所不同;例如,请求数据报文只包含前两部分;
<1>Header
*01(1 byte): protocol Id: 0x32;
*02a(1 byte): ROSCTR: Job (01);
*02b(2 byte): redundancy identification (reserved): 0x0000;
*2c(2 byte): protocol data unit reference; it’s increased by request event;
*2d(2 byte): parameter length - the total length (bytes) of parameter part;
*2e(2 byte): data length; 读取PLC内部数据,此处为00 00;对于其他功能,例如:读取CPU的型号,此处为Data部分的数据长度;
<2>Parameter(读取数据)
*3(1 byte): function code: Read Var (0x04);writeVar (0x05);
*4(1 byte): item count;
*5(1 byte): variable specification: 0x12;
*6(1 byte): length of following address specification – is 7~12length in byte;
*7(1 byte): syntax Id: S7ANY (0x10);
*8(1 byte):transport size: BYTE(2);
*9(2 byte): requested data length;
*10(2 byte): DB number; 如果访问的不是DB区域,此处为00 00;
*11(1 byte): Area: 0x84= data block(DB); 0X82= outputs(Q); 0x81=inputs(I); 0x83= Flags(M); 0x1d= S7 timers(T); 0x1c= S7counters(C);
*12(3 byte):address- start address from zero bit
*5~*12构成了一个基本的数据请求单元[Item],对多个不同地址区域的数据请求,就是有多个[Item]构成的。
Parameter部分的数据结构可以总结为:
[Function code ]+ [Item count] + Item[1] + Item[2] . . . Item[n]
<3>Data