You are on page 1of 15

DACAPL.

DLL, a CAPL DLL to use IOcab with CANalyzer

Author: Version: Status: Customer: Printing date:

Bodo Hesselmaier 1.1 dated 2005-10-11 completed


(in progress / completed / verified / released)

2005-10-17

Number of pages:

15

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer --Revision history Version 1.0.0 dated 2004-06-28 1.0.1 dated 2004-08-04

II

Editor Hb Hb

Description Created Some minor changes

Table of contents 1 General.........................................................................................................................4 2 Installation ...................................................................................................................4 3 Usage in CAPL ............................................................................................................4 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 Variables of the CAPL program.............................................................................................5 CAPL on preStart ...................................................................................................................6 CAPL on Start ........................................................................................................................7 Setting the Analog Outputs ....................................................................................................8 Setting the Digital Outputs .....................................................................................................8 Reading the Inputs..................................................................................................................9 Using PWM Out and Capture In ..........................................................................................11 Helper Functions ..................................................................................................................11

4 DLL Functions ...........................................................................................................12 4.1 unsigned long CAPLEXPORT far CAPLPASCAL daLoadLibrary(char libraryName[])...12 4.2 int CAPLEXPORT far CAPLPASCAL daGetDeviceCount(unsigned long libHandle)......12 4.3 int CAPLEXPORT far CAPLPASCAL daGetDeviceInfo(unsigned long libHandle, int dev, char deviceName[], int n).....................................................................................................12 4.4 int CAPLEXPORT far CAPLPASCAL daOpenDevice(unsigned long libHandle, int dev)12 4.5 int CAPLEXPORT far CAPLPASCAL daCloseDevice(unsigned long libHandle, int dev)13 4.6 int CAPLEXPORT far CAPLPASCAL daPrepStartDevice(unsigned long libHandle, int dev).......................................................................................................................................13 4.7 int CAPLEXPORT far CAPLPASCAL daStartDevice(unsigned long libHandle, int dev, int reset) .....................................................................................................................................13 4.8 int CAPLEXPORT far CAPLPASCAL daStopDevice(unsigned long libHandle, int dev) .13 4.9 long CAPLEXPORT far CAPLPASCAL daGetAIValueEx2(unsigned long libHandle, long device, int channel, double value[],unsigned long time[]) ...................................................13 4.10 int CAPLEXPORT far CAPLPASCAL daGetDIValueEx2(unsigned long libHandle, long device, int channel,char value[], unsigned long time[]) .......................................................13 4.11 int CAPLEXPORT far CAPLPASCAL daSetAOValue(unsigned long libHandle, long device, int channel, double value) ........................................................................................14 4.12 int CAPLEXPORT far CAPLPASCAL daSetDOValue(unsigned long libHandle, long device, int channel, long value)............................................................................................14 4.13 int CAPLEXPORT far CAPLPASCAL daSetModeDigital(long device, int channel, unsigned long mode) ............................................................................................................14 4.14 int CAPLEXPORT far CAPLPASCAL daSetModeAnalog(long device, int channel,

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

III

unsigned long mode, unsigned long voltRange) ..................................................................14 4.15 int CAPLEXPORT far CAPLPASCAL daSetModeTrigger(long device, unsigned long mode, unsigned long period, unsigned long source, unsigned long level) ...........................14 4.16 int CAPLEXPORT far CAPLPASCAL daSetModePWM(long device, unsigned long mode, unsigned long freq)....................................................................................................15

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

General

The CAPL-DLL dacapl.dll will enable using an IOcab in CANalyzer. The digital and analog outputs can be set, and the digital and analog inputs can be read in a CAPL program.

Installation
1. Copy the file dacapl.dll in the exec32-directory of your CANalyzer-installation. 2. The DLL must be inserted in the list of used CAPL-DLLs. Open the options-dialog (CANalyzer menu Configuration - Options), select Application Settings CAPL DLL and add dacapl.dll to the list. 3. Make sure that the file da_iocab.dll is in the same directory and has the version 1.1.0.3 or higher.

Usage in CAPL

