WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

GUI mit DAQ

 

kaktus
Forum-Newbie

Forum-Newbie


Beiträge: 3
Anmeldedatum: 28.08.19
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 18.12.2020, 13:16     Titel: GUI mit DAQ
  Antworten mit Zitat      
Hallo goMatlab-Mitglieder,

ich habe die Matlab-Code für die Steuerung von Anlage geschrieben. Das Programm funktioniert. Die Aufgabe ist bei bestimmte Signale in Volt, die durch DAQ-System die Signale im Amper in SCPI-Befehl für Instrument sendet.

Das Code:
Code:

clear
global obj1  s


% Create a serial port object.   Laborgerät


obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
%s1=Serial('<COM1','baudrate',9600,'stopbits',1,'Terminator','
%Create the serial port object if it does not exist
%otherwise use the object that was found.
if isempty(obj1)
    obj1 = serial('COM3');
else
    fclose(obj1);
    obj1 = obj1(1);
end

% Connect to instrument object, obj1.
fopen(obj1);

fprintf(obj1,'*IDN?');
 idn = fscanf(obj1)
obj1.ReadAsyncMode = 'continuous';
obj1.Timeout=45;


 fprintf(obj1,'syst:lock on');


s = daq.createSession('ni');
addAnalogInputChannel(s,'myDAQ1','ai0','Voltage');
s.DurationInSeconds = 200;




h1=addlistener(s,'DataAvailable',@plotData);

 startBackground(s);

 % Versuche
 

function plotData(src,event)
global obj1 s

      value=event.Data;
       
    % if (value >= 0.1) & (value <1.0)
         if value <=1.0
             
             
         stop(s);
           
        fprintf(obj1,'curr 0');
        stop(obj1);
       
       
        fprintf(obj1,'syst:lock off');
       
       
       
    end  
   if (value > 1.0) & (value <3.0)
       
        fprintf(obj1,'curr 1.5');
    end  
   
    if (value >= 3) & (value <7)
        fprintf(obj1,'curr 2');
    end
    if (value >= 2) & (value < 7)
        fprintf(obj1,'curr 2.5');
    end
    if (value >= 7) & (value < 8)
        fprintf(obj1,'curr 1.8');
    end
   
    if value >= 8
        fprintf(obj1,'curr 1.5');
       
    end

    plot(event.TimeStamps,event.Data);
   
   
end

 


Nächste Schritt ist GUI-Obelrfläche zu schreiben. Mein Code
Code:

function varargout = Zelle(varargin)
% ZELLE MATLAB code for Zelle.fig
%      ZELLE, by itself, creates a new ZELLE or raises the existing
%      singleton*.
%
%      H = ZELLE returns the handle to a new ZELLE or the handle to
%      the existing singleton*.
%
%      ZELLE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ZELLE.M with the given input arguments.
%
%      ZELLE('Property','Value',...) creates a new ZELLE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Zelle_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Zelle_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 Zelle

% Last Modified by GUIDE v2.5 27-Oct-2020 20:11:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Zelle_OpeningFcn, ...
                   'gui_OutputFcn',  @Zelle_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 Zelle is made visible.
function Zelle_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 Zelle (see VARARGIN)

% Choose default command line output for Zelle
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Zelle wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Zelle_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;


% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject    handle to Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 global obj1  strom spannung

obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');

if isempty(obj1)
    obj1 = serial('COM3');
else
    fclose(obj1);
    obj1 = obj1(1);
end

% Connect to instrument object, obj1.
fopen(obj1);
if ~isempty(obj1)
 set( handles.Labor,'ForegroundColor','green');  
 else
    set( handles.Labor,'ForegroundColor','red');
end

% fprintf(obj1,'*IDN?');
%  idn = fscanf(obj1)
obj1.ReadAsyncMode = 'continuous';
obj1.Timeout=45;

s = daq.createSession('ni');
addAnalogInputChannel(s,'myDAQ1','ai0','Voltage');
% s=daq('ni');
% ch=addinput(s,'myDAQ1','ai0','Voltage');
if isvalid(s)
    set( handles.Sensor,'ForegroundColor','green');
else
    set( handles.Sensor,'ForegroundColor','red');
end
%s.DurationInSeconds = 120;

s.DurationInSeconds = str2double(get(handles.edit1,'String'));

fprintf(obj1,'syst:lock on');
data=str2double(get(handles.Tabelle,'Data'));
  %  data=cell2mat(get(handles.Tabelle,'Data'));
    for i=1:length(data(:,1))
        if ~isnan(data(i,1))      
       spannung(i)=data(i,1);
        strom(i)=data(i,2);
        end
    end

 h1=addlistener(s,'DataAvailable',@plotData);
% s.IsContinuous = true;
 s.startBackground();

%s.startForeground();
 %start(s);
 %startBackground(s);
 %handles.axes1=eventdata;
 
 %plot(handles.axes1,event.TimeStamps,event.Data);



function plotData(s,event)
global obj1 strom spannung
   
    n=length(spannung);
      value=event.Data;
     
   
     for i=1:n-1
        if (value < spannung (i)) && (value >spannung (i+1))
            wert=strom(i);
         fprintf(obj1,strcat('curr ',string(wert)));
        end
     
     end
end
     


   % plot(handles.axes1,event.TimeStamps,event.Data);
   
   



% --- Executes when entered data in editable cell(s) in Tabelle.
function Tabelle_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to Tabelle (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
%   Indices: row and column indices of the cell(s) edited
%   PreviousData: previous data for the cell(s) edited
%   EditData: string(s) entered by the user
%   NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%   Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
hObject.ColumnEditable = [true true];
%set(hObject,'data',[0]);


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to 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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to 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

 


Hier kommt folgende Feler:
Zitat:

Hardware is reserved (possibly by an external application). Call release to unreserve the
hardware or exit the external application, and retry the operation.



Das Fehler kommt beim Befehl
s.Background();

Wo könnte das Problem sein?
Mit Grüß
kaktus
Private Nachricht senden Benutzer-Profile anzeigen E-Mail senden


Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.