Reading ADC data from MSP430 to Matlab via serial port
Show older comments
My situation is
1. external ADC samples from one channel and transmit to Serial port Via Uart and then switches over to second channel to do same and then third.
2. I manage to view data in Hterm and it looked quite fine.
3. I want to plot it live in matlab- I know there are ways to do it but can someone here explain me a basic step by step to achieve my objective?
I have basic idea of what i should be doing
- setup serial communication.
- create array to store uart values
- calculate voltage values
- plot live values
It would be nice if someone points out me for where to look for point 2 and 4 specifically.
19 Comments
Walter Roberson
on 15 Jun 2022
Is there a maximum number of samples to be saved? A maximum number at one time? For example, plot the most recent 1024 samples?
Hiril Patel
on 16 Jun 2022
Walter Roberson
on 16 Jun 2022
Edited: Walter Roberson
on 17 Jul 2024
If you do not have a maximum size for the array, then you need to grow the array dynamically, which can kill the performance if you do not plan for it. See for example John's https://www.mathworks.com/matlabcentral/fileexchange/8334-incremental-growth-of-an-array-revisited?s_tid=blogs_rc_4
Walter Roberson
on 16 Jun 2022
plotting as data comes in also needs some thought as you do not want to keep creating new graphics objects. I recommend using animatedline(), possibly with the maximum points limit turned on.
Hiril Patel
on 16 Jun 2022
Walter Roberson
on 16 Jun 2022
Read from each channel. Then
counter = counter + 1;
all_data(:, counter) = samples;
I used columns instead of rows because that is more efficient.
Hiril Patel
on 17 Jun 2022
Walter Roberson
on 17 Jun 2022
Could you show an example of what is being sent? And is it binary or character?
Hiril Patel
on 17 Jun 2022
Walter Roberson
on 18 Jun 2022
Edited: Walter Roberson
on 17 Jul 2024
You have binary, and it looks like you have a different number of bytes at times. You just might be sending a space after each / but that is not clear. If that is / space then it is generally followed by 8 bytes. But sometimes it is only 7 and sometimes it is 9.
You should switch to one of the following schemes:
- Write numbers in character form, either decimal or hexadecimal, with non-number between the entries. Either put newline after each individual number or put newline at the end of a group of samples; or
- Write numbers in binary using a fixed number of bytes per entry, so that you can ask to read a fixed number of bytes with no delimiter; or
- At the beginning of each burst of numbers, write a byte (or two) which is the number of bytes that follow in that packet, with no end of line. The input side reads the size (fixed size) and uses the size to read that many bytes; or
- use a variable length binary stream with a fixed termination byte, with the binary stream having been carefully adjusted so that it never accidentally includes the termination character. For example you might use newline, and everywhere that newline would have been sent as data send 255 0 and everything 255 would have been sent, send 255 255.
Hiril Patel
on 19 Jun 2022
Hiril Patel
on 21 Jun 2022
Walter Roberson
on 23 Jun 2022
Look at https://www.mathworks.com/matlabcentral/answers/uploaded_files/1036145/Capture.JPG in your display of the input data. That's the one that starts
ÿ ù/
Those are binary not character.
You have two choices:
- you can continue to send in binary, and do binary reads using fread() (for serial) or read() (for serialport). If you are not always sending the same number of bytes or if not everything is the same binary data type, then you may need to add some extra bytes to indicate how the following data is to be treated. If you use this fread() binary approach, you do not put any delimiters between values and you do not send newline; OR
- you can switch to sending the numbers as text. If you send as text, put a space between the values and put newline at the end of the line. On the MATLAB end, fscanf() will read the entire line of values, returning a numeric vector.
You only need things like your /A/ or similar if you are sending the channels in inconsistent order. For example if you had a series of devices that generated interrupts when it was ready, and you wanted to send the data in the order received, then there might be reason to tag each value according to the channel it is associated with.
Hiril Patel
on 28 Jun 2022
Hiril Patel
on 30 Jun 2022
Walter Roberson
on 30 Jun 2022
ch0 = readmatrix('ch0.csv');
Those ch0, ch1, ch2 are probably not scalars.
Channel0 = n*ch0;
so the Channel* variables are probably not scalars.
gauge0 = (4/2.04)*(Channel0/E);
and that makes the gauge* variables not scalars.
eq0 = (gauge0 - (e_x*((cos(0)).^2)) +(e_y*((sin(0)).^2)) + (lamda_xy*(sin(0))*(cos(0))));
So the eq* variables are individually probably not scalars, since they are in terms of the gauge variables that are (probably) not scalars.
solve(eq0,eq1,eq2,e_x,e_y,lamda_xy);
You are asking to solve those arrays of equations that are in terms of 3 scalar variables e_x ,e_y, lamda_xy and you are asking for a single e_x and e_y and lamda_xy that solves those arrays simultaneously.
For example if ch0 is 10 x 1 then eq0 would be 10 x 1, and your call to solve would be solve(10x1, 10x1, 10x1, scalar, scalar, scalar).
solve() is the simultaneous equation solver. When you pass it more than one equation, it has to find the values of the free variables that make all of the equations true at the same time.
What you can do is arrayfun():
sols = arrayfun(@(E0,E1,E2)) solve(E0,E1,E2,e_x,e_y,lamda_xy), eq0, eq1, eq2, 'uniform', 0);
I used uniform, 0 because there might be no solutions in some cases, or there might be multiple solutions perhaps.
Hiril Patel
on 3 Jul 2022
Hiril Patel
on 18 Sep 2022
Hiril Patel
on 18 Sep 2022
Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!