请教一个 C 中循环的问题

2021-12-28 14:03:06 +08:00
 commoccoom
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 比实际少一个即可完整输出列表,这是为何?

1924 次点击
所在节点    C
10 条回复
hello2090
2021-12-28 14:32:00 +08:00
很久没搞 C++了,
&data.device_name[i][6] 这个不应该是 &data.device_name[i]吗?
hello2090
2021-12-28 14:33:24 +08:00
这种东西设个断点或者打印一下 看看每步的变量值就行了啦
aviar
2021-12-28 14:37:58 +08:00
&data.device_name[i][6]
=>
&data.device_name[i][0]
hello2090
2021-12-28 14:38:24 +08:00

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]吧
commoccoom
2021-12-28 14:43:24 +08:00
@hello2090 data.device_name[i][6]是一个 uint8_t 的数组,没用[6]只是一个 uint8_t 了
commoccoom
2021-12-28 14:44:08 +08:00
@aviar 感谢老哥,对了😭,搞了一上午了。
yolee599
2021-12-28 14:46:59 +08:00
把:
constructMessage(send_data, data.uint_id, &data.device_name[i][6]);

修改为:
constructMessage(send_data, data.uint_id, data.device_name[i]);
commoccoom
2021-12-28 14:51:55 +08:00
@hello2090 感谢老哥,搞清楚了,确实可以直接传 data.device_name[i]
TSai2019
2021-12-28 20:49:17 +08:00
用 c++的 vector string 就不会迷糊了
commoccoom
2021-12-28 21:34:26 +08:00
@TSai2019 不会 C++...😔

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824869

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX