Let the future tell the truth and evaluate each one according to his work and accomplishments. The present is theirs; the future, for which I really worked, is mine. (Nikola Tesla)

18/06/2011

● Warning: A timeout occurred before the Terminator was reached

In this warning, terminator is the one that is required to be sent by the device to MATLAB. If you have this warning, serial port object that you open in MATLAB timed out before the required terminator was received.

When might this warning occur?
-When you can't get the required terminator that you defined in MATLAB from the device.
-When you can't get the correct terminator before timeout.

For instance, if you set the parameters as below;
set(SerPIC, 'terminator', 'LF');
set(SerPIC, 'timeout', .1);
you just want to wait for the data and so the linefeed terminator for 0.1 sec. So, your device should send the data with the terminator to MATLAB before 0.1 sec. If it doesn't, you will get this warning.

-When you don't have any data sent by the device.

What should you do?
-You may use "HHD Serial Port Monitor" to check if you have any data and so the terminator received or not. Or you can just use hyperterminal to see what you receive as data.
-Be sure that they're connected to proper COM ports.
-Set the communication parameters like baudrate, parity bit, terminator etc. as the hardware requires.
-Be sure that you use the correct terminator defined in the manual documentation of the device.
-Set the timeout to a proper value.



17/06/2011

Serial Communication in MATLAB - 1

In these series of article about serial communication in MATLAB, I explain how we can use MATLAB to interact with hardware which are connected to a PC through a serial port. Notebooks mostly don't have serial port, but USB ports. So, you can use a USB to Serial Converter as well.

We firstly learn some basic concepts like terminator, buffer, parameters, etc.


-Terminator-

We may roughly say that a terminator is used to specify the end of a message. For instance, the format in asynchronous serial communication for a 10-bit character frame is just like a start bit, followed by 8 data bits and one stop bit in the end. The stop bit here defines a stop period. Again, we may roughly say that the same logic is reasonable for terminator.

So, what are we going to do to configure terminator? We can use any integer value ranging between 0-127 for defining an ASCII character or we can directly configure terminator to the ASCII character. We may configure its value as "CR" or "13" which is carriage return. We may also configure its value as "LF" or "10" which is line feed. We may also configure terminator to CR/LF which is a carriage return followed by a line feed or vice versa. We can also set it as 1-by-2 cell array, first cell is the read terminator and the second one is the write terminator.

While we are writing string data to the device connected to the serial port object, we use 'fprintf' command. 'fprintf' also adds the terminator character at the end of the string that you send. It is new line (\n) by default or it is specified by the terminator property which you set while you are defining serial port object at first. So, you have a chance to change it as how it's defined in the documentation of external device. In your works, you can use "HHD Serial Port Monitor" to see what MATLAB sends as terminator.

While we are getting string data from external device, we use 'fscanf' command. As in the 'fprintf' command, 'fscanf' command also uses terminator. Any reading operation is completed when a terminator, which is specified by terminator property, is read. For instance if your device use "LF" (\n) as terminator, you can set your terminator property of serial port object as "LF" in MATLAB and so the reading process is completed when MATLAB gets terminator value from device.



12/06/2011

● Finding Maximum and Minimum by Golden-Section Search using MATLAB

This program implements the golden-section search algorithm. It locates the minimum or maximum of the function depending on the user’s preference, iterates until the relative error falls below a stooping criteria or exceeds an maximum number of iterations, return both the optimal x and f(x), minimizes the number of function evaluations.

r=(sqrt(5)-1)/2; %Golden Rate
disp('Number of max iteration')
maxiter=input('maxiter= ');
disp('Value of tolerance')
tol=input('Tol= ');
disp('For max enter "1", for min enter "0"')
x=input('minmax= ');
a=input('Lower bound= '); %Lower limit
b=input('Upper bound= '); %Upper limit
disp(' ')
disp('k x1 x2')

for k=1:maxiter
if (x==1) %Part that will find max
x1=a+r*(b-a); %First one of the points inside
x2=b-r*(b-a); %Second one of the points inside
fx1=-x1^4-2*x1^3-8*x1^2-5*x1; %Value of fx at first inside point
fx2=-x2^4-2*x2^3-8*x2^2-5*x2; %Value of fx at second inside point
if fx1>fx2 %Changing interval as [x2,b]
a=x2;
x2=x1;
x1=a+r*(b-a);
else
b=x1; %Changing interval as [a,x1]
x1=x2;
x2=b-r*(b-a);
end
fprintf ('%d %7.5f %7.5f\n', k,x1,x2)
%Stop if the error is below tolerance
if(abs((x1-x2))
end
else %Part that will find min
x1=a+r*(b-a);
x2=b-r*(b-a);
fx1=3+6*x1+5*x1^2+3*x1^3+4*x1^4;
fx2=3+6*x2+5*x2^2+3*x2^3+4*x2^4;
if fx1
a=x2; %Changing interval as [x2,b]
x2=x1;
x1=a+r*(b-a);
else
b=x1; %Changing interval as [a,x1]
x1=x2;
x2=b-r*(b-a);
end
fprintf ('%d %7.5f %7.5f\n', k,x1,x2)
if(abs((x1-x2))
end
end
end
disp(' ')
disp('Results')
disp('x1 x2 f(x1) f(x2)')
fprintf ('%7.5f %7.5f %7.5f %7.5f\n',x1,x2,fx1,fx2)



● Calculating cos(x) Using Taylor Series Expansion on MATLAB

This program evaluates cosine function using "Taylor Series Expansion".
x=input('x= '); %Number that we will calculate its cosine
N=input('N= '); %Number of iteraritons
cosx=1; %Initial value cos(0)
truecos=cos(x) %True value of cos(x) calculated by Matlab command

for k=2:N
fact=1; %Initial value to calculate factorial
for i=1:(2*(k-1)) %for loop to calculate required factorial
fact=fact*i;
end
cosx=cosx+(-1)^(k+1)*x^(2*(k-1))/fact %calculation of cos(x)
error=(truecos-cosx)/truecos*100 %calculation of relative error
end


ShareThis