[英]C++ arduino library returning an array of ints with incorrect values
I am currently writing a library for Arduino using C++ to take an 8 character hexadecimal RFID string from a reader and convert that string into bytes, treating every two characters as part of a single byte such that "1234ABCD" becomes 0X12, 0X34, 0XAB, 0XCD.我目前正在使用 C++ 为 Arduino 编写一个库,以从阅读器中获取 8 个字符的十六进制 RFID 字符串并将该字符串转换为字节,将每两个字符视为单个字节的一部分,以便“1234ABCD”变为 0X12、0X34、0XAB, 0XCD。 These bytes are then converted into the integer values that represent them such that the previous values become 18, 52, 171 and 205 respectively.
然后将这些字节转换为表示它们的整数值,使之前的值分别变为 18、52、171 和 205。 These integers will then be placed in an array and returned to the caller.
然后将这些整数放在一个数组中并返回给调用者。 As the RFID string is hexadecimal I am multiplying the integer value of the first character in each pair by 16.
由于 RFID 字符串是十六进制的,我将每对中第一个字符的整数值乘以 16。
The C++ library: C++ 库:
int AccessPortal::hexToInt(char character) {
if(int(character) - '0' < 10) {
return int(character) - '0';
}
else {
if(isupper(character)) {
return int(character) - '7';
}
else {
return int(character) - 'W';
}
}
}
int* AccessPortal::getByteArray(std::string uid) {
int byteArray[4];
int intArray[8];
int counter = 0;
for(int i = 0; i < 8; i++) {
if(i % 2 == 0) {
intArray[i] = hexToInt(uid[i]) * 16;
}
else {
intArray[i] = hexToInt(uid[i]);
}
}
for(int i = 1; i < 8; i = i + 2) {
byteArray[counter] = intArray[i - 1] + intArray[i];
counter ++;
}
return byteArray;
}
The Arduino code: Arduino代码:
void setup() {
// put your setup code here, to run once:
AccessPortal access(); //constructor
int *h;
h = access.getByteArray("1234ABCD");
for(int i = 0; i < 4; i++) {
Serial.println(h[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
However, when I run the program the following is printed to serial:但是,当我运行程序时,以下内容会打印到串行:
1073741740
1073741680
1073670364
1075849820
This is strange because when I run the functions in the C++ library separately and print the results to the terminal the values are correct.这很奇怪,因为当我单独运行 C++ 库中的函数并将结果打印到终端时,值是正确的。 I am guessing this means the error is occurring in the way the array is being returned and/or accessed in the Arduino code, however I am unsure.
我猜这意味着错误发生在 Arduino 代码中返回和/或访问数组的方式中,但我不确定。
As others have said, a pointer to a local variable becomes invalid as soon as the local variable goes out of scope, so it shouldn't be returned.正如其他人所说,一旦局部变量超出范围,指向局部变量的指针就会失效,因此不应返回它。 You can instead create a
ByteArray
object that you can return-by-value from your functions.您可以改为创建一个
ByteArray
对象,您可以从函数中按值返回该对象。 Example using ArduinoSTL:使用 ArduinoSTL 的示例:
#include <ArduinoSTL.h>
#include <string>
#include <cstdlib>
struct ByteArray {
uint32_t m_value;
bool m_valid;
// convert hex string to uint32_t value
ByteArray(const std::string& uid) :
m_value(std::strtoul(uid.c_str(), nullptr, 16)),
m_valid(true)
{
if(uid.size()!=8) m_valid = false;
}
// converting assignment operator
ByteArray& operator=(const std::string& uid) {
m_value = std::strtoul(uid.c_str(), nullptr, 16);
if(uid.size()==8) m_valid = true;
else m_valid = false;
return *this;
}
// subscript operator to access the individual bytes (as ints)
int operator[](size_t x) const {
if(x>3) return -1;
return (m_value>>((3-x)*8))&0xFF;
}
};
struct AccessPortal {
// change the getByteArray signature to this
ByteArray getByteArray(const std::string&);
};
ByteArray AccessPortal::getByteArray(const std::string& uid) {
// return by value - copy elision
return ByteArray(uid);
}
void setup() {
AccessPortal access; //default constructing
ByteArray h = access.getByteArray("1234ABCD");
Serial.println(h.m_value); // dec: 305441741 hex: 1234ABCD
for(int i = 0; i < 4; i++) {
Serial.println(h[i]); // dec: 18, 52, 171, 205 hex: 12, 34, AB, CD
}
}
void loop() {
//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.