类和构造函数中的函数根本不起作用,并且不返回任何错误

Functions inside class and constructor don't work at all and don't return any errors

本文关键字:返回 错误 任何 不起作用 构造函数 函数      更新时间:2023-10-16

TL;DR:我在 ESP32 上有以下类,但功能无法按预期工作:

class Wiegand {
private:
std::vector<bool> _array{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t facilityCode = 0;
uint16_t cardNumber = 0;
void calculate(uint32_t decimal) {
for (uint8_t i = 23; i >= 0; --i) {
_array[i] = decimal & 1;
decimal >>= 1;
}
}
public:
Wiegand(uint32_t id) {
calculate(id);
facilityCode = id >> 16;
cardNumber = id & 0xffff;
}
std::vector<bool> getCardID() {
return _array;
}
uint8_t getFacilityCode() {
if(facilityCode == 0) {
return 0;
}        
return facilityCode;
}
uint16_t getCardNumber() {
if(cardNumber == 0) {
return 0;
}        
return cardNumber;
}
};

下面的代码没有输出任何东西:

Wiegand card(86840);
std::vector<bool> cardID = card.getCardID();
for(bool i : cardID) {
Serial.print(i);
}
int foo = card.getFacilityCode();
Serial.println(foo);
Serial.println(card.getCardNumber());

说来话长:

我正在尝试将 1 和 2^24-1 范围内的任何十进制数转换为我放入cardID的 24 位二进制数。

例如:86840应返回000000010101001100111000。接下来,我想获取first 8 bitslast 16 bits,将它们转换为decimal形式并将它们存储在其他变量中。现在剩下的事情是按照韦根协议,将整个二进制数并添加其奇偶校验位以将其转换为 26 位韦根数。

对于无符号变量,这是无限循环,因为无符号变量总是零或更多:

for (uint8_t i = 23; i >= 0; --i)

相反,请使用例如:

for (uint8_t i = 23; i + 1 > 0; --i)