1

我有一个 NFC 扫描仪,它获取标签的 UID 并将其存储在byte TagReadB[4].

然后我需要将它与大量其他字节数组进行比较,以查看它是否与它们匹配(即,它是否是可识别的标签)。

识别的标签是这样存储的。

uint8_t RED1[4] = {0x15, 0x4C, 0xA9, 0xFB};
uint8_t RED2[4] = {0x67, 0xFC, 0x8A, 0x16};
uint8_t RED3[4] = {0xB7, 0xC4, 0xEA, 0xA4};
uint8_t RED4[4] = {0xE7, 0x32, 0xE7, 0xA4};
uint8_t RED5[4] = {0x87, 0x33, 0xED, 0x74};
uint8_t RED6[4] = {0xB7, 0x56, 0xFA, 0xA4};
uint8_t RED7[4] = {0xB5, 0x8A, 0xB6, 0xFB};
uint8_t RED8[4] = {0x07, 0xA6, 0xF3, 0xA4};
uint8_t RED9[4] = {0xE5, 0xB2, 0x9E, 0xFB};
uint8_t RED10[4] = {0x87, 0xF8, 0x8A, 0x16};
uint8_t RED11[4] = {0x47, 0x93, 0xDA, 0xA4};
uint8_t RED12[4] = {0x87, 0x1D, 0x8A, 0x16};
uint8_t RED13[4] = {0x85, 0x83, 0xB1, 0xFB};
uint8_t RED14[4] = {0x55, 0x33, 0x9C, 0xFB};
uint8_t RED15[4] = {0x45, 0x39, 0xB7, 0xFB};
uint8_t RED16[4] = {0xA5, 0xED, 0xA5, 0xFB};
uint8_t RED17[4] = {0x85, 0x6E, 0xA1, 0xFB};
uint8_t RED18[4] = {0x17, 0xC3, 0xEF, 0xA4};
uint8_t RED19[4] = {0x37, 0xCA, 0xEB, 0xA4};
uint8_t RED20[4] = {0x15, 0x66, 0xA0, 0xFB};
uint8_t YELLOW1[4] = {0x27, 0x44, 0x73, 0xE5};
uint8_t YELLOW2[4] = {0xD7, 0x44, 0xCB, 0xE2};
uint8_t YELLOW3[4] = {0x87, 0x6E, 0x78, 0xE5};
uint8_t YELLOW4[4] = {0x27, 0x49, 0x63, 0xE5};
uint8_t YELLOW5[4] = {0xA7, 0x0D, 0x6C, 0xE5};
uint8_t YELLOW6[4] = {0x87, 0x96, 0x69, 0xE5};
uint8_t YELLOW7[4] = {0x37, 0xF9, 0xCA, 0xE2};
uint8_t YELLOW8[4] = {0xD7, 0xAE, 0xCB, 0xE2};
uint8_t YELLOW9[4] = {0xA7, 0xE3, 0x78, 0xE5};
uint8_t YELLOW10[4] = {0x47, 0x72, 0x90, 0xE5};
uint8_t BLUE1[4] = {0x73, 0xD5, 0xB7, 0xAC};
uint8_t BLUE2[4] = {0x1D, 0x5D, 0xFC, 0x69};
uint8_t BLUE3[4] = {0xBD, 0xEC, 0x8F, 0x12};
uint8_t BLUE4[4] = {0xE6, 0x9F, 0x54, 0x73};
uint8_t BLUE5[4] = {0x38, 0x74, 0x7E, 0x33};
uint8_t BLUE6[4] = {0x6D, 0x05, 0x3E, 0x12};
uint8_t BLUE7[4] = {0x9C, 0x6B, 0x4A, 0x73};

我可以执行以下操作,但必须有一种更简单的方法,而不必重复 20 条 OR 语句?

if (!memcmp(TagReadB, RED1, 4) || !memcmp(TagReadB, RED2, 4) || ...)    {

        //do something

    }

更好的是,最好测试它匹配的已识别标签的颜色,因为每种颜色都会有相同的动作。

例如,如果它是红色标签做 x,黄色标签做 y,蓝色标签做 z。

非常感谢。

4

2 回答 2

1

您可以将数据扩展到

struct {uint8_t tag[4], uint8_t type}
data[] = {
   { {0x15, 0x4C, 0xA9, 0xFB}, RED}, 
// ...
   { {0xD7, 0xAE, 0xCB, 0xE2}, YELLOW },
// ...
};

#define RED 1
// etc. 

(或 const 字节或枚举)

然后循环此列表直到匹配并返回结果data[i].type 或返回 0 表示“未找到”

于 2019-04-07T11:27:20.707 回答
0

您可以将标签存储在多维数组中:

uint8_t RED_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
};

uint8_t YELLOW_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
};

uint8_t BLUE_TAGS[][4] = {
    {0x15, 0x4C, 0xA9, 0xFB},
    {0x67, 0xFC, 0x8A, 0x16},
    ...
}

现在您可以使用 for 循环检查标签是否在多维数组之一中:

int is_tag_in_array(uint8_t array[][4], int size, uint8_t tag[4]) {
    for (int i=0;i<size;i++) {
        if (memcmp(array[i], tag, 4) == 0)
            return 1;
    }
    return 0;
}

这样您就可以像这样检查颜色:

if (is_tag_in_array(RED_TAGS, 20, TagReadB)) {
    // Do something for a red tag
} else if (is_tag_in_array(YELLOW_TAGS, 10, TagReadB)) {
    // Do something for a yellow tag
} else if (is_tag_in_array(BLUE_TAGS, 7, TagReadB) {
    // Do something for a blue tag
} else {
    // Didn't match any color
}
于 2019-04-07T00:04:59.767 回答