主题:数据通过串口传到PC,在GUI中怎么动态显示数据波形呢?
数据通过串口传到PC中,MATLAB接收后在GUI中实时显示数据波形,
数据接受已经调试好了,可是不知道怎么显示。
使用axes 显示还是 frame 控件显示呢?
用axes时,可以在按钮回调函数中显示到GUI的axes中,
但在串口接收(中断方式)中显示,会出现弹出新绘图窗口。
我把代码发上来,高手帮我看看。
function comport_callback(obj, event)
%warning( 'COM recevice 16 bytes' );
global seriala_buf;
send_len = get( obj, 'BytesAvailable' );
serial_buf = fread( obj, 16,'uint8')
%在这里加axes绘制serial_buf数据时弹出新绘图窗口。
% --- Executes on selection change in popup_com_port.
function popup_com_port_Callback(hObject, eventdata, handles)
global handle_com;
% hObject handle to popup_com_port (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popup_com_port contents as cell array
% contents{get(hObject,'Value')} returns selected item from popup_com_port
sel_val = get(hObject,'Value');
com_str = get(hObject,'String'); %get COM ports list
sel_str = com_str{ sel_val }; %get old COM port name
handle_com = serial( sel_str );
baud_rate = get( handles.popup_baud_rate,'Value' );
data_bit = get( handles.popup_data_bit,'Value' );
stop_bit = get( handles.popup_stop_bit,'Value' );
parity = get( handles.popup_parity,'Value' );
%warning( 'COM port set-----------------------------' );
try
switch baud_rate
case 1
handle_com.BaudRate=115200;
case 2
handle_com.BaudRate=57600;
case 3
handle_com.BaudRate=38400;
case 4
handle_com.BaudRate=19200;
otherwise
handle_com.BaudRate=9600;
end
% warning( 'COM baud rate-----------------------------' );
switch data_bit
case 1
handle_com.DataBits=8;
case 2
handle_com.DataBits=7;
case 3
handle_com.DataBits=6;
case 4
handle_com.DataBits=5;
otherwise
handle_com.DataBits=8;
end
switch stop_bit
case 1
handle_com.StopBits=1;
case 2
handle_com.StopBits=1.5;
case 3
handle_com.StopBits=2;
otherwise
handle_com.StopBits=1;
end
switch parity
case 1
handle_com.Parity='none';
case 2
handle_com.Parity='odd';
case 3
handle_com.Parity='even';
otherwise
handle_com.Parity='none';
end
handle_com.InputBufferSize=1024;
handle_com.timeout=0.5;
handle_com.Terminator='LF';
handle_com.FlowControl='none';
handle_com.BytesAvailableFcnMode = 'byte';
handle_com.BytesAvailableFcnCount = 16;
handle_com.BytesAvailableFcn = @comport_callback;
% handle_com.BytesAvailableFcn = @instrcallback;
% handle_com.OutputEmptyFcn = @instrcallback;
get( handle_com )
fopen( handle_com );
% warning( 'open COM ok' );
set( hObject, 'Enable', 'off' );
set( handles.popup_baud_rate,'Enable','off' );
set( handles.popup_data_bit,'Enable','off' );
set( handles.popup_stop_bit,'Enable','off' );
set( handles.popup_parity,'Enable','off' );
% fwrite(handle_com,'COM init OK');
% out_data=fread(handle_com,16,'uint8')%接收32个数据(8位),并存入out数组中
% warning( out_data );
% fclose( handle_com );
% delete( handle_com );
% clear( handle_com );
catch
fclose( handle_com );
info_str = sprintf( '%s open failure, please select another!\n', sel_str);
errordlg( info_str,'COM PORT ERROR','modal');
end
% --- ascii string to hex numbers
function hexnum = asciistr_to_hex_nums( ascStr )
% ascStr is need transf string
len = length( ascStr );
hexnum= [];
for k = 1:len;
hexasc = ascStr(k);
if hexasc>='0'&& hexasc<='9'
hextmp = hexasc-'0';
elseif hexasc>='A' && hexasc<='F'
hextmp = hexasc-'A'+10;
elseif hexasc>='a' && hexasc<='f'
hextmp = hexasc-'a'+10;
else
hextmp = 0;
end
if bitand(k,1)==1;
hexval = hextmp;
else
hexnum(k/2) = hexval*16+hextmp;
end
end
% --- Executes on button press in pbutton_send_data.
function pbutton_send_data_Callback(hObject, eventdata, handles)
% hObject handle to pbutton_send_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global handle_com
txtstr = get( handles.edit_send_box, 'String' );
sendfmt = get( handles.radio_asc_fmt, 'Value' );
if sendfmt==1.0
fwrite( handle_com,txtstr );
else
hexnum = asciistr_to_hex_nums( txtstr );
fwrite( handle_com, hexnum );
%len = length( hexnum );
%for k = 1:len;
% fwrite( handle_com, hexnum(k) );
%end
%在这里加axes绘制serial_buf数据时在对应的GUI绘图窗口。
end
…………………………
数据接受已经调试好了,可是不知道怎么显示。
使用axes 显示还是 frame 控件显示呢?
用axes时,可以在按钮回调函数中显示到GUI的axes中,
但在串口接收(中断方式)中显示,会出现弹出新绘图窗口。
我把代码发上来,高手帮我看看。
function comport_callback(obj, event)
%warning( 'COM recevice 16 bytes' );
global seriala_buf;
send_len = get( obj, 'BytesAvailable' );
serial_buf = fread( obj, 16,'uint8')
%在这里加axes绘制serial_buf数据时弹出新绘图窗口。
% --- Executes on selection change in popup_com_port.
function popup_com_port_Callback(hObject, eventdata, handles)
global handle_com;
% hObject handle to popup_com_port (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popup_com_port contents as cell array
% contents{get(hObject,'Value')} returns selected item from popup_com_port
sel_val = get(hObject,'Value');
com_str = get(hObject,'String'); %get COM ports list
sel_str = com_str{ sel_val }; %get old COM port name
handle_com = serial( sel_str );
baud_rate = get( handles.popup_baud_rate,'Value' );
data_bit = get( handles.popup_data_bit,'Value' );
stop_bit = get( handles.popup_stop_bit,'Value' );
parity = get( handles.popup_parity,'Value' );
%warning( 'COM port set-----------------------------' );
try
switch baud_rate
case 1
handle_com.BaudRate=115200;
case 2
handle_com.BaudRate=57600;
case 3
handle_com.BaudRate=38400;
case 4
handle_com.BaudRate=19200;
otherwise
handle_com.BaudRate=9600;
end
% warning( 'COM baud rate-----------------------------' );
switch data_bit
case 1
handle_com.DataBits=8;
case 2
handle_com.DataBits=7;
case 3
handle_com.DataBits=6;
case 4
handle_com.DataBits=5;
otherwise
handle_com.DataBits=8;
end
switch stop_bit
case 1
handle_com.StopBits=1;
case 2
handle_com.StopBits=1.5;
case 3
handle_com.StopBits=2;
otherwise
handle_com.StopBits=1;
end
switch parity
case 1
handle_com.Parity='none';
case 2
handle_com.Parity='odd';
case 3
handle_com.Parity='even';
otherwise
handle_com.Parity='none';
end
handle_com.InputBufferSize=1024;
handle_com.timeout=0.5;
handle_com.Terminator='LF';
handle_com.FlowControl='none';
handle_com.BytesAvailableFcnMode = 'byte';
handle_com.BytesAvailableFcnCount = 16;
handle_com.BytesAvailableFcn = @comport_callback;
% handle_com.BytesAvailableFcn = @instrcallback;
% handle_com.OutputEmptyFcn = @instrcallback;
get( handle_com )
fopen( handle_com );
% warning( 'open COM ok' );
set( hObject, 'Enable', 'off' );
set( handles.popup_baud_rate,'Enable','off' );
set( handles.popup_data_bit,'Enable','off' );
set( handles.popup_stop_bit,'Enable','off' );
set( handles.popup_parity,'Enable','off' );
% fwrite(handle_com,'COM init OK');
% out_data=fread(handle_com,16,'uint8')%接收32个数据(8位),并存入out数组中
% warning( out_data );
% fclose( handle_com );
% delete( handle_com );
% clear( handle_com );
catch
fclose( handle_com );
info_str = sprintf( '%s open failure, please select another!\n', sel_str);
errordlg( info_str,'COM PORT ERROR','modal');
end
% --- ascii string to hex numbers
function hexnum = asciistr_to_hex_nums( ascStr )
% ascStr is need transf string
len = length( ascStr );
hexnum= [];
for k = 1:len;
hexasc = ascStr(k);
if hexasc>='0'&& hexasc<='9'
hextmp = hexasc-'0';
elseif hexasc>='A' && hexasc<='F'
hextmp = hexasc-'A'+10;
elseif hexasc>='a' && hexasc<='f'
hextmp = hexasc-'a'+10;
else
hextmp = 0;
end
if bitand(k,1)==1;
hexval = hextmp;
else
hexnum(k/2) = hexval*16+hextmp;
end
end
% --- Executes on button press in pbutton_send_data.
function pbutton_send_data_Callback(hObject, eventdata, handles)
% hObject handle to pbutton_send_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global handle_com
txtstr = get( handles.edit_send_box, 'String' );
sendfmt = get( handles.radio_asc_fmt, 'Value' );
if sendfmt==1.0
fwrite( handle_com,txtstr );
else
hexnum = asciistr_to_hex_nums( txtstr );
fwrite( handle_com, hexnum );
%len = length( hexnum );
%for k = 1:len;
% fwrite( handle_com, hexnum(k) );
%end
%在这里加axes绘制serial_buf数据时在对应的GUI绘图窗口。
end
…………………………