V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
commoccoom
V2EX  ›  C

请教一个 C 中循环的问题

  •  
  •   commoccoom · 2021-12-28 14:03:06 +08:00 · 2059 次点击
    这是一个创建于 1061 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条附言  ·  2021-12-28 14:45:12 +08:00
    根据 3 楼老哥的建议,改成 `&data.device_name[i][0]`即可。
    10 条回复    2021-12-28 21:34:26 +08:00
    hello2090
        1
    hello2090  
       2021-12-28 14:32:00 +08:00
    很久没搞 C++了,
    &data.device_name[i][6] 这个不应该是 &data.device_name[i]吗?
    hello2090
        2
    hello2090  
       2021-12-28 14:33:24 +08:00
    这种东西设个断点或者打印一下 看看每步的变量值就行了啦
    aviar
        3
    aviar  
       2021-12-28 14:37:58 +08:00   ❤️ 1
    &data.device_name[i][6]
    =>
    &data.device_name[i][0]
    hello2090
        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]吧
    commoccoom
        5
    commoccoom  
    OP
       2021-12-28 14:43:24 +08:00
    @hello2090 data.device_name[i][6]是一个 uint8_t 的数组,没用[6]只是一个 uint8_t 了
    commoccoom
        6
    commoccoom  
    OP
       2021-12-28 14:44:08 +08:00
    @aviar 感谢老哥,对了😭,搞了一上午了。
    yolee599
        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]);
    commoccoom
        8
    commoccoom  
    OP
       2021-12-28 14:51:55 +08:00
    @hello2090 感谢老哥,搞清楚了,确实可以直接传 data.device_name[i]
    TSai2019
        9
    TSai2019  
       2021-12-28 20:49:17 +08:00 via Android
    用 c++的 vector string 就不会迷糊了
    commoccoom
        10
    commoccoom  
    OP
       2021-12-28 21:34:26 +08:00
    @TSai2019 不会 C++...😔
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1099 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:01 · PVG 03:01 · LAX 11:01 · JFK 14:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.