hi this is my code to recieve data serial and show certain data

6 次查看(过去 30 天)
hello all,
i am trying to write an code in gui in which i can display the data sent by 8051 and receive through serial port is displayed in edit box separately, can anybody help me in correcting it as i am not able to retrieve data
function varargout = test1(varargin)
% TEST1 M-file for test1.fig
% TEST1, by itself, creates a new TEST1 or raises the existing
% singleton*.
%
% H = TEST1 returns the handle to a new TEST1 or the handle to
% the existing singleton*.
%
% TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST1.M with the given input arguments.
%
% TEST1('Property','Value',...) creates a new TEST1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before test1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to test1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help test1
% Last Modified by GUIDE v2.5 20-Feb-2012 22:38:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test1_OpeningFcn, ...
'gui_OutputFcn', @test1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before test1 is made visible.
function test1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to test1 (see VARARGIN)
serialPorts = instrhwinfo('serial');
nPorts = length(serialPorts.SerialPorts);
set(handles.coms, 'String', ...
[{'Select a port'} ; serialPorts.SerialPorts ]);
set(handles.coms, 'Value', 2);
% set(handles.rx_text, 'String', cell(1));
% Choose default command line output for test1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function Tx_data_Callback(hObject, eventdata, handles)
% hObject handle to Tx_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Tx_data as text
% str2double(get(hObject,'String')) returns contents of Tx_data as a double
% --- Executes during object creation, after setting all properties.
function Tx_data_CreateFcn(hObject, eventdata, handles)
% hObject handle to Tx_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function baudrate_Callback(hObject, eventdata, handles)
% hObject handle to baudrate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of baudrate as text
% str2double(get(hObject,'String')) returns contents of baudrate as a double
% --- Executes during object creation, after setting all properties.
function baudrate_CreateFcn(hObject, eventdata, handles)
% hObject handle to baudrate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in coms.
function coms_Callback(hObject, eventdata, handles)
% hObject handle to coms (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns coms contents as cell array
% contents{get(hObject,'Value')} returns selected item from coms
% --- Executes during object creation, after setting all properties.
function coms_CreateFcn(hObject, eventdata, handles)
% hObject handle to coms (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in connect.
function connect_Callback(hObject, eventdata, handles)
% hObject handle to connect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(get(hObject,'String'),'connect') % currently disconnected
serPortn = get(handles.coms, 'Value');
if serPortn == 1
errordlg('Select valid COM port');
else
serList = get(handles.coms,'String');
serPort = serList{serPortn};
serConn = serial(serPort, 'TimeOut', 1, ...
'BaudRate', str2num(get(handles.baudrate, 'String')));
try
fopen(serConn);
handles.serConn = serConn;
% enable Tx text field and Rx button
set(handles.Tx_data, 'Enable', 'On');
% set(handles.rxButton, 'Enable', 'On');
set(hObject, 'String','Disconnect')
catch e
errordlg(e.message);
end
end
else
set(handles.Tx_data, 'Enable', 'On');
% set(handles.rxButton, 'Enable', 'Off');
set(hObject, 'String','Connect')
fclose(handles.serConn);
end
guidata(hObject, handles);
function rx_text_Callback(hObject, eventdata, handles)
% hObject handle to rx_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of rx_text as text
% str2double(get(hObject,'String')) returns contents of rx_text as a double
try
RxText = fscanf(handles.serConn);
% currList = get(handles.rx_text, 'String');
% if length(RxText) < 1
% RxText = 'Timeout @ ';
% set(handles.rx_text, 'String', ...
% [currList ; [RxText datestr(now)] ]);
% else
% set(handles.rx_text, 'String', ...
% [currList ; ['Received @ ' datestr(now) ': ' RxText ] ]);
% end
set(handles.rx_text,'string', cellstr(RxText ));
catch e
disp(e)
end
% --- Executes during object creation, after setting all properties.
function rx_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to rx_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in send.
function send_Callback(hObject, eventdata, handles)
% hObject handle to send (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Tx_data = get(handles.Tx_data, 'String');
fprintf(handles.serConn, Tx_data);
currList = get(handles.rx_text, 'String');
set(handles.rx_text, 'String', ...
[currList ; Tx_data] );
% set(handles.rx_text, 'Value', length(currList) + 1 );
set(hObject, 'String', 'send');
% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject handle to clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Tx_data,'string','')
% --- Executes on button press in clear_rx.
function clear_rx_Callback(hObject, eventdata, handles)
% hObject handle to clear_rx (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.rx_text,'string','')
function selected_edit1_Callback(hObject, eventdata, handles)
% hObject handle to selected_edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of selected_edit1 as text
% str2double(get(hObject,'String')) returns contents of selected_edit1 as a double
set(handles.selected_edit1,'string', cellstr(RxText(2:6) ));
% --- Executes during object creation, after setting all properties.
function selected_edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to selected_edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function selected_edit2_Callback(hObject, eventdata, handles)
% hObject handle to selected_edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of selected_edit2 as text
% str2double(get(hObject,'String')) returns contents of selected_edit2 as a double
set(handles.selected_edit2,'string', cellstr(RxText(2:9) ));
% --- Executes during object creation, after setting all properties.
function selected_edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to selected_edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

回答(1 个)

Walter Roberson
Walter Roberson 2012-2-23
What style of uicontrol is rx_text ? You are setting its String as if it is a text uicontrol, but you are relying on it having a Callback which text uicontrol would not have ?

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by