Create Your Adaptor from the Demo Adaptor

Use the demo adaptor as a template for creating a custom adaptor which you can build, test, and access from the toolbox. The following sections provide a sequence of steps for adaptor modification. The examples in this topic create a custom adaptor named MyAdaptor with a vendor ID of my.

Edit and Build Your Adaptor

This section describes the step to make a new custom adaptor based on the shipped demo adaptor. This example modifies only the names of the adaptor, vendor, and devices, without any functional changes. You build the custom adaptor in a local folder, then add the build folders to the MATLAB path. This section uses two folder locations throughout:

LocationDescription
matlabrootMATLAB installation location. This is the MATLAB used both for the building of the adaptor, and for accessing the adaptor through a data acquisition session.
C:\adaptors\daqsdkLocal file location where the new adaptor is modified and built.
  1. Create the build area in a location of your choice. This example works with a new folder, C:\adaptors\daqsdk. Create a subfolder here called src, and within that a subfolder named daqadaptor.

  2. Copy the folder DemoAdaptor from matlabroot\toolbox\daq\daqsdk\src\daqadaptor into C:\adaptors\daqsdk\src\daqadaptor.

  3. Inside C:\adaptors\daqsdk\src\daqadaptor, rename the folder DemoAdapter to be MyAdaptor.

    Navigate into MyAdaptor, and rename three of its files according to the following table:

    Original NameNew Name
    custom_demo.cppcustom_my.cpp
    demoadaptor.cppmyadaptor.cpp
    demoadaptor.hppmyadaptor.hpp
  4. With a text editor, modify each of the three new files in the previous table, replacing all occurrences of text DemoAdaptor, demoadaptor, DemoDriver, and custom_demo.cpp with MyAdaptor, myadaptor, MyDriver, and custom_my.cpp, respectively, keeping the letter capitalization style with each replacement.

  5. Further edit the contents of myadaptor.cpp as shown in the following table:

    Original TextUpdated Text
    {
        shortName = "MW";
        fullName = "MathWorks";
        driverName = "DemoAdaptor";
    
        return DAQSuccess;
    }
    {
        shortName = "MY";
        fullName = "MyAdaptor";
        driverName = "MyDriver";
    
        return DAQSuccess;
    }
    prefix = "MWDev";
    prefix = "MyDev";
    DAQStatus MyDriver::inputSingleScanImpl(ChannelGroupIndex groupIndex, DataScan &data) const
    {
        deviceManager_->inputSingleScan(groupIndex, data);
        return DAQSuccess;
    }
    
    DAQStatus MyDriver::inputSingleScanImpl(ChannelGroupIndex groupIndex, DataScan &data) const
    {
    //  deviceManager_->inputSingleScan(groupIndex, data);
        data.push_back(1.125);
        data.push_back(2.250);
        return DAQSuccess;
    }
    

    The last row of this table causes the inputSingleScanImpl function to return hard data, rather than calling the driver function to read data.

    With these modifications saved and in place, you are ready to build the adaptor.

  6. In MATLAB, run the following utility to build the executable MEX-file for MyAdaptor:

    daq.sdk.utility.mex.buildAdaptor('MyAdaptor','custom_my', ...
      'C:\adaptors\daqsdk\src\daqadaptor\MyAdaptor','C:\adaptors\daqsdk\bin\win64')

    The function input arguments specify the adaptor name, source code file, source file location, and where to put the built output.

    Note

    The buildAdaptor function requires that your system be configured with Microsoft® Visual Studio® 2013 or later.

  7. Create the folder C:\adaptors\daqsdk\+daq, and copy into it the folder +demoadaptor found at matlabroot\toolbox\daq\daqsdk\+daq\+demoadaptor.

  8. Navigate into C:\adaptors\daqsdk\+daq, and rename +demoadaptor to +myadaptor.

  9. Navigate into C:\adaptors\daqsdk\+daq\+myadaptor, and edit these two MATLAB files in that folder:

    Session.m
    VendorInfo.m
    • In both of these files, replace all occurrences of the texts DemoAdaptor and demoadaptor with MyAdaptor and myadaptor, respectively, keeping the letter capitalization style with each replacement.

    • In the VendorInfo file, use % characters to comment out the lines that hide the adaptor, between the begin and end remove indicators. The change looks like this:

      % BEGIN REMOVE
      %   if daq.internal.getOptions().HideDAQSDKAdaptor
      %      throw(MException(message('daqsdk:HardwareInfo:VendorIsHidden', mfilename('class'))));
      %   end
      % END REMOVE
    • Save and close the files.

    Your modified adaptor is now ready for use.

