Je pense que cette solution est bonne. L'idée est de convertir le fichier CSV dans un fichier binaire (une séquence de flottants). Après la conversion, vous pouvez fournir le fichier binaire au MCU et utiliser la fonction getVal() (indiquée ci-dessous) pour lire les données.
Ce code convertit un data.csv de fichier dans un fichier data.bin:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
int main(void)
{
FILE * fin=NULL;
FILE * fout=NULL;
char * buff, *tmp;
float val;
int cnt=0;
size_t bsize;
fin=fopen("data.csv","r");
if (fin==NULL) {
perror("1 - Error");
return errno;
}
fseek(fin,0,SEEK_END);
bsize=ftell(fin);
if (!bsize) {
puts("The file doesn't contain data!");
return -1;
}
buff=malloc(bsize+1);
if (buff==NULL) {
perror("2 - Error");
return errno;
}
fout=fopen("data.bin","w");
if (fout==NULL) {
perror("3 - Error");
return errno;
}
fseek(fin,0,SEEK_SET);
fread(buff,1,bsize,fin);
buff[bsize]=0;
fclose(fin);
tmp=buff;
do {
val=strtof(tmp,&tmp);
printf("%12g ",val);
if ((++cnt)%5==0)
puts("");
fwrite(&val,sizeof(val),1,fout);
if (*tmp!=0)
tmp++;
} while(*tmp!=0);
puts("\nEnd of conversion\n");
fclose(fout);
free(buff);
return 0;
}
Cette fonction lire un champ à partir d'un fichier binaire qui contient des données float au format binaire:
float getVal(FILE *f, size_t index)
{
float val;
fseek(f,index*sizeof(val),SEEK_SET);
fread(&val,1,sizeof(val),f);
return val;
}
Cette fonction principale utilise la fonction getVal() pour prendre les données du fichier data.bin:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
float getVal(FILE *f, size_t index);
int main(void)
{
FILE * fin;
size_t index;
fin=fopen("data.bin","r");
if (fin==NULL) {
perror("1 - Error");
return errno;
}
while(1) {
printf("Insert index from 1 to n [Insert 0 to end]: ");
scanf("%lu",&index);
if (index==0)
break;
printf("%g\n",getVal(fin,index-1));
}
fclose(fin);
return 0 ;
}
Pourriez-vous montrer un exemple de Données CSV? Quel langage de programmation utilisez-vous? –
11,97; 130,72; 12,72; 131,61; 13,47; 132,52; 14,20; 133,43; 14,92; 134,36; 15,63; 135,30; 16,33; 136,26; 17,01; 137,22; 17,69; 138,20; 18,35; 139,20; 18,99; 140,20; 19,63; 141,22; 20,25; ; 142.26; 20.85; 143.31; 21.44; 144.37; 22.02; 145.44; 22.58; 146.53; 23.12; 147.63; 23.65; 148.75; 24.16; 149.88; 24.65; 151.02; 25.13; 152.18; 25.59; 153.35; 26.03; 154.53; 26.45; 155.73; ; 26,85; ....... – Fabrice
Je travaille sur STM32F429; J'utilise C++ – Fabrice