指令格式 帧头 指令版本号 指令长度(从设备码算起) 地址长度 地址位1 地址位2 地址位3 地址位4 地址位5 设备码 功能码 数据位 数据位... 指令控制码 指令序列号 保留位1 保留位2 8a 8a 8a 01 xx 0x05 00 00 00 00 01 01 01 03 ... 0x00 0x00 FF FF1. 有如下数组 char dd[] = {8A 8A 8A 01 07 05 00 00 00 00 01 01 02 01 01 01 FF FF 8A 8A 8A 01 16 05 00 00 00 00 01 02 02 01 00 00 ff ff 8A 8A 8A 01 07 05 00 00 00 00 01 01 02 01 01 01 FF FF} 请写一段代码,将上述数组按照指令格式解析到三个数组
时间: 2023-08-22 22:04:29 浏览: 40
根据指令格式的定义,我们可以将其拆分为不同的字段,并依次解析每个字段,将其存储到对应的数组中。具体实现如下:
```c++
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
struct Instruction {
char header[3];
char version;
int length;
int address_length;
int address[5];
char device_code;
char function_code;
vector<char> data;
char control_code;
int sequence_number;
char reserve1;
char reserve2;
};
int main() {
char dd[] = {0x8A, 0x8A, 0x8A, 0x01, 0x07, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0xFF, 0xFF, 0x8A, 0x8A, 0x8A, 0x01, 0x16, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x01, 0x00, 0x00, 0xff, 0xff, 0x8A, 0x8A, 0x8A, 0x01, 0x07, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0xFF, 0xFF};
vector<Instruction> instructions;
int index = 0;
while (index < sizeof(dd)) {
Instruction ins;
memcpy(ins.header, dd + index, 3);
index += 3;
memcpy(&ins.version, dd + index, sizeof(char));
index += sizeof(char);
memcpy(&ins.length, dd + index, sizeof(int));
index += sizeof(int);
memcpy(&ins.address_length, dd + index, sizeof(int));
index += sizeof(int);
for (int i = 0; i < 5; i++) {
memcpy(&ins.address[i], dd + index, sizeof(int));
index += sizeof(int);
}
memcpy(&ins.device_code, dd + index, sizeof(char));
index += sizeof(char);
memcpy(&ins.function_code, dd + index, sizeof(char));
index += sizeof(char);
int data_length = ins.length - ins.address_length - 4;
for (int i = 0; i < data_length; i++) {
ins.data.push_back(*(dd + index));
index++;
}
memcpy(&ins.control_code, dd + index, sizeof(char));
index += sizeof(char);
memcpy(&ins.sequence_number, dd + index, sizeof(int));
index += sizeof(int);
memcpy(&ins.reserve1, dd + index, sizeof(char));
index += sizeof(char);
memcpy(&ins.reserve2, dd + index, sizeof(char));
index += sizeof(char);
instructions.push_back(ins);
}
vector<char> array1, array2, array3;
for (auto ins : instructions) {
if (ins.function_code == 0x01) {
array1.insert(array1.end(), ins.data.begin(), ins.data.end());
} else if (ins.function_code == 0x02) {
array2.insert(array2.end(), ins.data.begin(), ins.data.end());
} else if (ins.function_code == 0x03) {
array3.insert(array3.end(), ins.data.begin(), ins.data.end());
}
}
return 0;
}
```
这段代码会先将原数组中的每个指令解析为一个 `Instruction` 结构体,然后根据指令的功能码将数据存储到对应的数组中。需要注意的是,这里只是简单示例,实际应用中需要根据具体需求进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)