Baud Rate on Matlab

Hi,
I am using the Matlab code below to plot the analog inputs from a microcontroller. Why is the code displaying a "Failed!" message when I change the baud rate from 9600 to 115200? The serial communication is with a Cortex M3 microcontroller.
% Reference: https://developer.mbed.org/cookbook/Interfacing-with-Matlab (SEE 'Serial Communication' SECTION)
delete(instrfindall); % if any port is already opened by MATLAB its gonna find and close it
TIMEOUT = 5; % time to wait for data before aborting
XPOINTS = 500; % number of points along x axis
try % this is the try/catch to Handle Errors
%create serial object to represent connection to mbed
mbed = serial('COM5', ...
'BaudRate', 115200, ...
'Parity', 'none', ...
'DataBits', 8, ...
'StopBits', 1); % change depending on mbed configuration
set(mbed,'Timeout',TIMEOUT); % adjust timeout to ensure fast response when mbed disconnected
fopen(mbed); % open serial connection
position = 1; %initialise graph variables
time = 1;
x = [(1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)'];
xlabels = (1:XPOINTS);
y = zeros(XPOINTS,5);
while (1)
values = fscanf(mbed, '%f,%f,%f,%f,%f');
y(position,:) = values'; %put into y to be displayed
%update position on x-axis and x-axis labels
xlabels(position) = time;
time = time + 1;
if (position < XPOINTS)
position = position + 1;
else
position = 1;
end
%display
plot(x,y);
set(gca, 'XTick', 1:XPOINTS);
set(gca, 'XTickLabel', xlabels);
drawnow; % this is required to force the display to update before the function terminates
end
fclose(mbed); % close connection (this should never be reached when using while(1), but included for completeness)
delete (mbed) % deleting serial port object
clear mbed
clear all
catch
%in case of error or mbed being disconnected
disp('Failed!');
fclose(mbed); % close connection to prevent COM port being lokced open
delete (mbed) % deleting serial port object
clear mbed
clear all
end
Below is the MBED code:
#include "mbed.h"
// Initialize a pins to perform analogue input fucntions
AnalogIn ain1(p15);
AnalogIn ain2(p16);
AnalogIn ain3(p17);
AnalogIn ain4(p19);
AnalogIn ain5(p20);
// USB serial (TX, RX)
Serial pc(USBTX, USBRX);
int main(void)
{
pc.baud(115200);
// Declaring variables to be used for the ADC voltages
float ADC1_voltage;
float ADC2_voltage;
float ADC3_voltage;
float ADC4_voltage;
float ADC5_voltage;
while (1)
{
// Multiply the digitized signal by 3.3 (which is the ADC reference) to get the real voltage value
ADC1_voltage = ain1*3.3;
ADC2_voltage = ain2*3.3;
ADC3_voltage = ain3*3.3;
ADC4_voltage = ain4*3.3;
ADC5_voltage = ain5*3.3;
// Send the five ADC values to serial port
pc.printf("%f,%f,%f,%f,%f\n", ADC1_voltage, ADC2_voltage, ADC3_voltage, ADC4_voltage, ADC5_voltage);
}
}

7 Comments

That code could display the Failed message due to errors occurring at any point. You need to use more debugging statements to find out the point at which the error was triggered. For example does it occur as soon as the fopen() is done? Does it occur after some data has been transferred?
Thanks for the reply. Is it possible to do line by line debugging on Matlab?
dbstep
Thanks. The following error message being displayed: "Subscripted assignment dimension mismatch." Error in Untitled (line 38) y(position,:) = values';
When I types "values" in the command window, I noticed that it only contains three values when it should have five. I tested the communication using Putty and no missing data was noticed. What can be causing this problem please? If there is no solution for this, is there a way to check the length of the variable "values" before the line y(position,:) = values'; is executed? I was thinking of introducing an if statement where the program will skip that particular line is the variable "values" does not contain five values. This way I will prevent the program from stopping execution.
You need hardware flow control for anything beyond 9600.
Use a temporary variable and check its length before doing the assignment.
Hi again, I think the LPC1768 does not provide hardware flow control. Is it possible to achieve the same thing using software? Do you know of any tutorial for this because this is my first time doing this? Thanks in advance.
You could check for software flow control, but it is notorious for overruns when you increase the baud rate. With faster baud rates, by the time the transmitter receives the "Slow down my buffer is nearly full!" message, it has already sent enough characters to overfill the buffer.
The software work-arounds for this involve adding layers of error detection and correction, such as Forward Error Correction

Sign in to comment.

Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 28 Mar 2016

Commented:

on 30 Mar 2016

Community Treasure Hunt

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

Start Hunting!