The usage of the dll will be explained with code examples from a CAPL-program.

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

3.1 Variables of the CAPL program


variables { int usedDevice =0; dword hIOcab; long devCountIOcab; //the timers msTimer tmPoll; msTimer tmOutA; msTimer tmOutD; //the timer periods int tmPollTime = 100; int tmOutTimeA = 500; int tmOutTimeD = 1000; //the device number of the device which shall be used //(Attention: the first device has number 0!) //the handle of the DLL //the devide count

//timer used to poll the input-ports //timer to set the analog output-ports //timer to set the digital output-ports

//the buffers to get/set the analog/digital I/O's float prevAVal[1], curAVal[1]; char prevDVal[8], curDVal[9]; //port modes: 0 =off, 1= in, 2=out, 65 = PWM in (only valid for the 4th digital input) dword modeAnalog = 2; dword modeDigital = 1; //analog input voltage range dword voltSel = 0; //pwm-settings dword pwmMode =2; dword pwmFreq =1250; //trigger settings dword trigMode = dword trigPeriod = dword trigSource = dword trigLevel = } selection: 0 ( high voltage) or 1 (low voltage)

//see port modes //scanner frequency, valid = 1250, 2500, 5000, 10000

1; 1; 4; 0;

//1=polling with pll. period, 2=trigger on rising edge, 3=trigger on falling edge, 4= trigger on rising/falling edge //polling period in ms //trigger source (only valid for trigger mode 2-4): 0-3 = digital in 0-3, 4=analog in 4 //trigger level in mV (only valid for source =4)

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

3.2 CAPL on preStart


on preStart { long i, result; char deviceName[256]; int channel; prevAVal[0] = 0.0; memset(curDVal,0,9); memset(prevDVal,0,8); //load the libray which supports the IOcab hIOcab = daLoadLibrary("da_iocab.dll"); write("da_iocab load result: 0x%x",hIOcab); if(hIOcab) { devCountIOcab = daGetDeviceCount(hIOcab); //how many devices (IOcabs) are connected? write("da_iocab # of devices: %d",devCountIOcab); //retrieve info for all attached devices - just to display the connected devices for(i=0;i< devCountIOcab;++i) { result = daGetDeviceInfo(hIOcab, i, deviceName, 256); //what kind of device is it? if(0 == result) { write("da_iocab dev#%d is:%s",i,deviceName); } } //now try to open the device which shall be used result = daGetDeviceInfo(hIOcab, usedDevice, deviceName, 256); //what kind of device is it? if(result == 0) { //set the device-configuration before using the device! daSetModeTrigger(usedDevice,trigMode,trigPeriod,trigSource,trigLevel); daSetModePWM(usedDevice,pwmMode,pwmFreq); for(channel =0; channel < 4; ++channel) { daSetModeAnalog(usedDevice,channel,modeAnalog,voltSel); } for(channel =0; channel < 8; ++channel) { daSetModeDigital(usedDevice,channel,modeDigital); } //open the device result = daOpenDevice(hIOcab,usedDevice);

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

write("da_iocab device #%d open result = 0x%x",usedDevice, result); if(0==result) { result = daPrepStartDevice(hIOcab,usedDevice); write("da_iocab device #%d prep. start result = 0x%x",usedDevice, result); } if(0!=result) { write("da_iocab device #%d could not be opened!",usedDevice); } } else { write("da_iocab device #%d is not found!",usedDevice); } } }

3.3 CAPL on Start


