数据流的传送,储存,读取方案
本文主要介绍数据流的传送,储存,读取方法。用到的程序语言有c语言和matlab语言。在分析一个复杂过程的时候,我们总希望得到在这一过程的所有变量的值,包括输入,中间变量,输出,扰动,反馈等等。本文介绍的一些代码(方法)可以帮的上忙。
数据流通过spi协议传送
其实不只是spi协议,任何协议都可以,只要传送速率足够,我们要做的只是数据的打包。这里所举的例子时把每5场数据打包到一起,便于传送。当然传送的另一端需要有对应的接收程序和解析程序。
//函数功能:进行数据打包,加入帧头,每5场数据作为一包发送。
void send_data(void)
{
//数据准备
Data0[0] = 0;
Data0[1] = 0;
//其他数据更新。
Data0[48] = (int16)AD1;
Data0[49] = (int16)AD2;
//数据包头准备
if(count++%5==0)
{
*(data+0) = (uint8)(count/5);
*(data+1) = (uint8)((count/5)>>8);
*(data+2) = (uint8)((count/5)>>16);
*(data+3) = (uint8)((count/5)>>24);
}
//数据包装
for(int i=0;i<50;i++)
{
temp_data = Data0[i+1];
*(data + 2 * i + 4 + data_num * 2 * ((count-1)%5)) = temp_data;
*(data + 2 * i + 5 + data_num * 2 * ((count-1)%5)) = temp_data>> 8;
}
if(!(count % 5))
{
spi_start();
spi_send_data(data, 512);
spi_end();
}
}
SPI协议写入sd卡
void running(void)
{
for(;;)
{
if(Isqueuefull())
{
number_write ++;
uint8 *p = getheadptr();
int m = number_write-1 ;
int k = WRITE_BASE_ADDRESS+m;
disk_write(p, k, 1);
queueout();
}
}
}
程序在执行过程中会有一个队列储存spi协议收到的数据。一旦队列中有数据,即if(Isqueuefull()),就进行数据写入。首先得到队列中数据存放的首地址,即getheadptr()函数,然后得到sd卡要写入的地址k,调用 disk_write 函数进行写入。disk_write函数属于底层函数,不同的硬件实现方法不同,可以在芯片底层函数中找到。写入完成后记得清除写过的数据,调用queueout()。注意sd卡数据写入只能以扇区为单位进行。
MATLAB读取sd卡
写入sd卡的数据怎么读取以进行分析处理呢?这里提供的方案是用matlab读取。
for a = 67:1:90
if isdir(strcat(char(a),'://')) && exist(strcat(char(a),'://','1.txt'),'file') == 2
DirFlag = 1;
break;
end
end
if DirFlag == 0
msgbox('插入SD卡!!');
end
warning('初始化完毕,开始处理...');
%%
for FileNum = 1:1:60
FileName = strcat(char(a),'://',num2str(FileNum),'.txt');
%
warning(['正在处理文本文档 ',num2str(FileName),'...']);
fp = fopen(FileName,'r');
All_Data = fread(fp);
if( All_Data==0&FileNum==1)
msgbox('SD卡没有数据!!!!');
error('SD卡没有数据!!!!');
end
if( All_Data==0)
break;
end
All_Data=All_Data(1:128*1024);
all_data(:,FileNum)= All_Data;
fclose(fp);
end
%%
colcount = size(all_data,2);
allvaliddata = [];
for i = 1:colcount
All_Data = all_data(:,i);
for j = 1:sectorperfile
sectordata = All_Data(((j-1)*bytepersector+1):(j*bytepersector));
if sectordata == 0
losssectorcount = losssectorcount + 1;
else
len = length(allvaliddata);
allvaliddata((len+1):(len+bytepersector)) = sectordata;
end
end
end
%%
validsectorcount = length(allvaliddata)/bytepersector;
datadata = zeros(validsectorcount*framepersector,unitperframe);
for i = 1:validsectorcount
sectordata = allvaliddata(((i-1)*bytepersector+1):(i*bytepersector));
squencenum(length(squencenum)+1) = sectordata(4)*bitshift(1,24) + sectordata(3)*bitshift(1,16) + sectordata(2)*bitshift(1,8) + sectordata(1);
for j = 1:framepersector
for k = 1:unitperframe
datadata((i-1)*framepersector+j,k) = 0;
for m = bytesperunit:-1:1
datadata((i-1)*framepersector+j,k) = datadata((i-1)*framepersector+j,k) + sectordata((j-1)*unitperframe*bytesperunit+(k-1)*bytesperunit+m+4)*bitshift(1,8*(m-1));
end
if sectordata((j-1)*unitperframe*bytesperunit+(k-1)*bytesperunit+bytesperunit+4)>=128
datadata((i-1)*framepersector+j,k) = datadata((i-1)*framepersector+j,k) -65536;
end
end
end
End
至此,分析复杂变化过程所需要的数据传输,保存,读取方法已经介绍完毕。