Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Hng dn lp trnh giao tip RS232 trn PC/LAPTOP bng phn

mm Matlab.
I. Chun b cc cng c cn thit.
Thng thng cc my tnh PC hoc LAPTOP ngy nay khng c cng COM (RS232), chnh v
th khi thc hin vit chng trnh giao tip RS232 bng MATLAB, chng ta cn thc hin mt
s th thut sau:
Ngoi MATLB ci sn trn my tnh chng ta c th s dng 2 cng c h tr l Virtual Serial
Port Driver v Proteus.
Trc tin hy ci t 2 phn mm trn my tnh (hy c k hng dn s dng ca phn
mm).
Khi ng MATLAB v ch sn trn ca s lnh (Command Windows). Sau khi ng phn
mm Virtual Serial Port Driver kch hot to ra cng COM o (hnh H-01).

H-01
Ch rng Virtual Serial Port Driver to ra 1 cp cng COM v c kt ni dy tn hiu o
vi nhau. iu ny rt thun li khi chng ta s dng kt hp vi Proteus trong qu trnh m phng
v thc hin chng trnh.
By gi th t ca s lnh ca MATLAB truy xut kim tra cc cng COM va to ra xem ta
thu c kt qu nh th no.
Trn ca s lnh Command Windows ca MATLAB g lnh >> s=serial (COM1)

>> s = serial('COM1')
Serial Port Object : Serial-COM1
1

Communication Settings
Port:
COM1
BaudRate:
9600
Terminator:
'LF'
Communication State
Status:
closed
RecordStatus:
off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent:
0
Nh vy i tng l Serial-COM1, tc 9600, v mt s tham s khc c lit k. Tip theo,
chng ta xem cc tham s ca i tng nh th no bng lnh >> get(s):
>> get(s)
ByteOrder = littleEndian
BytesAvailable = 0
BytesAvailableFcn =
BytesAvailableFcnCount = 48
BytesAvailableFcnMode = terminator
BytesToOutput = 0
ErrorFcn =
InputBufferSize = 512
Name = Serial-COM1
ObjectVisibility = on
OutputBufferSize = 512
OutputEmptyFcn =
RecordDetail = compact
RecordMode = overwrite
RecordName = record.txt
RecordStatus = off
Status = closed
Tag =
Timeout = 10
TimerFcn =
TimerPeriod = 1
TransferStatus = idle
Type = serial
UserData = []
ValuesReceived = 0
ValuesSent = 0

SERIAL specific properties:


BaudRate = 9600
BreakInterruptFcn =
DataBits = 8
DataTerminalReady = on
FlowControl = none
Parity = none
PinStatus = [1x1 struct]
PinStatusFcn =
Port = COM1
ReadAsyncMode = continuous
RequestToSend = on
StopBits = 1
Terminator = LF
C rt nhiu tham s phi khng no? chng ta y quan tm n tham
s: BytesAvailableFcn tham s ny cha tht lp. Tham s ny chnh l hm callback m n s
gi khi c byte nhn c b m nhn.Vy chng ta vit hm ny chnh l vit
hm OnComm p ng s kin ReceiveEvent nh trong MSCOMM ca MS vy. Thit lp ny
phi thc hin trc khi m cng giao tip, nn chng ta s vit hm callback trc. Hy vit
mt m-file vi tnSerial_Callback.m nh sau:
function Serial_Callback(obj,event)
ind = fscanf(obj)
C php ca hm callback nh trn vi obj l i tng kiu Serial nh trn. Hm c tc dng c
d liu v hin th lun kt qu ln command window. Chng ta a tham s tn hm vo cho i
tng s ca ta:
>> s.BytesAvailableFcn = @Serial_Callback;
C php ca hm callback nh trang trc vi obj l i tng kiu Serial nh trn. Hm c tc
dng c d liu v hin th lun kt qu ln command window.
Tip theo a tham s tn hm vo i tng s:
>>s.BytesAvailableFcn = @Serial_Callback;

II. Thit k giao din RS232 Communication trn MATLAB GUI


Giao din c thit k c bn nh hnh minh ha di y:

Cc i tng v thuc tnh thit lp ty thuc vo ngi lp trnh, thng thng s c cc quy
c pht sinh. Nhng trn ht chng ta nn lm theo mt chun quy c nht nh sau ny g
ri chng trnh mt cch thun tin.

Ton b code chng trnh c th xem ti y:


