/* *Compile the mex file: mex LabJackReadAI.c -L "LabJackud.lib" */ #include #include #include "mex.h" #include "C:\Program Files (x86)\LabJack\Drivers\LabJackUD.h" //taken from U3simple Examlpe code //This is our simple error handling function that is called after every UD //function call. This function displays the errorcode and string description //of the error. It also has a line number input that can be used with the //macro __LINE__ to display the line number in source code that called the //error handler. It also has an iteration input is useful when processing //results in a loop (getfirst/getnext). void ErrorHandler (LJ_ERROR lngErrorcode, long lngLineNumber, long lngIteration) { char err[255]; if (lngErrorcode != LJE_NOERROR) { ErrorToString(lngErrorcode,err); printf("Error number = %d\n",lngErrorcode); printf("Error string = %s\n",err); printf("Source line number = %d\n",lngLineNumber); printf("Iteration = %d\n\n",lngIteration); if(lngErrorcode > LJE_MIN_GROUP_ERROR) //Quit if this is a group error. return; } } void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { //taken from U3simple Examle code LJ_ERROR lngErrorcode; long lngGetNextIteration; double dblDriverVersion; long lngIOType=0, lngChannel=0; double dblValue=0; double Value2=9999,Value3=9999,Value4=9999; double ValueDIBit=9999,ValueDIPort=9999,ValueCounter=9999; LJ_HANDLE lngHandle=0; char ch; //My variables long DACID; double Voltage; double *pout; //Get MATLAB Input Arguments (RHS) DACID=(long)mxGetScalar(prhs[0]); //Read and display the UD version. dblDriverVersion = GetDriverVersion(); printf("UD Driver Version = %.3f\n\n",dblDriverVersion); //Open the first found LabJack U3. lngErrorcode = OpenLabJack (LJ_dtU3, LJ_ctUSB, "1", 1, &lngHandle); ErrorHandler(lngErrorcode, __LINE__, 0); //Start by using the pin_configuration_reset IOType so that all //pin assignments are in the factory default condition. lngErrorcode = ePut (lngHandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0); ErrorHandler(lngErrorcode, __LINE__, 0); //Configure FIO2-FIO4 as analog, all else as digital. That means we //will start from channel 0 and update all 16 flexible bits. We will //pass a value of b0000000000011100 or d28. lngErrorcode = ePut (lngHandle, LJ_ioPUT_ANALOG_ENABLE_PORT, 0, 28, 16); ErrorHandler(lngErrorcode, __LINE__, 0); lngErrorcode =eAIN (lngHandle, DACID , (long) 31, &Voltage,0,1,1,0,0,0); ErrorHandler(lngErrorcode, __LINE__, 0); printf("Voltage in channel %d = %f\n",DACID,Voltage); //configure matlab return variable /* Create matrix for the return argument. */ plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL); pout = mxGetPr(plhs[0]); *pout=Voltage; }