Baud Rate on Matlab
9 views (last 30 days)
Show older comments
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
Walter Roberson
on 30 Mar 2016
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
Answers (0)
See Also
Categories
Find more on FPGA, ASIC, and SoC Development 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!