Use Your Adaptor in a Session

This example shows how to access the vendor and device represented by your modified adaptor. A data acquisition session with your adaptor allows you to add channels and get information from the device.

Start MATLAB, and use the following commands to make your adaptor available.

addpath 'C:\adaptors\daqsdk\bin\win64'
addpath 'C:\adaptors\daqsdk'

Then you can access your adaptor.

v = daq.getVendors
v = 

Number of vendors: 2

index     ID      Operational         Comment         
----- ----------- ----------- ------------------------
1     ni          false       Click here for more info
2     my          true        MyAdaptor

Use the index of the vendor ID my to get more information.

vendor = v(2)
vendor = 

Data acquisition vendor 'MyAdaptor':

            ID: 'my'
      FullName: 'MyAdaptor'
AdaptorVersion: '3.13 (R2018a)'
DriverVersion: '1.0.0'
IsOperational: true

Create a session for your device.

s = daq.createSession('my')
s = 

Data acquisition session using MyAdaptor hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   No channels have been added.

Add an analog input channel to the session, associated with the device MyDev0, channel ai0.

ch1 = addAnalogInputChannel(s,'MyDev0','ai0','Voltage')
ch1 = 

Data acquisition analog output voltage channel 'ao0' on device 'MyDev0':

TerminalConfig: SingleEnded
          Range: -10 to +10 Volts
           Name: ''
             ID: 'ao0'
         Device: [1x1 daq.sdk.DeviceInfo]
MeasurementType: 'Voltage'

Add a second analog input channel.

ch2 = addAnalogInputChannel(s,'MyDev0','ai1','Voltage');

View the session to see the channel configurations.

s
s = 

Data acquisition session using MyAdaptor hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   Number of channels: 2
      index Type Device Channel MeasurementType      Range       Name
      ----- ---- ------ ------- --------------- ---------------- ----
      1     ai   MyDev0 ai0     Voltage (Diff)  -10 to +10 Volts
      2     ai   MyDev0 ai1     Voltage (Diff)  -10 to +10 Volts
   
Examine the objects so far in the base workspace.
whos
   Name        Size            Bytes  Class                                Attributes

  ch1         1x1                 8  daq.sdk.AnalogInputVoltageChannel              
  ch2         1x1                 8  daq.sdk.AnalogInputVoltageChannel              
  s           1x1                 8  daq.myadaptor.Session                          
  v           1x2                16  daq.VendorInfo                                 
  vendor      1x1                 8  daq.myadaptor.VendorInfo      

With the example data hard coded into the adaptor inputSingleScanImpl function, you can execute a single scan measurement on the session channels.

data = inputSingleScan(s)
data =

    1.1250    2.2500

You can also read streaming input data, in this case provided by the demo adaptor DAQstream object. The session default configuration captures 1000 scans in 1 second.

stdata = startForeground(s);
whos stdata
  Name           Size            Bytes  Class     Attributes

  stdata      1000x2             16000  double

stdata contains a column of 1000 samples for each channel. View the first six rows.

stdata(1:6,:)
         0    0.2500
    0.2487    0.4987
    0.4818    0.7318
    0.6845    0.9345
    0.8443    1.0943
    0.9511    1.2011

When you are finished, delete the session and clear the objects.

delete(s)
clear v vendor s ch1 ch2

See Also

Functions

Related Examples

More About