View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12763 Members
Join this group to post and comment.

continuously read serial port data using Matlab & plot it continuously

Hello all,

i am not very much expert in MATLB... i am designing a real time pic microcontroller based ECG system...for that after processing the real time ECG data through PIC16F877a microcontroller than i have to display the real time ECG to an interface in the computer screen

can anybody share me some idea about how to make it??

ryt now i am able to collect the ECG data and process it to the computer UART through pic16f877a microcontroller continuously..

my code is---

#include "built_in.h"

unsigned int adc_rd;



void main()
{
unsigned int temp_res;
Uart1_Init(9600);
ADCON0 = 0xC1;
ADCON1 = 0x8E; // Configure AN0 pin as analog
TRISA = 0xFF; // PORTA is input
TRISC = TRISC & 0xFC; // Pins RC7, RC6 are outputs
TRISB = 0x00; // PORTB is output
Delay_ms(100);
do
{
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
//UART1_write_text("mukti");
UART1_write(Lo(temp_res)); // Send lower 8 bits to UART
Delay_ms(100);
UART1_write(Hi(temp_res)); // Send upper 8 bits to UART
Delay_ms(100);
PORTB = Lo(temp_res); // Send lower 8 bits to PORTB
PORTC = Hi(temp_res); // Send 2 most significant bits to RC7, RC6

}while(1);
}


now i have to show it matlab...
Donagh
Donagh • Mar 2, 2012
Hi mukti, I’m working on something pretty similar at the moment, give me an example of your serial string and I’ll see if I can help you out.

What I am doing is taking ADC data from an 8052 an processing it with MATLAB, I haven’t figured out how to parse my sting yet but I can import it into MATLAB with this code:
*******************************************************************
% Function: STRINGGET
%
% Description: Spectialy made code for capturing a paticular string
% String Description: <-...,w,w,w,w,w,w,1,2,B444,B444,.......,B444,w,w,w,w,w,...->
% Trigger: '$'
% Serial Port Terminator: 'w'
%
% Inputs: ADuC842 UART serial string
% Outputs: serial string

s = serial('COM1');
set(s,'BaudRate',2400, 'Parity','none', 'Terminator', 'w', 'InputBufferSize', 4500);
fopen(s);

fprintf(s, '$');

A = fgetl(s);
strlength = length(A);

while strlength < 2000
A = fgetl(s);
strlength = length(A);
end

A

fclose(s);
delete(s)
clear s
***************************************************************************************

My sting is constant like your one. When it is in idle mode it prints w,w,w,w,w,w,w,w,w,w
And when I send a trigger ‘$’ the micro controller goes w,w,V00B5,R004E,T0432,t00,#00,……
It goes on like that for about 4000 CHAR and then goes back to w,w,w,w,w,w,w,w.
SO.. I set my terminator to w and set my buffer size to 4500 and used fgetl(s) to get the string

Pay close attention to my command “set(s,’…….’)” this is the most important one for setting your serial port parameters.
hii donagh ...

actually i have to send microcontrollers internal ADC read data to the matlab continuously with the serial port.. adc is 10 bit.. and we know UART will read only 8 bit at atime.. so we have to split the 10 bit to two 8 bit data...

here i am advised to send the data serially to the uart through the Microcontroller as the following sting so that MATLAB recognize the data.....

start xx end

where first x represents higher order ADC bits & second x represents lower order ADC bits.

now i also have to modify my Microcontroller code ..
Donagh
Donagh • Mar 8, 2012
Well every wrong answer brings you closer to the right one.

Are you using a data index to save/send your data?
Ie.

TOP_OF_INDEX equ 0x60 ; or whatever


MOV DPTR, #TOP_OF_INDEX
LOOP:
MOVC A, @A+DPTR ; take byte from relevant memory location
CALL SENDCHAR ; send byte ß (UARTIO function)
INC DPTR ; increment memory location
Jmp LOOP

This is a handy way of storing and sending lots of data without having to write too much code.
I think it’s called data indexing anyway.
Oh yeah, if you haven’t done so already, download “UARTIO” it is a load of premade functions that you can call for serial data transfer.
For learning how to use the serial port in MATLAB, just read tones of examples and look at all the different commands that you can use to do it, it should make itself pretty clear after… a very long time.
hii donagh ...

my string is look like as follows---

startxxxxend

actually when matlab sends a character 's' to the pic microcontroller's UART then matlab reads these string.. and matlab checks 'start' and when it meets it reads the xxxx data and plots it real time ...and when matlab reads 'end' ...matlab again sends character 's' and wait for the 2nd string for read...and so on....

when matlab sends other character than 's' it will stop reading string from UART.

i have to plot it in realtime...
Donagh
Donagh • Mar 20, 2012
That doesn't sound too bad, at least I think it can be done. Write a function that parses the string to an array and put your plotting commands at the end of the function.

I used a command like this recently:

while k(N) ~= 'W'
character = textscan(k(N), '%c', 1);
N = N + 1;

stChar = char(character);
iChar = sscanf(stChar, '%x');

‘W’ is the end of my string, so this bit of code looks at every individual character in the string when I’m parsing it. Textscan is a very versatile tool and can no doubt make your life easier.
I would tell you exactly how to parse your string but all I know about it is that it looks like startxxxxend

Linspace is a good tool for plotting, it allows you to generate a pot of something without knowing all of the details.

y = linspace(a,b)

Consult the MATLAB help for to see how to use these better.
To use linspace what I do is:
I take (my data) = x;
Which is an array -> [1 2 3 4 5 6 7 8 9 0];
And then I say z = count(x);
And then y = linspace(1, z);
This gives me two arrays of the same dimensions x and y
So I can just say plot(x, y);

One final thing you should think about!
Consider using SIMULINK, it’s a MATLAB thing, like a different sort of workspace, it might make things easier working in real time, might not, who knows.

Share this content on your social channels -