Access serial port for data sent from Nios II's uart for real time plotting graph in Matlab

5 次查看(过去 30 天)
Hi all,
I am able to read data from FPGA to Nios processor, then transmit this data to Uart component created in Qsys using direct register method (IORD_ALTERA_AVALON_UART_STATUS (UART_BASE); IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE, txdata);etc).
Then, I was trying to access serial port data sent from uart in Altera Nios II to plot graph real time in Matlab. I was using this code from Matlab central (<http://uk.mathworks.com/matlabcentral/fileexchange/28941-real-time-data-plot-from-serial-port>) with minor modification as pasted below:
When I run this code I keep getting the following error. What should I do ?
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> real_time_data_serial at 80 voltage(count) = fscanf(s,'%f');
I found a few solutions on the web, what I have tried: 1. add pause in .m file so that it has enough time to read the data before the next cycle of data
2. add enough delay at C code where I transmit data to uart
3. make sure the uart has the same setting as comport such as comport number, baud rate, parity, flow control
%%real time data plot from a serial port % This matlab script is for ploting a graph by accessing serial port data in % real time. Change the com values and all variable values accroding to % your requirements. Dont forget to add terminator in to your serial device program. % This script can be modified to be used on any platform by changing the % serialPort variable. % Author: Moidu thavot.
%%Clear all variables
clear all; %%Variables (Edit yourself)
SerialPort='com8'; %serial port MaxDeviation = 3;%Maximum Allowable Change from one value to next TimeInterval=0.2;%time interval between each input.0.2 loop=120;%count values %%Set up the serial port object s=serial('com8'); set(s,'BaudRate',9600,'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none','Terminator','LF'); set(s, 'terminator', 'LF'); set(s, 'timeout', 10);
%%s = serial(SerialPort) fopen(s);
time =now; voltage = 0; %% Set up the figure figureHandle = figure('NumberTitle','off',... 'Name','Voltage Characteristics',... 'Color',[0 0 0],'Visible','off');
% Set axes axesHandle = axes('Parent',figureHandle,... 'YGrid','on',... 'YColor',[0.9725 0.9725 0.9725],... 'XGrid','on',... 'XColor',[0.9725 0.9725 0.9725],... 'Color',[0 0 0]);
hold on;
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'Color',[0 1 0]);
xlim(axesHandle,[min(time) max(time+0.001)]);
% Create xlabel xlabel('Time','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create ylabel ylabel('Voltage in V','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create title title('Real Time Data','FontSize',15,'Color',[1 1 0]);
%% Initializing variables
voltage(1)=0; time(1)=0; count = 2; k=1; while ~isequal(count,loop)
%%Re creating Serial port before timeout
k=k+1;
if k==25
fclose(s);
delete(s);
clear s;
s = serial('com8');
fopen(s)
k=0;
end
%%Serial data accessing
pause(2);
fprintf('here');
voltage(count) = fscanf(s,'%f');
%%For reducing Error Use your own costant
voltage(1)=0;
if (voltage(count)-voltage(count-1)>MaxDeviation)
voltage(count)=voltage(count-1);
end
time(count) = count;
set(plotHandle,'YData',voltage,'XData',time);
set(figureHandle,'Visible','on');
datetick('x','mm/DD HH:MM');
pause(5);
count = count +1;
end
%% Clean up the serial port fclose(s); delete(s); clear s;
When I check the serial port status, I got the following:
Communication Settings Port: COM8 BaudRate: 9600 Terminator: 'LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
I noticed the serial port is ready (since it is open) but no data is sent or receive. Could you please tell me what is wrong so I could try?
Please , thank you

回答(1 个)

want2know
want2know 2015-3-9
anyone please?

Community Treasure Hunt

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

Start Hunting!

Translated by