从硬件序列读取无符号长数组?

Read Array of Unsigned Long from the hardware serial?

我得到了一个无符号长整数数组

unsigned long readings[ 64 ];

我想从硬件串行接口填充。 反正没有直接从里面读取unsigned long的函数。

如果你的 ASCII 字符是串行的,那么你可以将字符块转换成你需要的任何格式:

 unsigned long my_long = 0;
 char inputChunk[] ="2273543"; // you fill that from serial
 my_long = strtoul(inputChunk, NULL, 10);
 Serial.print(my_long);
readings[0] = my_long;

因为你没有给出数据如何串行传输或如何区分不同垃圾(是'\n'还是其他一些终止符)的例子,这只是 ASCII 的基本方法。
正如你在此处尝试使用 '\n' 和 ASCII 示例:

 unsigned long my_long = 0;
 char inputChunk[16] = {'[=11=]'}; // size big enough you fill that from serial

uint8_t strIndex = 0;
uint8_t longCounter = 0;
while (Serial.available())  {
 char readChar = Serial.read();
 if (readChar == '\n') {
    my_long = strtoul(inputChunk, NULL, 10);
    break;
 }
 else {
    inputChunk[strIndex] = readChar;
    strIndex++;
    inputChunk[strIndex] = '[=11=]]; // Keep the array NULL-terminated
 }
 Serial.print(my_long);
 readings[longCounter] = my_long;
 longCounter++;
 if (longCounter>=64) Serial.print("readings[] is full")
}

我喜欢为此使用联合,让你免于很多讨厌的转换。

union{
   uint8_t asBytes[SERIAL_ARRAY_LEN];
   unsigned long asULongs[SERIAL_ARRAY_LEN/sizeof(unsigned long)];
}data;

//use memcpy, or you could for while through and transfer byte by byte;
memcpy(sizeof(SERIAL_ARRAY_LEN, data.asBytes, serialBuffer);

for (int i = 0; i < SERIAL_ARRAY_LEN/sizeof(unsigned long); i++){
   ESP_LOGD(TAG, "%d", data.asULongs[i]);
}