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 比实际少一个即可完整输出列表,这是为何?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.