From: dpb on
moshe wrote:
> Hi,
> I am in a middle of my project, and I have some problem.
> I connect to the PC some kind of ADC(ez430-rf2500),this kit sample in
> 1KHz sample/sec. the problem is that I want to read the data to matlab
> with the code :
> s=serial('com3');
> fopen(s)
> while(1)
> t{p}=fscanf(s);
> p=p+1
> end
>
> the problem is that the command fscanf is very slow. this function read
> 100 sample/sec instead of 1000. I have overflow in the queue of the
> serial port.
> How can I do this fast????
> please help me.

Besides us' comment on baud rates, the reallocation on every loop you're
doing will cause it to slow down as t() grows.

Pre-allocate the t array to some size and fill instead...

--
From: moshe on

>
> Besides us' comment on baud rates, the reallocation on every loop you're
> doing will cause it to slow down as t() grows.
>
> Pre-allocate the t array to some size and fill instead...
>
> --
1) This kit support only 9600 baud rate
2) I tried to Pre-allocate the cell_array (p is only the index) and this does not work.

I pretty sure that matlab can work with rate greater then 100Hz with the SerialPort.
But the Question is How??
From: dpb on
moshe wrote:
>
>>
>> a hint:
>> - did you set the baud rate(?)...
>>
>> us
>
> the baud rate is 9600, as default.
> the kit support this baud rate.
> this is a slow rate?

Not terribly slow, no; but not extremely fast, either. What's the data
stream look like from the instrument and what's the communication protocol?

You need to consult the documentation for the device and determine how
it expects communication; generally there's some sort of handshaking
expected, perhaps your difficulties w/ being slow are that the device
and the comm port are renegotiating a connection for every sample or
somesuch that you can eliminate. But, there's no way to say precisely
w/o detail that you should be able to discover from the available
documentation.

--
From: Walter Roberson on
moshe wrote:

> I am in a middle of my project, and I have some problem.
> I connect to the PC some kind of ADC(ez430-rf2500),this kit sample in
> 1KHz sample/sec. the problem is that I want to read the data to matlab
> with the code :
> s=serial('com3');
> fopen(s)
> while(1)
> t{p}=fscanf(s);
> p=p+1
> end
>
> the problem is that the command fscanf is very slow. this function read
> 100 sample/sec instead of 1000. I have overflow in the queue of the
> serial port.
> How can I do this fast????

Later you indicate that you are using a baud rate of 9600. 9600 baud at
the standard 10 bits per character (1 start bit, 1 stop bit, 8 data
bits) would be 960 characters per second. Unless you can pack multiple
samples into one character, there will be no way that you are going to
be able to fit 1000 samples per second into a pipe that is only 960
items per second wide.

When you use fscanf(s) on a device, then each of the calls will read
characters until the terminator is read or the buffer fills up
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/serial.fscanf.html
If your p is counting "samples" the implication is that each sample
requires the transmission of a minimum of 2 characters (the "sample" as
a character and then a character of terminator), and the maximum number
of samples per second you would be able to read in such a case would be
960/2 = 480. Scale appropriately for larger text strings per sample.

If you intend to read one line per sample, then using fgetl(s) might be
more efficient in internal processing, but you are still going to be
greatly limited by the baud rate.


Guideline: take the number of printable characters per sample. Add 2 to
account for line terminators. Multiply the sum by 10. The result will be
the minimum baud rate that could possibly handle that data rate. Round
up to (75 times (a power of 2)) to get the necessary device baud rate
(75, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, etc.) If the
result is above 1 megabaud, you will need to find a different transfer
mechanism. If the result is above about 1/4 megabaud (estimated) then
you will need a fairly new version of Matlab to be able to handle serial
ports that fast.
From: Steven_Lord on


"moshe " <reubinoff(a)hotmail.com> wrote in message
news:i21k1k$8v2$1(a)fred.mathworks.com...
>
>>
>> Besides us' comment on baud rates, the reallocation on every loop you're
>> doing will cause it to slow down as t() grows.
>>
>> Pre-allocate the t array to some size and fill instead...
>>
>> --
> 1) This kit support only 9600 baud rate
> 2) I tried to Pre-allocate the cell_array (p is only the index) and this
> does not work.

Show the group how you tried to preallocate the cell array and what you mean
by "this did not work". Did you do something like this?

n = 1000;
C = cell(1, n);
for k = 1:n
C{k} = repmat('hello ', 1, n);
end

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: gate
Next: For loop iteration number?