function varargout = PulseInstrument(varargin)
% PULSEINSTRUMENT MATLAB code for PulseInstrument.fig
% PULSEINSTRUMENT, by itself, creates a new PULSEINSTRUMENT or raises the existing
% singleton*.
%
% H = PULSEINSTRUMENT returns the handle to a new PULSEINSTRUMENT or the handle to
% the existing singleton*.
%
% PULSEINSTRUMENT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PULSEINSTRUMENT.M with the given input arguments.
%
% PULSEINSTRUMENT('Property','Value',...) creates a new PULSEINSTRUMENT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before PulseInstrument_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to PulseInstrument_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 PulseInstrument
% Last Modified by GUIDE v2.5 18-Sep-2018 23:03:15
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @PulseInstrument_OpeningFcn, ...
'gui_OutputFcn', @PulseInstrument_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 PulseInstrument is made visible.
function PulseInstrument_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 PulseInstrument (see VARARGIN)
% Choose default command line output for PulseInstrument
handles.output = hObject;
% Search for serial available
serialAvailable = instrhwinfo('serial');
searchSerialTimer = timer;
searchSerialTimer.StartFcn = {@searchSerialTimerStartFcn,handles};
searchSerialTimer.TimerFcn = {@searchSerialTimerFcn,handles};
searchSerialTimer.Name = 'searchSerialTimer';
searchSerialTimer.Period = 1;
searchSerialTimer.UserData = serialAvailable;
searchSerialTimer.ExecutionMode = 'fixedSpacing';
handles.searchSerialTimer = searchSerialTimer;
start(searchSerialTimer);
% Set file
handles.fid = -1;
folderName = pwd;
folderName = strcat(folderName,'\Dat');
currentTimeStr = datestr(now,'yyyy-mm-dd-HH-MM-SS');
fileName = [currentTimeStr,'.dat'];
fileroad = [folderName,'\',fileName];
if ~exist(folderName,'dir')
mkdir(folderName);
end
try
/*
开发不易,整理也不易,如需要详细的说明文档和程序,以及完整的数据集,训练好的模型,或者进一步开发,
可加作者新联系方式咨询,WX:Q3101759565,QQ:3101759565
*/
% 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;
% --- Executes serial search timer start
function searchSerialTimerStartFcn(hObject, eventdata, handles)
availableSerialPorts = get(hObject,'UserData');
set(handles.InstrCom,'String',availableSerialPorts.AvailableSerialPorts);
% --- Executes serial search timer fcn
function searchSerialTimerFcn(hObject, eventdata, handles)
serialAvailable = instrhwinfo('serial');
hObject.UserData = serialAvailable;
set(handles.InstrCom,'String',serialAvailable.AvailableSerialPorts);
% --- Executes on selection change in InstrCom.
function InstrCom_Callback(hObject, eventdata, handles)
% hObject handle to InstrCom (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 InstrCom contents as cell array
% contents{get(hObject,'Value')} returns selected item from InstrCom
% --- Executes during object creation, after setting all properties.
function InstrCom_CreateFcn(hObject, eventdata, handles)
% hObject handle to InstrCom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
% --- Create a serial
function s = on_SerialCreate(handles)
instr = instrfind('Name','PulseInstrument');
if ~isempty(instr)
fclose(instr);
delete(instr);
end
stop(handles.searchSerialTimer);
comList = get(handles.InstrCom,'String');
comValue = get(handles.InstrCom,'Value');
com = comList{comValue};
s = serial(com);
s.Name = 'PulseInstrument';
s.BaudRate = 1382400;
s.DataBits = 8;
s.Parity = 'none';
s.StopBits = 1;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = {@SerialAvailableFcn,handles};
s.TimerFcn = {@SerialTimerFcn,handles};
s.TimerPeriod = 0.05;
s.InputBufferSize = 10000;
s.BytesAvailableFcnCount = 100;
isRead = 0;
bytesReceived = 0;
s.UserData = [isRead,bytesReceived];
% --- Delete a serial
function ret = on_SerialDelete(s)
if ~isa(s,'char')
return;
end
intrName = s;
instr = instrfind('Name',intrName);
if ~isempty(instr)
fclose(instr);
delete(instr);
end
function SerialAvailableFcn(hObject, eventdata, handles)
UserData = get(hObject,'UserData');
isRead = UserData(1);
bytesReceived = UserData(2);
if isRead
return;
end
availableNum = get(hObject,'BytesAvailable');
if availableNum > 0
isRead = 1;
bits = fread(hObject,availableNum,'uint8');
if handles.fid >0
%fprintf('received:%s\n',bits);
fwrite(handles.fid,bits,'uint8');
end
bytesReceived = bytesReceived + availableNum;
UserData = [isRead,bytesReceived];
set(hObject,'UserData',UserData);
end
function SerialTimerFcn(hObject, eventdata, handles)
UserData = get(hObject,'UserData');
isRead = UserData(1);
bytesReceived = UserData(2);
AvailableNum = get(hObject,'BytesAvailable');
isOpen = get(handles.PlotBt,'UserData');
if AvailableNum > 0
isRead = 1;
bits = fread(hObject,AvailableNum,'uint8');
if handles.fid > 0 && (isOpen == 1)
%fprintf('received:%s\n',bits);
fwrite(handles.fid,bits,'uint8');
end
bytesReceived = bytesReceived + AvailableNum;
UserData = [isRead,bytesReceived];
set(hObject,'UserData',UserData);
end
% --- Send by serial
function on_SerialSend(sName,s)
instr = instrfind('Name',sName);
if ~isempty(instr)
fwrite(instr,s,'uint8');
end
% --- Executes on button press in InstrConnect.
function InstrConnect_Callback(hObject, eventdata, handles)
% hObject handle to InstrConnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of InstrConnect
% Search for instr
% Get button value
btValue = get(hObject,'Value');
if btValue==1
%Create a serial
serial = on_SerialCreate(handles);
try
fopen(serial);
set(handles.InstrConnectStatus,'BackgroundColor',[0,1,0]);
set(hObject,'String','关闭串口');
catch
fprintf('Serial is not available\n');
set(handles.InstrConnectStatus,'BackgroundColor',[0.502 0.502 0.502]);
end
else