struct node_data {
char ip_address[16]; // 对端地址
int port; // 对端端口
uint8_t uint_id; // 单元标识
int device_counts; // 传感器数量
uint8_t device_name[15][6]; // 传感器列表
};
// 构建报文
void constructMessage(uint8_t* buffer, uint8_t unit_id, uint8_t device_name[6])
{
uint8_t transaction[] = { 0x00,0x00 }; // 事务标识
uint8_t protocol_id[] = { 0x00,0x00 }; // 协议标识
uint8_t length[] = { 0x00,0x0D }; // 长度
uint8_t f_code[] = { 0x03 }; // 功能码
uint8_t command_code[] = { 0x20,0x0A,0x00,0x0A }; // 命令码
uint8_t data_length[] = { 0x06 }; // 数据长度
size_t size = sizeof(uint8_t);
memcpy(buffer + size * 0, transaction, size * 2); // 0
memcpy(buffer + size * 2, protocol_id, size * 2);// 2
memcpy(buffer + size * 4, length, size * 2); // 4
memcpy(buffer + size * 6, &unit_id, size); // 6
memcpy(buffer + size * 7, f_code, size); // 7
memcpy(buffer + size * 8, command_code, size * 4); // 12
memcpy(buffer + size * 12, data_length, size); // 13
memcpy(buffer + size * 13, device_name, size * 6);
for (int i = 0; i < 6; i++)
{
printf("0x%02x,", device_name[i]);
}
printf("\n");
}
// 功能函数
int func(struct node_data data)
{
int send_len = sizeof(uint8_t) * 19;
uint8_t* send_data = malloc(send_len);
for (int i = 0; i < data.device_counts; i++)
{
constructMessage(send_data, data.uint_id, &data.device_name[i][6]);
}
free(send_data);
return 0;
}
int main(void)
{
struct node_data node_datas[2] = {
{
"127.0.0.1",
2021,
0x01,
13,
{ {0x21,0x69,0x10,0x00,0x00,0x01},{0x21,0x69,0x10,0x00,0x00,0x02},{0x21,0x69,0x10,0x00,0x00,0x03},{0x21,0x69,0x10,0x00,0x00,0x04},{0x21,0x69,0x10,0x00,0x00,0x05},
{0x21,0x69,0x10,0x00,0x00,0x06},{0x21,0x69,0x10,0x00,0x00,0x07},{0x21,0x69,0x10,0x00,0x00,0x08},{0x21,0x69,0x10,0x00,0x00,0x09},{0x21,0x69,0x10,0x00,0x00,0x10},{0x21,0x69,0x10,0x00,0x00,0x11},
{0x21,0x69,0x10,0x00,0x00,0x12},{0x21,0x69,0x10,0x00,0x00,0x13} }
},
{
"127.0.0.1",
2021,
0x01,
14,
{ {0x21,0x69,0x10,0x00,0x00,0x14},{0x21,0x69,0x10,0x00,0x00,0x15},{0x21,0x69,0x10,0x00,0x00,0x16},{0x21,0x69,0x10,0x00,0x00,0x17},{0x21,0x69,0x10,0x00,0x00,0x18},
{0x21,0x69,0x10,0x00,0x00,0x19},{0x21,0x69,0x10,0x00,0x00,0x20},{0x21,0x69,0x10,0x00,0x00,0x21},{0x21,0x69,0x10,0x00,0x00,0x22},{0x21,0x69,0x10,0x00,0x00,0x23},{0x21,0x69,0x10,0x00,0x00,0x24},
{0x21,0x69,0x10,0x00,0x00,0x25},{0x21,0x69,0x10,0x00,0x00,0x26},{0x21,0x69,0x10,0x00,0x00,0x27} }
}
};
for (int i = 0; i < 2; i++)
{
func(node_datas[i]);
}
return 0;
}
程序的主要功能是跟一个厂商的 TCP 设备通信,发送报文获取传感器采集到的数据,{0x21,0x69,0x10,0x00,0x00,0x26}是每个传感器的地址
现在遇到的问题是constructMessage
中打印出来的设备列表如下
0x21,0x69,0x10,0x00,0x00,0x02,
0x21,0x69,0x10,0x00,0x00,0x03,
0x21,0x69,0x10,0x00,0x00,0x04,
0x21,0x69,0x10,0x00,0x00,0x05,
0x21,0x69,0x10,0x00,0x00,0x06,
0x21,0x69,0x10,0x00,0x00,0x07,
0x21,0x69,0x10,0x00,0x00,0x08,
0x21,0x69,0x10,0x00,0x00,0x09,
0x21,0x69,0x10,0x00,0x00,0x10,
0x21,0x69,0x10,0x00,0x00,0x11,
0x21,0x69,0x10,0x00,0x00,0x12,
0x21,0x69,0x10,0x00,0x00,0x13,
0x00,0x00,0x00,0x00,0x00,0x00,
0x21,0x69,0x10,0x00,0x00,0x15,
0x21,0x69,0x10,0x00,0x00,0x16,
0x21,0x69,0x10,0x00,0x00,0x17,
0x21,0x69,0x10,0x00,0x00,0x18,
0x21,0x69,0x10,0x00,0x00,0x19,
0x21,0x69,0x10,0x00,0x00,0x20,
0x21,0x69,0x10,0x00,0x00,0x21,
0x21,0x69,0x10,0x00,0x00,0x22,
0x21,0x69,0x10,0x00,0x00,0x23,
0x21,0x69,0x10,0x00,0x00,0x24,
0x21,0x69,0x10,0x00,0x00,0x25,
0x21,0x69,0x10,0x00,0x00,0x26,
0x21,0x69,0x10,0x00,0x00,0x27,
0x00,0x00,0x00,0x00,0x00,0x00,
少了第一个设备,多了一个 00 号
将 func 中循环的 i 从 -1 开始 ,node_data 中的 device_counts 比实际少一个即可完整输出列表,这是为何?
1
hello2090 2021-12-28 14:32:00 +08:00
很久没搞 C++了,
&data.device_name[i][6] 这个不应该是 &data.device_name[i]吗? |
2
hello2090 2021-12-28 14:33:24 +08:00
这种东西设个断点或者打印一下 看看每步的变量值就行了啦
|
3
aviar 2021-12-28 14:37:58 +08:00 1
&data.device_name[i][6]
=> &data.device_name[i][0] |
4
hello2090 2021-12-28 14:38:24 +08:00 1
你
for (int i = 0; i < data.device_counts; i++) { constructMessage(send_data, data.uint_id, &data.device_name[i][6]); } 每一步传入一个设备名,第 i 个设备名应该是 data.device_name[i]吧 |
5
commoccoom OP @hello2090 data.device_name[i][6]是一个 uint8_t 的数组,没用[6]只是一个 uint8_t 了
|
6
commoccoom OP @aviar 感谢老哥,对了😭,搞了一上午了。
|
7
yolee599 2021-12-28 14:46:59 +08:00 1
把:
constructMessage(send_data, data.uint_id, &data.device_name[i][6]); 修改为: constructMessage(send_data, data.uint_id, data.device_name[i]); |
8
commoccoom OP @hello2090 感谢老哥,搞清楚了,确实可以直接传 data.device_name[i]
|
9
TSai2019 2021-12-28 20:49:17 +08:00 via Android
用 c++的 vector string 就不会迷糊了
|
10
commoccoom OP @TSai2019 不会 C++...😔
|