on start { long result; //start the device if(hIOcab) { result = daStartDevice(hIOcab,usedDevice,0); if(result != 0) { write("da_iocab error 0x%x starting device #%d",result,usedDevice); } } }

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

3.4 Setting the Analog Outputs


The setting of the outputs in this example is in a timer-callback, but can also be placed anywhere else in the CAPL-program.
on timer tmOutA {
//generate sawtooth for the for analog outputs float aval[4]={0,0,0,0}; int result,i; for(i=0; i<4; ++i) { result = daSetAOValue(hIOcab,usedDevice,i,aval[i]); if(result != 0) { write("da_iocab: Setting of device #%d, analog channel #%d failed",usedDevice,i); } aval[i] += 0.1 *( i + 1); if(aval[i] > 4.1) aval[i] =0; } }

3.5 Setting the Digital Outputs


The setting of the outputs in this example is in a timer-callback, but can also be placed anywhere else in the CAPL-program.
on timer tmOutD {
char dval = 0x5, val; int result,i; for(i=0; i<4; ++i) { val =dval & (1<<i) ? (char)1:(char)0; result = daSetDOValue(hIOcab,usedDevice,i,val); if(result != 0) { write("da_iocab: Setting of device #%d, digital channel #%d, value = %d failed",usedDevice,i,val); } } }

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

3.6 Reading the Inputs


Reading the input ports in this example is in a timer-callback, but can also be placed anywhere else in the CAPL-program.
on timer tmPoll {
//polls all analog and digital inputs dword tsec[2]; long result, i; int maxAval = 1; int maxDval = 8; char nextDVal[1]; //read and display the analog inputs for(i=0;i<maxAval;++i) { result = daGetAIValueEx2(hIOcab,usedDevice,i,curAVal, tsec); if((result == 0) && (abs(prevAVal[0] - curAVal[0]) > 0.033)) //to eliminate some input jitter { write("time:%f, da_iocab device #%d: AI:%f, t=%f, result = 0x%x", (float)(timeNow())/100000, //current simulation time usedDevice, curAVal[0], tsec[0]+(float)(tsec[1])/1000000, //timestamp in seconds result); prevAVal[0] = curAVal[0]; } } //read and store the digital inputs for(i=0;i<maxDval;++i) { result = daGetDIValueEx2(hIOcab,usedDevice,i,nextDVal, tsec); if((0==result) &&(0!=nextDVal[0])) { curDVal[7-i] = '1'; } else { curDVal[7-i] = '0'; } } //display the digital inputs if they are changed if(0==memcmp(prevDVal, curDVal, i)) { curDVal[i]= '\0'; write("time:%f, da_iocab device #%d: DI:%s, t=%f", (float)(timeNow())/100000, //current simulation time usedDevice, curDVal,

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer --tsec[0]+(float)(tsec[1])/1000000); memcpy(prevDVal, curDVal,8); } } //timestamp in seconds

10

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

11

3.7 Using PWM Out and Capture In


A signal on the PWM Output (IOcab pin 5) can be generated by using the analog outputs 4 and 5. First, these two ports must be configured for output in on preStart:
daSetModePWM(usedDevice,pwmMode,pwmFreq);

with pwmMode = 2 (output, see also chapter CAPL on preStart). The frequency and duty cycle can be set with:
float frequency = 100; //frequency in Hz float duty = 0.5; //duty cycle int result; result = daSetAOValue(hIOcab,usedDevice, 4, frequency); result = daSetAOValue(hIOcab,usedDevice, 5, duty);

If DPWM shall be used to capture a signal, then the DPWM must be configured as input:
daSetModePWM(usedDevice,pwmMode,pwmFreq);

with pwmMode = 1 (use the DPWM pin as input), or with pwmMode = 65 (use the DIO4 as input). The frequency and duty cycle can be read with:
dword tsec[2]; long result; result = daGetAIValueEx2(hIOcab,usedDevice,4,curAVal, tsec); result = daGetAIValueEx2(hIOcab,usedDevice,5,curAVal, tsec); //get the frequency //get the duty cycle

3.8 Helper Functions


memset(char m[], char v, int s) { for(;s>0;--s) { m[s-1]=v; } } int memcmp(char m[], char n[], int s) { for(;s>0;--s) { if(m[s-1]!=n[s-1]) return 0;

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer --} return 1; } memcpy(char m[], char n[], int s) { for(;s>0;--s) { m[s-1]=n[s-1]; } }

12

DLL Functions

