需求文档

This commit is contained in:
claw committed 2026-04-03 17:01:39 +08:00
1 parent 9f3b8cf65a
commit 09fffb1a33
1 file changed
+100
+100
View File
@@ -0,0 +1,100 @@
西门子s7通信也是一种tcp通信,比slmp通信多两次协商。参考slmp通信代码,使用myrtumst作为数据配置。用slmp和fins通信类似的代码格式,完成s7 tcpc的代码。
通信之前需要COTP协商
uint8_t comm1[] =
{ 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC0, 0x01, 0x0A, 0xC1, 0x02, 0x01, 0x00, 0xC2, 0x02, 0x03, 0x02 };
和S7Comm协商
uint8_t comm2[] = { 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0xE0 };
如果你也不知道西门子 s7 通信 协商报文的回应,那先不管报文回应吧,后续再完善,我目前不知道,有回应就算通过。
两次协商之后。就可以开始通信了
void to_big_endian_16(uint16_t value, uint8_t *dest) {
dest[0] = (uint8_t) (value >> 8); // 高字节
dest[1] = (uint8_t) (value & 0xFF); // 低字节
}
void to_big_endian_24(uint32_t value, uint8_t *dest) {
dest[0] = (uint8_t) (value >> 16); // 最高字节
dest[1] = (uint8_t) (value >> 8); // 次高字节
dest[2] = (uint8_t) (value & 0xFF); // 最低字节
}
typedef union {
uint8_t frame[31];
struct {
uint8_t padding1[23];
uint8_t read_size[2];
uint8_t db_id[2];
uint8_t padding2[1];
uint8_t start_address[3];
};
} s7ReadFrame;
typedef union {
uint8_t frame[1024];
struct {
uint8_t padding1[2];
uint8_t frame_size[2];
uint8_t padding2[11];
uint8_t dataframe_size[2];
uint8_t padding3[6];
uint8_t data_size[2];
uint8_t db_id[2];
uint8_t padding4[1];
uint8_t start_address[3];
uint8_t padding5[2];
uint8_t databyte_size_t8[2];
uint8_t data[800];
};
} s7WriteFrame;
s7ReadFrame readframe = { .frame = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01,
0x12, 0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 } };
s7WriteFrame writeframe = { .frame = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05,
0x01, 0x12, 0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
读取报文准备
to_big_endian_16(get_register(DBID1_IDX), readframe.db_id);
to_big_endian_24(get_register(START1_IDX) * 8, readframe.start_address);
to_big_endian_16(get_register(SIZE1_IDX) * 2, readframe.read_size);
memcpy(sendbuf, readframe.frame, read_size);
sendbufsize = read_size;
写入准备报文
uint16_t prepare_write_frame(uint8_t *write, uint16_t writebyte_size) {
if (writebyte_size == 0) {
return 0;
}
uint16_t frame_size = 35 + writebyte_size;
to_big_endian_16(frame_size, writeframe.frame_size);
to_big_endian_16(4 + writebyte_size, writeframe.dataframe_size);
to_big_endian_16(writebyte_size, writeframe.data_size);
to_big_endian_16(get_register(DBID2_IDX), writeframe.db_id);
to_big_endian_24(get_register(START2_IDX) * 8, writeframe.start_address);
to_big_endian_16(writebyte_size * 8, writeframe.databyte_size_t8);
memcpy(writeframe.data, write, writebyte_size);
return frame_size;
}
写入的回应不用管,读取的回应要解析到本地。
看完需求不着急写,有疑问先和我确定,写的话写入到s7tcpmaster.c。 假设你是专业的编程专家,确保代码正确。