Vous êtes sur la page 1sur 9

1/26/2011

MATLAB GUI Communication


Day 1 of 2

PC to micro Communication
• Playing with PC side

1
1/26/2011

Serial Port on PC
• If you have a serial port use ‘COM1’

• If you do NOT have a serial port


– Need a USB to Serial converter
– Needs a driver, Google search for:
• prolific usb serial driver download

– Use Device manager for find COM#

Finding the COM port


• Search for “device manager”
in the Start area
• Device Manager
– Expand Ports
– Find USB to Serial port

USB-to-Serial

Requires hardware to see it pop up. Good to know how to find it though.

2
1/26/2011

UART over USB


• Moving off the PICDEM board
• Also needs a driver

• Google search for:


ftdi virtual com port driver download

MATLAB Serial Objects


pic = serial(‘COM1’, ‘BaudRate’, 19200,'Terminator',10)

• Communication Settings
– Port: COM1
– BaudRate: 19200
– Terminator: 10
• Communication State
– Status: closed

3
1/26/2011

Writing data
pic = serial(‘COM1’, ‘BaudRate’,…
19200,'Terminator',10)

fopen(pic)

fprintf(pic, ‘Hello World!’);

fclose(pic)

Reading data
pic = serial(‘COM1’, ‘BaudRate’,…
19200,'Terminator',10)

fopen(pic)

fscanf(pic);

fclose(pic)

4
1/26/2011

Fancy serial object open function


Download “openSerial.m”. Event driven receive!

>> pic = openSerial(3)


>> fprintf(pic, ‘to PICDEM')

(then if the PIC sends data this will automatically print)


On 10/8/2009 at 10:32pm (59 secs) received 12 char(s) =
From PICDEM

>>

Event driven receive


serialObj = serial(portStr, 'BaudRate', 19200,...
'TimeOut',0.1,...
'Terminator',10,...
'BytesAvailableFcnMode','terminator',...
'BytesAvailableFcn',@serial_PICDEM_BytesAvailableFcn);

function serial_PICDEM_BytesAvailableFcn(serialObj, eventData)


%serial_PICDEM_BytesAvailableFcn bytes from serial object
% This is the callback function after a 'terminator' is received

% Code for callback function – Display function and processing

end

5
1/26/2011

MATLAB GUIDE
Graphical User Interface Development Environment

Panels
• Group objects
• Tag
– uipanel_nameForPanel
– Handy to disable/enable whole panel
• Name
– Title on Panel

6
1/26/2011

Pushbuttons
• Tag
– pushbutton_nameForButton
– pushbutton_goLeft

• String
– Label on button

• BackgroundColor, FontSize, Position


• Callback function

Handles structure using tags


handles.uipanel_ADC handles.pushbutton_sendMessage

handles.uipanel_buzzer handles.edit_comPort

handles.uipanel_allMessages handles.pushbutton_connect

handles.uipanel_connect handles.pushbutton_disconnect

handles.text_RA0value handles.text1, handles.text2, etc.

handles.pushbutton_getRA0 handles.figure1

handles.pushbutton_stopBuzzer

handles.pushbutton_startBuzzer handles.user.connected

handles.text_receivedMessages handles.user.pic

handles.edit_sendMessage

7
1/26/2011

Callbacks
• Functions that get called when an event
happens
– Much like an interrupt with a PIC
– Event driving programming!
– hObject is the object that was selected

• We’ll use callbacks for


– Pushbuttons
– Radio Buttons

Edit Text Objects


• Tag
– edit_nameForEditBox
– edit_comPort
• String
– Getting string currently in the edit box
• get(handles.tagName,'String')
– Getting value as a number
• str2double(get(handles.tagName,'String'))
• Callback
– Often callback function is not used
– Within callback can use hObject instead of handles struct

8
1/26/2011

handles struct
• A struct variable with a reference to every
object in your GUI!

• Set functions
– set(handles.text_RA0value,'String',500);

• Get functions
– get(handles.edit_sendMessage,'String');

Static Text
• Tag
– text_nameForStaticText
– text_RA0value
– Sometimes renaming tag is not necessary
• String
– Setting string in the static text
• set(handles.tagName,'String',newStringValue);
– Example
• set(handles.text_RA0value,'String',500);

Vous aimerez peut-être aussi