首页 > 解决方案 > 如何通过 MATLAB 向 ESP32 串口发送一个大数组(96000 个样本)?

问题描述

简而言之,我正在 MATLAB 中读取 .wav 文件,以便将其发送到 ESP32 进行 FFT 分析。有问题的 .wav 文件包含电晕效应的记录。输入 MATLAB 时,我的文件有 96223 个样本。

现在,我正在尝试取回校验和,以便知道数据已正确发送。

我已经尝试使用我为较小样本量编写的代码。例如,当我发送 200 个样本时,我得到了正确的校验和,尽管代码花费的时间比我想要的要长,这并不好。不仅如此,而且由于超时,我再也没有得到任何回报。

这是我的 MATLAB 代码:

esp = serial('COM3');
set(esp, 'DataBits' , 8);
set(esp, 'StopBits', 1);
set(esp, 'BaudRate', 9600);
set(esp, 'Parity', 'none');
set(esp, 'terminator', 'LF');

%filename = 'test100.wav';
%corona = audioread(filename);

load('corona')
fopen(esp);
pause(0.1)
for i = 1:200
   fprintf(esp, '%5.9f\n', corona(i,1)); 
   pause(0.1);       
end
output = fscanf(esp, '%f\n') %read the checksum
fclose(instrfind);

这是我的 Arduino 代码:

#include <Arduino.h>
float sentData[200]; //initialize data array
int i = 0;
const int ledPin = 26;
float checksum = 0;
int CNT = 0;

void printFloat(float value, int places);

void setup()
{
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

  while (Serial.available() < 200)
  {
    digitalWrite(ledPin, HIGH); //keep the LED on while the data is being sent
  }

  while (Serial.available() != 0)
  {
    sentData[i] = Serial.parseFloat(); //parse the data to the array
    i++;
  }
  Serial.flush();
  delay(500);
  digitalWrite(ledPin, LOW); //turn off the LED when data is fully parsed

  for (size_t x = 0; x < 200; ++x)
  {
    checksum += sentData[x]; //calculate the sum of all elements in the sentData array
  }
  printFloat(checksum, 10); //send the checksum to the serial port for reading
}
void loop()
{
}

void printFloat(float value, int places)
{
  // this is used to cast digits
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;

  // if this rounding step isn't here, the value  54.321 prints as 54.3209

  // calculate rounding term d:   0.5/pow(10,places)
  float d = 0.5;
  if (value < 0)
    d *= -1.0;
  // divide by ten for each decimal place
  for (i = 0; i < places; i++)
    d /= 10.0;
  tempfloat += d;

  // first get value tens to be the large power of ten less than value

  if (value < 0)
    tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat)
  {
    tens *= 10.0;
    tenscount += 1;
  }

  // write out the negative if needed
  if (value < 0)
    Serial.print('-');

  if (tenscount == 0)
    Serial.print(0, DEC);

  for (i = 0; i < tenscount; i++)
  {
    digit = (int)(tempfloat / tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }

  // if no places after decimal, stop now and return
  if (places <= 0)
    return;

  // otherwise, write the point and continue on
  Serial.print('.');

  // now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
  for (i = 0; i < places; i++)
  {
    tempfloat *= 10.0;
    digit = (int)tempfloat;
    Serial.print(digit, DEC);
    // once written, subtract off that digit
    tempfloat = tempfloat - (float)digit;
  }
}

我希望能取回校验和,但在使用非常大的样本量时会超时。我还应该补充一点,即使 ESP32 应该能够处理我的文件,我也不能将整个文件推送到串行端口,因为我收到缓冲区溢出错误。有针对这个的解决方法吗?

标签: arraysmatlabarduinoserial-portarduino-c++

解决方案


首先%5.9f对我来说没有意义。

那是最少 5 个字符,精度为 9 位。那 5 没有意义,因为您将始终拥有至少 11 个字符,精度为 9 位

然后让我为你做一些数学:

96000 个样本,每个 12 个字符(包括\n),总共 10368000 位。

在 9600 波特下,传输时间为 1080 秒。-> 18 分钟。

当您在每个样本后添加 0.1 秒暂停时,您会再添加 9600 秒。

这使您总共有 178 分钟(3 小时)的转移时间。

你能指望什么?

对于 200 个样本,它仍然是 22.25 秒。


推荐阅读