4.1 unsigned long CAPLEXPORT far CAPLPASCAL daLoadLibrary(char libraryName[]) Loads the library 'libraryName'. Returns the handle or 0 in case of an error. 4.2 int CAPLEXPORT far CAPLPASCAL daGetDeviceCount(unsigned long libHandle) Returns the device count of the attached devices, which are supported from the loaded DLL. 4.3 int CAPLEXPORT far CAPLPASCAL daGetDeviceInfo(unsigned long libHandle, int dev, char deviceName[], int n) Copies the device info-string in deviceName. Returns 0 if ok, else !=0. Use this function to find out if the device is operable, and the device name. 4.4 int CAPLEXPORT far CAPLPASCAL daOpenDevice(unsigned long libHandle, int dev) Opens the device. Returns 0 if ok, else !=0. This function must be called before the device can be used.

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

13

4.5 int CAPLEXPORT far CAPLPASCAL daCloseDevice(unsigned long libHandle, int dev) Closes the device. Returns 0 if ok, else !=0. 4.6 int CAPLEXPORT far CAPLPASCAL daPrepStartDevice(unsigned long libHandle, int dev) daPrepStartDevice must be called one time in CAPL-PrepStart. The device initializes the measurement. Returns 0 if ok, else !=0. 4.7 int CAPLEXPORT far CAPLPASCAL daStartDevice(unsigned long libHandle, int dev, int reset) daPrepStartDevice must be called one time in CAPL-Start. Returns 0 if ok, else !=0. 4.8 int CAPLEXPORT far CAPLPASCAL daStopDevice(unsigned long libHandle, int dev) daPrepStartDevice must be called one time in CAPL-StopMeasurement. Returns 0 if ok, else !=0. 4.9 long CAPLEXPORT far CAPLPASCAL daGetAIValueEx2(unsigned long libHandle, long device, int channel, double value[],unsigned long time[]) Reads one analog port and stores the value in 'value[]'

The timestamp of the value (that means the time when the port is read) is stored in time[0] (the seconds) and time[1] (the microseconds). The timestamp is set to 0 if the driver does not support this function. Returns 0 if ok, else !=0. 4.10 int CAPLEXPORT far CAPLPASCAL daGetDIValueEx2(unsigned long libHandle, long device, int channel,char value[], unsigned long time[]) Reads one digital port and stores the value in 'value[]'. The timestamp of the value ( that means the time when the port is read) is stored in time[0] (the seconds) and time[1] (the microseconds).

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer --The timestamp is set to 0 if the driver does not support this function. Returns 0 if ok, else !=0.

14

4.11 int CAPLEXPORT far CAPLPASCAL daSetAOValue(unsigned long libHandle, long device, int channel, double value) Sets one analog port. Returns 0 if ok, else !=0. 4.12 int CAPLEXPORT far CAPLPASCAL daSetDOValue(unsigned long libHandle, long device, int channel, long value) Sets one digital port Returns 0 if ok, else !=0. 4.13 int CAPLEXPORT far CAPLPASCAL daSetModeDigital(long device, int channel, unsigned long mode) Sets the configuration for one device. The data will be stored in an ini-file which is located in the exec32-directory. Must be called before calling OpenDevice Returns 0 if ok, else !=0. 4.14 int CAPLEXPORT far CAPLPASCAL daSetModeAnalog(long device, int channel, unsigned long mode, unsigned long voltRange) Sets the configuration for one device. The data will be stored in an ini-file which is located in the exec32-directory. Must be called before calling OpenDevice Returns 0 if ok, else !=0. 4.15 int CAPLEXPORT far CAPLPASCAL daSetModeTrigger(long device, unsigned long mode, unsigned long period, unsigned long source, unsigned long level) Sets the configuration for one device. The data will be stored in an ini-file which is located in the exec32-directory. Must be called before calling OpenDevice Returns 0 if ok, else !=0.

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

DACAPL.DLL, a CAPL DLL to use IOcab with CANalyzer ---

15

4.16 int CAPLEXPORT far CAPLPASCAL daSetModePWM(long device, unsigned long mode, unsigned long freq) Sets the configuration for one device. The data will be stored in an ini-file which is located in the exec32-directory. Must be called before calling OpenDevice Returns 0 if ok, else !=0.

2005 Vector Informatik GmbH

dacapl_dll.doc

Version 1.1 dated 2005-10-11

You might also like