1. function RS232_Communication_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for RS232_Communication
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes RS232_Communication wait for user response (see UIRESUME)
5

% uiwait(handles.figure1);
% manual
set(handles.edit_TX,'String','');
set(handles.edit_RX,'String','');
set(handles.edit_StatusConnect,'String','Disconnect');
set(handles.edit_StatusSend,'String','');
set(handles.edit_TX,'HorizontalAlignment','Left');
set(handles.edit_RX,'HorizontalAlignment','Left');
set(handles.button_Send,'String','Send');
set(handles.button_Exit,'String','Exit');
set(handles.button_ClearTX,'String','Clear TX');
set(handles.button_ClearRX,'String','Clear RX');
set(handles.button_Connect,'String','Connect');
set(handles.button_Send,'Enable','off');
IsConnect = 0; % 1 = Connect,0 = Disconnect
%Declare global variable
global hand;
hand = handles;
save data.
handles.status_com = IsConnect;
guidata(hObject,handles);
2. function button_Send_Callback(hObject, eventdata, handles)
IsConnect = handles.status_com;
s = handles.com;
if (IsConnect == 1)
set(handles.edit_StatusSend,'String','Sending... ');
strTX = get(handles.edit_TX,'String');
fprintf(s,strTX);
set(handles.edit_StatusSend,'String','Sent successfully ');
end
handles.com = s;
guidata(hObject,handles);
3. function button_Connect_Callback(hObject, eventdata, handles)
IsConnect = handles.status_com;
6

if (IsConnect == 0),

% if comport is disconnected so set up and open it.

IsConnect = 1;
% Setting for Serial Object
s = serial(get_stringPopup(handles.popup_ComPort));
s.BaudRate = get_doublePopup(handles.popup_BaudRate);
s.DataBits = get_doublePopup(handles.popup_DataBit);
s.Parity = get_stringPopup(handles.popup_ParityBit);
s.StopBit = get_doublePopup(handles.popup_StopBit);

% COM port
% Baud rate
% Data bit
% Parity bit
% Stop bit

% Setting for reat direction


s.ReadAsyncMode = 'continuous';
s.BytesAvailableFcnCount = 1;
s.Terminator = 'CR'
s.BytesAvailableFcnMode = 'terminator';%'terminator';
% Declare some callback functions or interrupt functions.
s.BytesAvailableFcn = @BytesAvailable_Callback;
s.OutputEmptyFcn = @OutputEmpty_Callback;
s.BreakInterruptFcn = @BreakInterrupt_Callback;
s.ErrorFcn
= @Error_Callback;
s.PinStatusFcn
= @PinStatus_Callback;
s.BreakInterruptFcn = @BreakInterrupt_Callback;
s.TimerFcn
= @Timer_Callback;
%s.RecordName = get(handles.edit_RecordName,'String');
%s.RecordMode = 'index';
% Open port to operate
fopen(s);
%record(s,'on')
% Changing the text displayed on the button named Connect or Disconnect
% depend on the status of comport.
temp = s.status;
if(temp == 'open'),
set(handles.button_Connect,'String','Disconnect');
set(handles.edit_StatusConnect,'String','Connected');
else
set(handles.edit_StatusConnect,'String','A Problem occour..');
7

end;
% Hide all setting popup menu.
set(handles.popup_ComPort,'Enable','off');
set(handles.popup_BaudRate,'Enable','off');
set(handles.popup_DataBit,'Enable','off');
set(handles.popup_StopBit,'Enable','off');
set(handles.popup_ParityBit,'Enable','off');
set(handles.edit_RecordName,'Enable','off');
set(handles.button_Send,'Enable','on');
else
% If Com Port is opened so close it
s = handles.com;
set(handles.button_Connect,'String','Connect');
set(handles.edit_StatusConnect,'String','Disconnected');
%record(s,'off')
% close serial port and delete serial object.
fclose(s);
IsConnect = 0;
delete(s);
% Show all setting popup menu.
set(handles.popup_ComPort,'Enable','on');
set(handles.popup_BaudRate,'Enable','on');
set(handles.popup_DataBit,'Enable','on');
set(handles.popup_StopBit,'Enable','on');
set(handles.popup_ParityBit,'Enable','on');
set(handles.edit_RecordName,'Enable','on');
set(handles.button_Send,'Enable','off');
end

% end of if.

% Save all data to all fields of handles.


handles.com = s;
handles.status_com = IsConnect;
guidata(hObject,handles);

You might also like