Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can I boost my read_serial code?

1 次查看(过去 30 天)
enes kuzucu
enes kuzucu 2017-10-20
关闭: MATLAB Answer Bot 2021-8-20
Hello;
I am currently working on a project about Emg signals which are basicly muscle signals.
For Emg signals i need 1000 sample rate.
With Stm32 microcontroller i'm able to read and send the data to matlab with 4000 hz .
But reading that much data with 1000 hz from matlab is problematic.
I'm sending data with syntax like this :
" .1234/0587.1234/0587.1234/ "
When matlab reads that data it takes "1234" and store it in a variable . also it takes the second part "0587" and store it in another variable
here is my serial reading function:
%
function [sizeVector,varargout]=read2piece(serial)
%coming data is in form like this : 1234.2345/ 1234.2345/
% 1234 first data 2345 sec data
temp=0;
readings = fread(serial,1,'uchar');
while(readings~=47 )%if readed value is not "/ "
readings = fread(serial,1,'uchar') ;%read 1 more character
end
temp= fread(serial,9,'uchar'); %when it founds "/" then reads 9 data : 1234.2345
a=temp';
sizeVector(1)=a(1)*1000+a(2)*100+a(3)*10+a(4); %digit to real number
sizeVector(2)=a(6)*1000+a(7)*100+a(8)*10+a(9);
varargout = cell(1,nargout-1);
for k = 1:length(varargout)
varargout{k} = sizeVector(k);
end
end
here is the explanation : 1234.2345/ data comes like this. i first try to detect beginning of packet which is / and ascii for / is 47. so icheck if freads reads 47 value.
if it does then i can easly read my full packet because it is in order packet is 9 char long. first 4 char belong to data1 and the last 4 char belongs to data2 . so i convert these digits to real numbers. now they are integers.
this function's 1 loop is 0.002935 sec. something like 300 hz.
my baudrate is 115200 . My code works fine with low speed. I am able to read 1000*9 =10 kb in 42 secs. What i have to achieve is like 10 seconds. That would be enough for me .
  2 个评论
Jan
Jan 2017-10-20
编辑:Jan 2017-10-20
Your reading function is completely commented out? Please give us a chance to understand, what you are doing and remove the "%" characters from the code lines, which are active.
Is it really useful to provide the output twice, as vector and as list of scalars?

回答(1 个)

Ray
Ray 2017-10-20
Besides speed performance, there appears to be a problem with your conversion to decimal number. if temp is, for example the string '1234.2345', then a(2)*100 yields the number 5000 rather than what I am interpreting as the desired value, 200. That is because a(2) when used as a number converts the character '2' to its ASCII code, 50. You can solve the above problem by subtracting 48 from the converted character: (a(i)-48)*multiplier. Per time efficiency, I tried several options for speeding up the character conversion including: - vectorizing:
multipliers = [1000 100 10 1]; % outside loop
sizeVector(1) = sum( (temp(1:4)-48).*multipliers
and str2double:
sizeVector(1) = str2double(temp(1:4))
but, neither of these were faster than the explicit sum of the four terms (even with the -48 included). Another thought would be that you could read and process more than one sample from the stream in one gulp:
nbatch = 100;
temp = fread(serial, 10*nbatch, 'uchar');
temp = reshape(temp, 10,nbatch).';
temp = temp - 48;
sizeVector(1) = temp(1,:)*1000 + temp(2,:)*100+temp(3,:)*10+temp(4,:);
sizeVector(2) = temp(6,:)*1000 + temp(7,:)*100+temp(8,:)*10+temp(9,:);
would read 100 values at a time.
  1 个评论
enes kuzucu
enes kuzucu 2017-10-20
Thanks for answering ; /1234.6521 is decimal of what i am transfering. So there is no need to make it dec again
as for the multipliers codecan you explain a little bit more?You just are using vectors for efficiency right?with that calculations will be faster?
And the last one . I did tried to read more than 2 packets. (tried 10 packet which is equal to 500 bit . ) The are some issues like losing data resolition . I am still working on it .After some more try it might reduce time .

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by