Featured image of post Serial Communication and MATLAB

Serial Communication and MATLAB

For producing the Quad Rotor’s GUI I needed to be able to implement the serial communication in MATLAB. This it seemed would be a simple task as on windows machines it is a simple task of opening the comms port, send the data, and closing the port. All seemed so simple, but as it turns out this was not the case on the Mac which I was using from my project with MATLAB R2008a

s = serial('/dev/tty.usbserial-FT4KIQ90');
fopen(s);
fprintf(s, 'a');
fclose(s);

I found that the serial interface on this version of MATLAB was not fully implemented, as a result every time that you tried to close the serial port you would need to restart the computer, which is far from ideal. Luckily for me my university had just made available the latest version of MATLAB (R2011a) which supported the serial communication commands that I needed.

So how does the serial communication work? Well it all starts when the user request the software to connect to the Quad Rotor, this checks to see if the serial port is open or not, then if it is not it opens up the serial link. This series of events first starts when the GUI is opened up, at this point the serial object is created, with the parameters set at this stage it allows the program to access it at any point though a common interface. For the purpose of testing I am using a USB serial connection in place of the bluetooth link, to prevent problems that could be caused in the case of a communication failure. At this point the BaudRate is set for the link, which is 115200, the same as used in pervious tests, apart from this I user the standard settings for the port.

handles.s = serial('/dev/tty.usbserial-11BP0140');
handles.s.BaudRate = 115200;

Opening the connection, with the object no created it is possible to connect to the Quad Rotor. This is done when the user selects the Connect Button, which is names cmdConnect, and calls the function bellow.

% --- Executes on button press in cmdConnect.
function cmdConnect_Callback(hObject, eventdata, handles)
% hObject handle to cmdConnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

%check if the serial port is avalible to connect to
if isa(handles.s,'serial') && isvalid(handles.s)
if strcmpi(get(handles.s,'Status'),'open')
addstr = cellstr('Already Connected to Quadrotor');
else
fopen(handles.s);
addstr = cellstr('Connected to Quadrotor');
pause(2);
end
commsStats = 'Connected to Quadrotor';
%set the enabled buttons
set(handles.cmdDisconnect, 'Enable', 'on');
set(handles.cmdConnect, 'Enable , 'off');

else
addstr = cellstr('Failed to connect to quadrotor');
commsStats = 'Disconnected from Quadrotor';
end

%add the log update to the gui
oldstr = cellstr(get(handles.lbDataveiw, 'String'));
set(handles.lbDataveiw, 'str', { addstr{:} ,oldstr{:} });
%set(handles.tbComms, 'str', commsStats);
set( handles.tbComms, 'String', commsStats );
end

This function carries out a few simple error checks to ensure that the program only calls the function that opens the COM port at the right time, otherwise MATLAB and GUI could crash. It first checks that the serial port object has been created, and if it is valid. This allows the confirmation of if the serial port exists, as the Quad Rotor may not have power, be disconnected from the setup, or the port is in use by another program, in this case it reports to the user about the failure, by adding a new cell to the text reporting system in the GUI.

Now that it confirmed that the port exists it checks if the port is actually open or not, using the following code, which comes back with a true or false statement which is used as part of an If statement.

strcmpi(get(handles.s,'Status'),'open')

If the port is already open it again reports this to the user, but if all the details are correct then it is able to open the connection to the Aurdino Microcontroller on board the quad rotor.

The next stage in the system is sending data to the Aurdino Microcontroller, and receiving it back. This is relatively straight forward, to send the data, For example to request a communication link test:

fprintf(handles.s, 't');

And then to receive the data that is send back we use:

testresult = fscanf(handles.s);

The next stage on the process, is now to close the serial port. This is important to free it us for use with other programs, and ensure we are able to connect to it again when the program is next started up. The port can simply closed using the following command, which closes the connection:

fclose(handles.s);

In my case this is called when we click on the Disconnect button in the GUI. Next thing to do on the project now is automating the process of sending an receiving this data.

Built with Hugo
Theme Stack designed by Jimmy